oculus1
diff libovr/Src/Kernel/OVR_Log.cpp @ 3:b069a5c27388
added a couple more stuff, fixed all the LibOVR line endings
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 15 Sep 2013 04:10:05 +0300 |
parents | e2f9e4603129 |
children |
line diff
1.1 --- a/libovr/Src/Kernel/OVR_Log.cpp Sat Sep 14 17:51:03 2013 +0300 1.2 +++ b/libovr/Src/Kernel/OVR_Log.cpp Sun Sep 15 04:10:05 2013 +0300 1.3 @@ -1,1 +1,173 @@ 1.4 -/************************************************************************************ 1.5 1.6 Filename : OVR_Log.cpp 1.7 Content : Logging support 1.8 Created : September 19, 2012 1.9 Notes : 1.10 1.11 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.12 1.13 Use of this software is subject to the terms of the Oculus license 1.14 agreement provided at the time of installation or download, or which 1.15 otherwise accompanies this software in either electronic or hard copy form. 1.16 1.17 ************************************************************************************/ 1.18 1.19 #include "OVR_Log.h" 1.20 #include "OVR_Std.h" 1.21 #include <stdarg.h> 1.22 #include <stdio.h> 1.23 1.24 #if defined(OVR_OS_WIN32) 1.25 #include <windows.h> 1.26 #elif defined(OVR_OS_ANDROID) 1.27 #include <android/log.h> 1.28 #endif 1.29 1.30 namespace OVR { 1.31 1.32 // Global Log pointer. 1.33 Log* volatile OVR_GlobalLog = 0; 1.34 1.35 //----------------------------------------------------------------------------------- 1.36 // ***** Log Implementation 1.37 1.38 Log::~Log() 1.39 { 1.40 // Clear out global log 1.41 if (this == OVR_GlobalLog) 1.42 { 1.43 // TBD: perhaps we should ASSERT if this happens before system shutdown? 1.44 OVR_GlobalLog = 0; 1.45 } 1.46 } 1.47 1.48 void Log::LogMessageVarg(LogMessageType messageType, const char* fmt, va_list argList) 1.49 { 1.50 if ((messageType & LoggingMask) == 0) 1.51 return; 1.52 #ifndef OVR_BUILD_DEBUG 1.53 if (IsDebugMessage(messageType)) 1.54 return; 1.55 #endif 1.56 1.57 char buffer[MaxLogBufferMessageSize]; 1.58 FormatLog(buffer, MaxLogBufferMessageSize, messageType, fmt, argList); 1.59 DefaultLogOutput(buffer, IsDebugMessage(messageType)); 1.60 } 1.61 1.62 void OVR::Log::LogMessage(LogMessageType messageType, const char* pfmt, ...) 1.63 { 1.64 va_list argList; 1.65 va_start(argList, pfmt); 1.66 LogMessageVarg(messageType, pfmt, argList); 1.67 va_end(argList); 1.68 } 1.69 1.70 1.71 void Log::FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType, 1.72 const char* fmt, va_list argList) 1.73 { 1.74 bool addNewline = true; 1.75 1.76 switch(messageType) 1.77 { 1.78 case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); break; 1.79 case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); break; 1.80 case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); break; 1.81 case Log_Text: buffer[0] = 0; addNewline = false; break; 1.82 case Log_DebugText: buffer[0] = 0; addNewline = false; break; 1.83 default: 1.84 buffer[0] = 0; 1.85 addNewline = false; 1.86 break; 1.87 } 1.88 1.89 UPInt prefixLength = OVR_strlen(buffer); 1.90 char *buffer2 = buffer + prefixLength; 1.91 OVR_vsprintf(buffer2, bufferSize - prefixLength, fmt, argList); 1.92 1.93 if (addNewline) 1.94 OVR_strcat(buffer, bufferSize, "\n"); 1.95 } 1.96 1.97 1.98 void Log::DefaultLogOutput(const char* formattedText, bool debug) 1.99 { 1.100 1.101 #if defined(OVR_OS_WIN32) 1.102 // Under Win32, output regular messages to console if it exists; debug window otherwise. 1.103 static DWORD dummyMode; 1.104 static bool hasConsole = (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE) && 1.105 (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &dummyMode)); 1.106 1.107 if (!hasConsole || debug) 1.108 { 1.109 ::OutputDebugStringA(formattedText); 1.110 } 1.111 else 1.112 { 1.113 fputs(formattedText, stdout); 1.114 } 1.115 1.116 #elif defined(OVR_OS_ANDROID) 1.117 __android_log_write(ANDROID_LOG_INFO, "OVR", formattedText); 1.118 1.119 #else 1.120 fputs(formattedText, stdout); 1.121 1.122 #endif 1.123 1.124 // Just in case. 1.125 OVR_UNUSED2(formattedText, debug); 1.126 } 1.127 1.128 1.129 //static 1.130 void Log::SetGlobalLog(Log *log) 1.131 { 1.132 OVR_GlobalLog = log; 1.133 } 1.134 //static 1.135 Log* Log::GetGlobalLog() 1.136 { 1.137 // No global log by default? 1.138 // if (!OVR_GlobalLog) 1.139 // OVR_GlobalLog = GetDefaultLog(); 1.140 return OVR_GlobalLog; 1.141 } 1.142 1.143 //static 1.144 Log* Log::GetDefaultLog() 1.145 { 1.146 // Create default log pointer statically so that it can be used 1.147 // even during startup. 1.148 static Log defaultLog; 1.149 return &defaultLog; 1.150 } 1.151 1.152 1.153 //----------------------------------------------------------------------------------- 1.154 // ***** Global Logging functions 1.155 1.156 #define OVR_LOG_FUNCTION_IMPL(Name) \ 1.157 void Log##Name(const char* fmt, ...) \ 1.158 { \ 1.159 if (OVR_GlobalLog) \ 1.160 { \ 1.161 va_list argList; va_start(argList, fmt); \ 1.162 OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList); \ 1.163 va_end(argList); \ 1.164 } \ 1.165 } 1.166 1.167 OVR_LOG_FUNCTION_IMPL(Text) 1.168 OVR_LOG_FUNCTION_IMPL(Error) 1.169 1.170 #ifdef OVR_BUILD_DEBUG 1.171 OVR_LOG_FUNCTION_IMPL(DebugText) 1.172 OVR_LOG_FUNCTION_IMPL(Debug) 1.173 OVR_LOG_FUNCTION_IMPL(Assert) 1.174 #endif 1.175 1.176 } // OVR 1.177 \ No newline at end of file 1.178 +/************************************************************************************ 1.179 + 1.180 +Filename : OVR_Log.cpp 1.181 +Content : Logging support 1.182 +Created : September 19, 2012 1.183 +Notes : 1.184 + 1.185 +Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.186 + 1.187 +Use of this software is subject to the terms of the Oculus license 1.188 +agreement provided at the time of installation or download, or which 1.189 +otherwise accompanies this software in either electronic or hard copy form. 1.190 + 1.191 +************************************************************************************/ 1.192 + 1.193 +#include "OVR_Log.h" 1.194 +#include "OVR_Std.h" 1.195 +#include <stdarg.h> 1.196 +#include <stdio.h> 1.197 + 1.198 +#if defined(OVR_OS_WIN32) 1.199 +#include <windows.h> 1.200 +#elif defined(OVR_OS_ANDROID) 1.201 +#include <android/log.h> 1.202 +#endif 1.203 + 1.204 +namespace OVR { 1.205 + 1.206 +// Global Log pointer. 1.207 +Log* volatile OVR_GlobalLog = 0; 1.208 + 1.209 +//----------------------------------------------------------------------------------- 1.210 +// ***** Log Implementation 1.211 + 1.212 +Log::~Log() 1.213 +{ 1.214 + // Clear out global log 1.215 + if (this == OVR_GlobalLog) 1.216 + { 1.217 + // TBD: perhaps we should ASSERT if this happens before system shutdown? 1.218 + OVR_GlobalLog = 0; 1.219 + } 1.220 +} 1.221 + 1.222 +void Log::LogMessageVarg(LogMessageType messageType, const char* fmt, va_list argList) 1.223 +{ 1.224 + if ((messageType & LoggingMask) == 0) 1.225 + return; 1.226 +#ifndef OVR_BUILD_DEBUG 1.227 + if (IsDebugMessage(messageType)) 1.228 + return; 1.229 +#endif 1.230 + 1.231 + char buffer[MaxLogBufferMessageSize]; 1.232 + FormatLog(buffer, MaxLogBufferMessageSize, messageType, fmt, argList); 1.233 + DefaultLogOutput(buffer, IsDebugMessage(messageType)); 1.234 +} 1.235 + 1.236 +void OVR::Log::LogMessage(LogMessageType messageType, const char* pfmt, ...) 1.237 +{ 1.238 + va_list argList; 1.239 + va_start(argList, pfmt); 1.240 + LogMessageVarg(messageType, pfmt, argList); 1.241 + va_end(argList); 1.242 +} 1.243 + 1.244 + 1.245 +void Log::FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType, 1.246 + const char* fmt, va_list argList) 1.247 +{ 1.248 + bool addNewline = true; 1.249 + 1.250 + switch(messageType) 1.251 + { 1.252 + case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); break; 1.253 + case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); break; 1.254 + case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); break; 1.255 + case Log_Text: buffer[0] = 0; addNewline = false; break; 1.256 + case Log_DebugText: buffer[0] = 0; addNewline = false; break; 1.257 + default: 1.258 + buffer[0] = 0; 1.259 + addNewline = false; 1.260 + break; 1.261 + } 1.262 + 1.263 + UPInt prefixLength = OVR_strlen(buffer); 1.264 + char *buffer2 = buffer + prefixLength; 1.265 + OVR_vsprintf(buffer2, bufferSize - prefixLength, fmt, argList); 1.266 + 1.267 + if (addNewline) 1.268 + OVR_strcat(buffer, bufferSize, "\n"); 1.269 +} 1.270 + 1.271 + 1.272 +void Log::DefaultLogOutput(const char* formattedText, bool debug) 1.273 +{ 1.274 + 1.275 +#if defined(OVR_OS_WIN32) 1.276 + // Under Win32, output regular messages to console if it exists; debug window otherwise. 1.277 + static DWORD dummyMode; 1.278 + static bool hasConsole = (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE) && 1.279 + (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &dummyMode)); 1.280 + 1.281 + if (!hasConsole || debug) 1.282 + { 1.283 + ::OutputDebugStringA(formattedText); 1.284 + } 1.285 + else 1.286 + { 1.287 + fputs(formattedText, stdout); 1.288 + } 1.289 + 1.290 +#elif defined(OVR_OS_ANDROID) 1.291 + __android_log_write(ANDROID_LOG_INFO, "OVR", formattedText); 1.292 + 1.293 +#else 1.294 + fputs(formattedText, stdout); 1.295 + 1.296 +#endif 1.297 + 1.298 + // Just in case. 1.299 + OVR_UNUSED2(formattedText, debug); 1.300 +} 1.301 + 1.302 + 1.303 +//static 1.304 +void Log::SetGlobalLog(Log *log) 1.305 +{ 1.306 + OVR_GlobalLog = log; 1.307 +} 1.308 +//static 1.309 +Log* Log::GetGlobalLog() 1.310 +{ 1.311 +// No global log by default? 1.312 +// if (!OVR_GlobalLog) 1.313 +// OVR_GlobalLog = GetDefaultLog(); 1.314 + return OVR_GlobalLog; 1.315 +} 1.316 + 1.317 +//static 1.318 +Log* Log::GetDefaultLog() 1.319 +{ 1.320 + // Create default log pointer statically so that it can be used 1.321 + // even during startup. 1.322 + static Log defaultLog; 1.323 + return &defaultLog; 1.324 +} 1.325 + 1.326 + 1.327 +//----------------------------------------------------------------------------------- 1.328 +// ***** Global Logging functions 1.329 + 1.330 +#define OVR_LOG_FUNCTION_IMPL(Name) \ 1.331 + void Log##Name(const char* fmt, ...) \ 1.332 + { \ 1.333 + if (OVR_GlobalLog) \ 1.334 + { \ 1.335 + va_list argList; va_start(argList, fmt); \ 1.336 + OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList); \ 1.337 + va_end(argList); \ 1.338 + } \ 1.339 + } 1.340 + 1.341 +OVR_LOG_FUNCTION_IMPL(Text) 1.342 +OVR_LOG_FUNCTION_IMPL(Error) 1.343 + 1.344 +#ifdef OVR_BUILD_DEBUG 1.345 +OVR_LOG_FUNCTION_IMPL(DebugText) 1.346 +OVR_LOG_FUNCTION_IMPL(Debug) 1.347 +OVR_LOG_FUNCTION_IMPL(Assert) 1.348 +#endif 1.349 + 1.350 +} // OVR