oculus1

diff libovr/Src/Kernel/OVR_Log.cpp @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children b069a5c27388
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/Kernel/OVR_Log.cpp	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,1 @@
     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