OVR_Log.cpp
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   OVR_Log.cpp
00004 Content     :   Logging support
00005 Created     :   September 19, 2012
00006 Notes       : 
00007 
00008 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00009 
00010 Use of this software is subject to the terms of the Oculus license
00011 agreement provided at the time of installation or download, or which
00012 otherwise accompanies this software in either electronic or hard copy form.
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 // Global Log pointer.
00030 Log* volatile OVR_GlobalLog = 0;
00031 
00032 //-----------------------------------------------------------------------------------
00033 // ***** Log Implementation
00034 
00035 Log::~Log()
00036 {
00037     // Clear out global log
00038     if (this == OVR_GlobalLog)
00039     {
00040         // TBD: perhaps we should ASSERT if this happens before system shutdown?
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     // Under Win32, output regular messages to console if it exists; debug window otherwise.
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     // Just in case.
00122     OVR_UNUSED2(formattedText, debug);
00123 }
00124 
00125 
00126 //static
00127 void Log::SetGlobalLog(Log *log)
00128 {
00129     OVR_GlobalLog = log;
00130 }
00131 //static
00132 Log* Log::GetGlobalLog()
00133 {
00134 // No global log by default?
00135 //    if (!OVR_GlobalLog)
00136 //        OVR_GlobalLog = GetDefaultLog();
00137     return OVR_GlobalLog;
00138 }
00139 
00140 //static
00141 Log* Log::GetDefaultLog()
00142 {
00143     // Create default log pointer statically so that it can be used
00144     // even during startup.
00145     static Log defaultLog;
00146     return &defaultLog;
00147 }
00148 
00149 
00150 //-----------------------------------------------------------------------------------
00151 // ***** Global Logging functions
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 } // OVR


oculus_sdk
Author(s):
autogenerated on Fri Aug 28 2015 11:53:11