Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_Log.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3 PublicHeader: OVR
4 Filename : OVR_Log.h
5 Content : Logging support
6 Created : September 19, 2012
7 Notes :
8 
9 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
10 
11 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
13 which is provided at the time of installation or download, or which
14 otherwise accompanies this software in either electronic or hard copy form.
15 
16 You may obtain a copy of the License at
17 
18 http://www.oculusvr.com/licenses/LICENSE-3.1
19 
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
25 
26 ************************************************************************************/
27 
28 #ifndef OVR_Log_h
29 #define OVR_Log_h
30 
31 #include "OVR_Types.h"
32 #include <stdarg.h>
33 
34 namespace OVR {
35 
36 //-----------------------------------------------------------------------------------
37 // ***** Logging Constants
38 
39 // LogMaskConstants defined bit mask constants that describe what log messages
40 // should be displayed.
42 {
43  LogMask_Regular = 0x100,
44  LogMask_Debug = 0x200,
47 };
48 
49 
50 // LogMessageType describes the type of the log message, controls when it is
51 // displayed and what prefix/suffix is given to it. Messages are subdivided into
52 // regular and debug logging types. Debug logging is only generated in debug builds.
53 //
54 // Log_Text - General output text displayed without prefix or new-line.
55 // Used in OVR libraries for general log flow messages
56 // such as "Device Initialized".
57 //
58 // Log_Error - Error message output with "Error: %s\n", intended for
59 // application/sample-level use only, in cases where an expected
60 // operation failed. OVR libraries should not use this internally,
61 // reporting status codes instead.
62 //
63 // Log_DebugText - Message without prefix or new lines; output in Debug build only.
64 //
65 // Log_Debug - Debug-build only message, formatted with "Debug: %s\n".
66 // Intended to comment on incorrect API usage that doesn't lead
67 // to crashes but can be avoided with proper use.
68 // There is no Debug Error on purpose, since real errors should
69 // be handled by API user.
70 //
71 // Log_Assert - Debug-build only message, formatted with "Assert: %s\n".
72 // Intended for severe unrecoverable conditions in library
73 // source code. Generated though OVR_ASSERT_MSG(c, "Text").
74 
76 {
77  // General Logging
79  Log_Error = LogMask_Regular | 1, // "Error: %s\n".
80 
81  // Debug-only messages (not generated in release build)
83  Log_Debug = LogMask_Debug | 1, // "Debug: %s\n".
84  Log_Assert = LogMask_Debug | 2, // "Assert: %s\n".
85 };
86 
87 
88 // LOG_VAARG_ATTRIBUTE macro, enforces printf-style fromatting for message types
89 #ifdef __GNUC__
90 # define OVR_LOG_VAARG_ATTRIBUTE(a,b) __attribute__((format (printf, a, b)))
91 #else
92 # define OVR_LOG_VAARG_ATTRIBUTE(a,b)
93 #endif
94 
95 
96 //-----------------------------------------------------------------------------------
97 // ***** Log
98 
99 // Log defines a base class interface that can be implemented to catch both
100 // debug and runtime messages.
101 // Debug logging can be overridden by calling Log::SetGlobalLog.
102 
103 class Log
104 {
105  friend class System;
106 public:
107  Log(unsigned logMask = LogMask_Debug) : LoggingMask(logMask) { }
108  virtual ~Log();
109 
110  // Log formating buffer size used by default LogMessageVarg. Longer strings are truncated.
111  enum { MaxLogBufferMessageSize = 4096 };
112 
113  unsigned GetLoggingMask() const { return LoggingMask; }
114  void SetLoggingMask(unsigned logMask) { LoggingMask = logMask; }
115 
116  // This virtual function receives all the messages,
117  // developers should override this function in order to do custom logging
118  virtual void LogMessageVarg(LogMessageType messageType, const char* fmt, va_list argList);
119 
120  // Call the logging function with specific message type, with no type filtering.
121  void LogMessage(LogMessageType messageType,
122  const char* fmt, ...) OVR_LOG_VAARG_ATTRIBUTE(3,4);
123 
124 
125  // Helper used by LogMessageVarg to format the log message, writing the resulting
126  // string into buffer. It formats text based on fmt and appends prefix/new line
127  // based on LogMessageType.
128  static void FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType,
129  const char* fmt, va_list argList);
130 
131  // Default log output implementation used by by LogMessageVarg.
132  // Debug flag may be used to re-direct output on some platforms, but doesn't
133  // necessarily disable it in release builds; that is the job of the called.
134  static void DefaultLogOutput(const char* textBuffer, bool debug);
135 
136  // Determines if the specified message type is for debugging only.
137  static bool IsDebugMessage(LogMessageType messageType)
138  {
139  return (messageType & LogMask_Debug) != 0;
140  }
141 
142  // *** Global APIs
143 
144  // Global Log registration APIs.
145  // - Global log is used for OVR_DEBUG messages. Set global log to null (0)
146  // to disable all logging.
147  static void SetGlobalLog(Log *log);
148  static Log* GetGlobalLog();
149 
150  // Returns default log singleton instance.
151  static Log* GetDefaultLog();
152 
153  // Applies logMask to the default log and returns a pointer to it.
154  // By default, only Debug logging is enabled, so to avoid SDK generating console
155  // messages in user app (those are always disabled in release build,
156  // even if the flag is set). This function is useful in System constructor.
157  static Log* ConfigureDefaultLog(unsigned logMask = LogMask_Debug)
158  {
159  Log* log = GetDefaultLog();
160  log->SetLoggingMask(logMask);
161  return log;
162  }
163 
164 private:
165  // Logging mask described by LogMaskConstants.
166  unsigned LoggingMask;
167 };
168 
169 
170 //-----------------------------------------------------------------------------------
171 // ***** Global Logging Functions and Debug Macros
172 
173 // These functions will output text to global log with semantics described by
174 // their LogMessageType.
175 void LogText(const char* fmt, ...) OVR_LOG_VAARG_ATTRIBUTE(1,2);
176 void LogError(const char* fmt, ...) OVR_LOG_VAARG_ATTRIBUTE(1,2);
177 
178 #ifdef OVR_BUILD_DEBUG
179 
180  // Debug build only logging.
181  void LogDebugText(const char* fmt, ...) OVR_LOG_VAARG_ATTRIBUTE(1,2);
182  void LogDebug(const char* fmt, ...) OVR_LOG_VAARG_ATTRIBUTE(1,2);
183  void LogAssert(const char* fmt, ...) OVR_LOG_VAARG_ATTRIBUTE(1,2);
184 
185  // Macro to do debug logging, printf-style.
186  // An extra set of set of parenthesis must be used around arguments,
187  // as in: OVR_LOG_DEBUG(("Value %d", 2)).
188  #define OVR_DEBUG_LOG(args) do { OVR::LogDebug args; } while(0)
189  #define OVR_DEBUG_LOG_TEXT(args) do { OVR::LogDebugText args; } while(0)
190 
191  #define OVR_ASSERT_LOG(c, args) do { if (!(c)) { OVR::LogAssert args; OVR_DEBUG_BREAK; } } while(0)
192 
193 #else
194 
195  // If not in debug build, macros do nothing.
196  #define OVR_DEBUG_LOG(args) ((void)0)
197  #define OVR_DEBUG_LOG_TEXT(args) ((void)0)
198  #define OVR_ASSERT_LOG(c, args) ((void)0)
199 
200 #endif
201 
202 } // OVR
203 
204 #endif
virtual ~Log()
Definition: OVR_Log.cpp:46
void LogText(const char *fmt,...) OVR_LOG_VAARG_ATTRIBUTE(1
#define OVR_LOG_VAARG_ATTRIBUTE(a, b)
Definition: OVR_Log.h:92
static bool IsDebugMessage(LogMessageType messageType)
Definition: OVR_Log.h:137
unsigned LoggingMask
Definition: OVR_Log.h:166
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
LogMessageType
Definition: OVR_Log.h:75
void void LogError(const char *fmt,...) OVR_LOG_VAARG_ATTRIBUTE(1
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
void static void FormatLog(char *buffer, unsigned bufferSize, LogMessageType messageType, const char *fmt, va_list argList)
Definition: OVR_Log.cpp:79
void SetLoggingMask(unsigned logMask)
Definition: OVR_Log.h:114
static Log * GetGlobalLog()
Definition: OVR_Log.cpp:143
LogMaskConstants
Definition: OVR_Log.h:41
unsigned GetLoggingMask() const
Definition: OVR_Log.h:113
static Log * ConfigureDefaultLog(unsigned logMask=LogMask_Debug)
Definition: OVR_Log.h:157
Log(unsigned logMask=LogMask_Debug)
Definition: OVR_Log.h:107