Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_Log.cpp
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_Log.cpp
4 Content : Logging support
5 Created : September 19, 2012
6 Notes :
7 
8 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
9 
10 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
14 
15 You may obtain a copy of the License at
16 
17 http://www.oculusvr.com/licenses/LICENSE-3.1
18 
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24 
25 ************************************************************************************/
26 
27 #include "OVR_Log.h"
28 #include "OVR_Std.h"
29 #include <stdarg.h>
30 #include <stdio.h>
31 
32 #if defined(OVR_OS_WIN32)
33 #include <windows.h>
34 #elif defined(OVR_OS_ANDROID)
35 #include <android/log.h>
36 #endif
37 
38 namespace OVR {
39 
40 // Global Log pointer.
41 Log* volatile OVR_GlobalLog = 0;
42 
43 //-----------------------------------------------------------------------------------
44 // ***** Log Implementation
45 
47 {
48  // Clear out global log
49  if (this == OVR_GlobalLog)
50  {
51  // TBD: perhaps we should ASSERT if this happens before system shutdown?
52  OVR_GlobalLog = 0;
53  }
54 }
55 
56 void Log::LogMessageVarg(LogMessageType messageType, const char* fmt, va_list argList)
57 {
58  if ((messageType & LoggingMask) == 0)
59  return;
60 #ifndef OVR_BUILD_DEBUG
61  if (IsDebugMessage(messageType))
62  return;
63 #endif
64 
65  char buffer[MaxLogBufferMessageSize];
66  FormatLog(buffer, MaxLogBufferMessageSize, messageType, fmt, argList);
67  DefaultLogOutput(buffer, IsDebugMessage(messageType));
68 }
69 
70 void OVR::Log::LogMessage(LogMessageType messageType, const char* pfmt, ...)
71 {
72  va_list argList;
73  va_start(argList, pfmt);
74  LogMessageVarg(messageType, pfmt, argList);
75  va_end(argList);
76 }
77 
78 
79 void Log::FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType,
80  const char* fmt, va_list argList)
81 {
82  bool addNewline = true;
83 
84  switch(messageType)
85  {
86  case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); break;
87  case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); break;
88  case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); break;
89  case Log_Text: buffer[0] = 0; addNewline = false; break;
90  case Log_DebugText: buffer[0] = 0; addNewline = false; break;
91  default:
92  buffer[0] = 0;
93  addNewline = false;
94  break;
95  }
96 
97  UPInt prefixLength = OVR_strlen(buffer);
98  char *buffer2 = buffer + prefixLength;
99  OVR_vsprintf(buffer2, bufferSize - prefixLength, fmt, argList);
100 
101  if (addNewline)
102  OVR_strcat(buffer, bufferSize, "\n");
103 }
104 
105 
106 void Log::DefaultLogOutput(const char* formattedText, bool debug)
107 {
108 
109 #if defined(OVR_OS_WIN32)
110  // Under Win32, output regular messages to console if it exists; debug window otherwise.
111  static DWORD dummyMode;
112  static bool hasConsole = (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE) &&
113  (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &dummyMode));
114 
115  if (!hasConsole || debug)
116  {
117  ::OutputDebugStringA(formattedText);
118  }
119  else
120  {
121  fputs(formattedText, stdout);
122  }
123 
124 #elif defined(OVR_OS_ANDROID)
125  __android_log_write(ANDROID_LOG_INFO, "OVR", formattedText);
126 
127 #else
128  fputs(formattedText, stdout);
129 
130 #endif
131 
132  // Just in case.
133  OVR_UNUSED2(formattedText, debug);
134 }
135 
136 
137 //static
139 {
140  OVR_GlobalLog = log;
141 }
142 //static
144 {
145 // No global log by default?
146 // if (!OVR_GlobalLog)
147 // OVR_GlobalLog = GetDefaultLog();
148  return OVR_GlobalLog;
149 }
150 
151 //static
153 {
154  // Create default log pointer statically so that it can be used
155  // even during startup.
156  static Log defaultLog;
157  return &defaultLog;
158 }
159 
160 
161 //-----------------------------------------------------------------------------------
162 // ***** Global Logging functions
163 
164 #define OVR_LOG_FUNCTION_IMPL(Name) \
165  void Log##Name(const char* fmt, ...) \
166  { \
167  if (OVR_GlobalLog) \
168  { \
169  va_list argList; va_start(argList, fmt); \
170  OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList); \
171  va_end(argList); \
172  } \
173  }
174 
177 
178 #ifdef OVR_BUILD_DEBUG
179 OVR_LOG_FUNCTION_IMPL(DebugText)
181 OVR_LOG_FUNCTION_IMPL(Assert)
182 #endif
183 
184 } // OVR
virtual ~Log()
Definition: OVR_Log.cpp:46
UPInt OVR_CDECL OVR_vsprintf(char *dest, UPInt destsize, const char *format, va_list argList)
Definition: OVR_Std.h:304
static bool IsDebugMessage(LogMessageType messageType)
Definition: OVR_Log.h:137
unsigned LoggingMask
Definition: OVR_Log.h:166
#define OVR_LOG_FUNCTION_IMPL(Name)
Definition: OVR_Log.cpp:164
static void DefaultLogOutput(const char *textBuffer, bool debug)
Definition: OVR_Log.cpp:106
static void SetGlobalLog(Log *log)
Definition: OVR_Log.cpp:138
virtual void LogMessageVarg(LogMessageType messageType, const char *fmt, va_list argList)
Definition: OVR_Log.cpp:56
size_t UPInt
Definition: OVR_Types.h:218
LogMessageType
Definition: OVR_Log.h:75
char *OVR_CDECL OVR_strcpy(char *dest, UPInt destsize, const char *src)
Definition: OVR_Std.h:148
void LogMessage(LogMessageType messageType, const char *fmt,...) OVR_LOG_VAARG_ATTRIBUTE(3
Definition: OVR_Log.cpp:70
static Log * GetDefaultLog()
Definition: OVR_Log.cpp:152
char *OVR_CDECL OVR_strcat(char *dest, UPInt destsize, const char *src)
Definition: OVR_Std.h:170
void static void FormatLog(char *buffer, unsigned bufferSize, LogMessageType messageType, const char *fmt, va_list argList)
Definition: OVR_Log.cpp:79
static Log * GetGlobalLog()
Definition: OVR_Log.cpp:143
Log *volatile OVR_GlobalLog
Definition: OVR_Log.cpp:41
UPInt OVR_CDECL OVR_strlen(const char *str)
Definition: OVR_Std.h:143
#define OVR_UNUSED2(a1, a2)