Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "OVR_Log.h"
00017 #include "OVR_Std.h"
00018 #include <stdarg.h>
00019 #include <stdio.h>
00020
00021 #if defined(OVR_OS_WIN32)
00022 #include <windows.h>
00023 #elif defined(OVR_OS_ANDROID)
00024 #include <android/log.h>
00025 #endif
00026
00027 namespace OVR {
00028
00029
00030 Log* volatile OVR_GlobalLog = 0;
00031
00032
00033
00034
00035 Log::~Log()
00036 {
00037
00038 if (this == OVR_GlobalLog)
00039 {
00040
00041 OVR_GlobalLog = 0;
00042 }
00043 }
00044
00045 void Log::LogMessageVarg(LogMessageType messageType, const char* fmt, va_list argList)
00046 {
00047 if ((messageType & LoggingMask) == 0)
00048 return;
00049 #ifndef OVR_BUILD_DEBUG
00050 if (IsDebugMessage(messageType))
00051 return;
00052 #endif
00053
00054 char buffer[MaxLogBufferMessageSize];
00055 FormatLog(buffer, MaxLogBufferMessageSize, messageType, fmt, argList);
00056 DefaultLogOutput(buffer, IsDebugMessage(messageType));
00057 }
00058
00059 void OVR::Log::LogMessage(LogMessageType messageType, const char* pfmt, ...)
00060 {
00061 va_list argList;
00062 va_start(argList, pfmt);
00063 LogMessageVarg(messageType, pfmt, argList);
00064 va_end(argList);
00065 }
00066
00067
00068 void Log::FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType,
00069 const char* fmt, va_list argList)
00070 {
00071 bool addNewline = true;
00072
00073 switch(messageType)
00074 {
00075 case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); break;
00076 case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); break;
00077 case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); break;
00078 case Log_Text: buffer[0] = 0; addNewline = false; break;
00079 case Log_DebugText: buffer[0] = 0; addNewline = false; break;
00080 default:
00081 buffer[0] = 0;
00082 addNewline = false;
00083 break;
00084 }
00085
00086 UPInt prefixLength = OVR_strlen(buffer);
00087 char *buffer2 = buffer + prefixLength;
00088 OVR_vsprintf(buffer2, bufferSize - prefixLength, fmt, argList);
00089
00090 if (addNewline)
00091 OVR_strcat(buffer, bufferSize, "\n");
00092 }
00093
00094
00095 void Log::DefaultLogOutput(const char* formattedText, bool debug)
00096 {
00097
00098 #if defined(OVR_OS_WIN32)
00099
00100 static DWORD dummyMode;
00101 static bool hasConsole = (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE) &&
00102 (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &dummyMode));
00103
00104 if (!hasConsole || debug)
00105 {
00106 ::OutputDebugStringA(formattedText);
00107 }
00108 else
00109 {
00110 fputs(formattedText, stdout);
00111 }
00112
00113 #elif defined(OVR_OS_ANDROID)
00114 __android_log_write(ANDROID_LOG_INFO, "OVR", formattedText);
00115
00116 #else
00117 fputs(formattedText, stdout);
00118
00119 #endif
00120
00121
00122 OVR_UNUSED2(formattedText, debug);
00123 }
00124
00125
00126
00127 void Log::SetGlobalLog(Log *log)
00128 {
00129 OVR_GlobalLog = log;
00130 }
00131
00132 Log* Log::GetGlobalLog()
00133 {
00134
00135
00136
00137 return OVR_GlobalLog;
00138 }
00139
00140
00141 Log* Log::GetDefaultLog()
00142 {
00143
00144
00145 static Log defaultLog;
00146 return &defaultLog;
00147 }
00148
00149
00150
00151
00152
00153 #define OVR_LOG_FUNCTION_IMPL(Name) \
00154 void Log##Name(const char* fmt, ...) \
00155 { \
00156 if (OVR_GlobalLog) \
00157 { \
00158 va_list argList; va_start(argList, fmt); \
00159 OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList); \
00160 va_end(argList); \
00161 } \
00162 }
00163
00164 OVR_LOG_FUNCTION_IMPL(Text)
00165 OVR_LOG_FUNCTION_IMPL(Error)
00166
00167 #ifdef OVR_BUILD_DEBUG
00168 OVR_LOG_FUNCTION_IMPL(DebugText)
00169 OVR_LOG_FUNCTION_IMPL(Debug)
00170 OVR_LOG_FUNCTION_IMPL(Assert)
00171 #endif
00172
00173 }