oculus1

view libovr/Src/Kernel/OVR_Log.cpp @ 10:b2abb08c8f94

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