oculus1

annotate libovr/Src/Kernel/OVR_Log.cpp @ 12:d797639e0234

moving on to the distortion... not correct yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 10:14:29 +0300
parents e2f9e4603129
children
rev   line source
nuclear@3 1 /************************************************************************************
nuclear@3 2
nuclear@3 3 Filename : OVR_Log.cpp
nuclear@3 4 Content : Logging support
nuclear@3 5 Created : September 19, 2012
nuclear@3 6 Notes :
nuclear@3 7
nuclear@3 8 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
nuclear@3 9
nuclear@3 10 Use of this software is subject to the terms of the Oculus license
nuclear@3 11 agreement provided at the time of installation or download, or which
nuclear@3 12 otherwise accompanies this software in either electronic or hard copy form.
nuclear@3 13
nuclear@3 14 ************************************************************************************/
nuclear@3 15
nuclear@3 16 #include "OVR_Log.h"
nuclear@3 17 #include "OVR_Std.h"
nuclear@3 18 #include <stdarg.h>
nuclear@3 19 #include <stdio.h>
nuclear@3 20
nuclear@3 21 #if defined(OVR_OS_WIN32)
nuclear@3 22 #include <windows.h>
nuclear@3 23 #elif defined(OVR_OS_ANDROID)
nuclear@3 24 #include <android/log.h>
nuclear@3 25 #endif
nuclear@3 26
nuclear@3 27 namespace OVR {
nuclear@3 28
nuclear@3 29 // Global Log pointer.
nuclear@3 30 Log* volatile OVR_GlobalLog = 0;
nuclear@3 31
nuclear@3 32 //-----------------------------------------------------------------------------------
nuclear@3 33 // ***** Log Implementation
nuclear@3 34
nuclear@3 35 Log::~Log()
nuclear@3 36 {
nuclear@3 37 // Clear out global log
nuclear@3 38 if (this == OVR_GlobalLog)
nuclear@3 39 {
nuclear@3 40 // TBD: perhaps we should ASSERT if this happens before system shutdown?
nuclear@3 41 OVR_GlobalLog = 0;
nuclear@3 42 }
nuclear@3 43 }
nuclear@3 44
nuclear@3 45 void Log::LogMessageVarg(LogMessageType messageType, const char* fmt, va_list argList)
nuclear@3 46 {
nuclear@3 47 if ((messageType & LoggingMask) == 0)
nuclear@3 48 return;
nuclear@3 49 #ifndef OVR_BUILD_DEBUG
nuclear@3 50 if (IsDebugMessage(messageType))
nuclear@3 51 return;
nuclear@3 52 #endif
nuclear@3 53
nuclear@3 54 char buffer[MaxLogBufferMessageSize];
nuclear@3 55 FormatLog(buffer, MaxLogBufferMessageSize, messageType, fmt, argList);
nuclear@3 56 DefaultLogOutput(buffer, IsDebugMessage(messageType));
nuclear@3 57 }
nuclear@3 58
nuclear@3 59 void OVR::Log::LogMessage(LogMessageType messageType, const char* pfmt, ...)
nuclear@3 60 {
nuclear@3 61 va_list argList;
nuclear@3 62 va_start(argList, pfmt);
nuclear@3 63 LogMessageVarg(messageType, pfmt, argList);
nuclear@3 64 va_end(argList);
nuclear@3 65 }
nuclear@3 66
nuclear@3 67
nuclear@3 68 void Log::FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType,
nuclear@3 69 const char* fmt, va_list argList)
nuclear@3 70 {
nuclear@3 71 bool addNewline = true;
nuclear@3 72
nuclear@3 73 switch(messageType)
nuclear@3 74 {
nuclear@3 75 case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); break;
nuclear@3 76 case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); break;
nuclear@3 77 case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); break;
nuclear@3 78 case Log_Text: buffer[0] = 0; addNewline = false; break;
nuclear@3 79 case Log_DebugText: buffer[0] = 0; addNewline = false; break;
nuclear@3 80 default:
nuclear@3 81 buffer[0] = 0;
nuclear@3 82 addNewline = false;
nuclear@3 83 break;
nuclear@3 84 }
nuclear@3 85
nuclear@3 86 UPInt prefixLength = OVR_strlen(buffer);
nuclear@3 87 char *buffer2 = buffer + prefixLength;
nuclear@3 88 OVR_vsprintf(buffer2, bufferSize - prefixLength, fmt, argList);
nuclear@3 89
nuclear@3 90 if (addNewline)
nuclear@3 91 OVR_strcat(buffer, bufferSize, "\n");
nuclear@3 92 }
nuclear@3 93
nuclear@3 94
nuclear@3 95 void Log::DefaultLogOutput(const char* formattedText, bool debug)
nuclear@3 96 {
nuclear@3 97
nuclear@3 98 #if defined(OVR_OS_WIN32)
nuclear@3 99 // Under Win32, output regular messages to console if it exists; debug window otherwise.
nuclear@3 100 static DWORD dummyMode;
nuclear@3 101 static bool hasConsole = (GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE) &&
nuclear@3 102 (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &dummyMode));
nuclear@3 103
nuclear@3 104 if (!hasConsole || debug)
nuclear@3 105 {
nuclear@3 106 ::OutputDebugStringA(formattedText);
nuclear@3 107 }
nuclear@3 108 else
nuclear@3 109 {
nuclear@3 110 fputs(formattedText, stdout);
nuclear@3 111 }
nuclear@3 112
nuclear@3 113 #elif defined(OVR_OS_ANDROID)
nuclear@3 114 __android_log_write(ANDROID_LOG_INFO, "OVR", formattedText);
nuclear@3 115
nuclear@3 116 #else
nuclear@3 117 fputs(formattedText, stdout);
nuclear@3 118
nuclear@3 119 #endif
nuclear@3 120
nuclear@3 121 // Just in case.
nuclear@3 122 OVR_UNUSED2(formattedText, debug);
nuclear@3 123 }
nuclear@3 124
nuclear@3 125
nuclear@3 126 //static
nuclear@3 127 void Log::SetGlobalLog(Log *log)
nuclear@3 128 {
nuclear@3 129 OVR_GlobalLog = log;
nuclear@3 130 }
nuclear@3 131 //static
nuclear@3 132 Log* Log::GetGlobalLog()
nuclear@3 133 {
nuclear@3 134 // No global log by default?
nuclear@3 135 // if (!OVR_GlobalLog)
nuclear@3 136 // OVR_GlobalLog = GetDefaultLog();
nuclear@3 137 return OVR_GlobalLog;
nuclear@3 138 }
nuclear@3 139
nuclear@3 140 //static
nuclear@3 141 Log* Log::GetDefaultLog()
nuclear@3 142 {
nuclear@3 143 // Create default log pointer statically so that it can be used
nuclear@3 144 // even during startup.
nuclear@3 145 static Log defaultLog;
nuclear@3 146 return &defaultLog;
nuclear@3 147 }
nuclear@3 148
nuclear@3 149
nuclear@3 150 //-----------------------------------------------------------------------------------
nuclear@3 151 // ***** Global Logging functions
nuclear@3 152
nuclear@3 153 #define OVR_LOG_FUNCTION_IMPL(Name) \
nuclear@3 154 void Log##Name(const char* fmt, ...) \
nuclear@3 155 { \
nuclear@3 156 if (OVR_GlobalLog) \
nuclear@3 157 { \
nuclear@3 158 va_list argList; va_start(argList, fmt); \
nuclear@3 159 OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList); \
nuclear@3 160 va_end(argList); \
nuclear@3 161 } \
nuclear@3 162 }
nuclear@3 163
nuclear@3 164 OVR_LOG_FUNCTION_IMPL(Text)
nuclear@3 165 OVR_LOG_FUNCTION_IMPL(Error)
nuclear@3 166
nuclear@3 167 #ifdef OVR_BUILD_DEBUG
nuclear@3 168 OVR_LOG_FUNCTION_IMPL(DebugText)
nuclear@3 169 OVR_LOG_FUNCTION_IMPL(Debug)
nuclear@3 170 OVR_LOG_FUNCTION_IMPL(Assert)
nuclear@3 171 #endif
nuclear@3 172
nuclear@3 173 } // OVR