diff options
author | Sven Gothel <[email protected]> | 2015-03-21 21:19:34 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2015-03-21 21:19:34 +0100 |
commit | e490c3c7f7bb5461cfa78a214827aa534fb43a3e (patch) | |
tree | b86b0291ef529ec6b75cc548d73599fa9c283cd6 /LibOVR/Src/Kernel/OVR_Log.cpp | |
parent | 05bb4364bfd9930fb1902efec86446ef035ee07a (diff) |
Bump OculusVR RIFT SDK to 0.4.4
Diffstat (limited to 'LibOVR/Src/Kernel/OVR_Log.cpp')
-rw-r--r-- | LibOVR/Src/Kernel/OVR_Log.cpp | 316 |
1 files changed, 282 insertions, 34 deletions
diff --git a/LibOVR/Src/Kernel/OVR_Log.cpp b/LibOVR/Src/Kernel/OVR_Log.cpp index d5f8a68..2631879 100644 --- a/LibOVR/Src/Kernel/OVR_Log.cpp +++ b/LibOVR/Src/Kernel/OVR_Log.cpp @@ -5,16 +5,16 @@ Content : Logging support Created : September 19, 2012 Notes : -Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved. +Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. -Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); you may not use the Oculus VR Rift SDK except in compliance with the License, which is provided at the time of installation or download, or which otherwise accompanies this software in either electronic or hard copy form. You may obtain a copy of the License at -http://www.oculusvr.com/licenses/LICENSE-3.1 +http://www.oculusvr.com/licenses/LICENSE-3.2 Unless required by applicable law or agreed to in writing, the Oculus VR SDK distributed under the License is distributed on an "AS IS" BASIS, @@ -28,23 +28,83 @@ limitations under the License. #include "OVR_Std.h" #include <stdarg.h> #include <stdio.h> +#include <time.h> +#include "../Kernel/OVR_System.h" +#include "../Kernel/OVR_DebugHelp.h" +#include "../Util/Util_SystemGUI.h" -#if defined(OVR_OS_WIN32) +#if defined(OVR_OS_MS) && !defined(OVR_OS_MS_MOBILE) +#define WIN32_LEAN_AND_MEAN #include <windows.h> #elif defined(OVR_OS_ANDROID) #include <android/log.h> +#elif defined(OVR_OS_LINUX) || defined(OVR_OS_MAC) || defined(OVR_OS_UNIX) +#include <syslog.h> #endif + +class LogSubject : public OVR::SystemSingletonBase<LogSubject> +{ + static bool isShuttingDown; + +public: + + LogSubject(){ + isShuttingDown = false; + // Must be at end of function + PushDestroyCallbacks(); + } + + virtual ~LogSubject(){} // Required because we use delete this below. + + virtual void OnThreadDestroy() + { + isShuttingDown = true; + } + + virtual void OnSystemDestroy() + { + delete this; + } + + static bool IsValid() { + return isShuttingDown == false; + } + + OVR::Lock logSubjectLock; + OVR::ObserverScope<OVR::Log::LogHandler> logSubject; +}; + +bool LogSubject::isShuttingDown; + +OVR_DEFINE_SINGLETON(LogSubject); + namespace OVR { -// Global Log pointer. -Log* volatile OVR_GlobalLog = 0; + // Global Log pointer. + Log* volatile OVR_GlobalLog = 0; //----------------------------------------------------------------------------------- // ***** Log Implementation +Log::Log(unsigned logMask) : + LoggingMask(logMask) +{ +#ifdef OVR_OS_WIN32 + hEventSource = RegisterEventSourceA(NULL, "OculusVR"); + OVR_ASSERT(hEventSource != NULL); +#endif +} + Log::~Log() { +#ifdef OVR_OS_WIN32 + if (hEventSource) + { + DeregisterEventSource(hEventSource); + } +#endif + // Clear out global log if (this == OVR_GlobalLog) { @@ -52,6 +112,49 @@ Log::~Log() OVR_GlobalLog = 0; } } +void Log::AddLogObserver(ObserverScope<LogHandler> *logObserver) +{ + if (OVR::System::IsInitialized() && LogSubject::GetInstance()->IsValid()) + { + Lock::Locker locker(&LogSubject::GetInstance()->logSubjectLock); + logObserver->GetPtr()->Observe(LogSubject::GetInstance()->logSubject); + } +} +void Log::LogMessageVargInt(LogMessageType messageType, const char* fmt, va_list argList) +{ + if (OVR::System::IsInitialized() && LogSubject::GetInstance()->IsValid()) + { + // Invoke subject + char buffer[MaxLogBufferMessageSize]; + char* pBuffer = buffer; + char* pAllocated = NULL; + + #if !defined(OVR_CC_MSVC) // Non-Microsoft compilers require you to save a copy of the va_list. + va_list argListSaved; + va_copy(argListSaved, argList); + #endif + + int result = FormatLog(pBuffer, MaxLogBufferMessageSize, Log_Text, fmt, argList); + + if(result >= MaxLogBufferMessageSize) // If there was insufficient capacity... + { + pAllocated = (char*)OVR_ALLOC(result + 1); // We assume C++ exceptions are disabled. FormatLog will handle the case that pAllocated is NULL. + pBuffer = pAllocated; + + #if !defined(OVR_CC_MSVC) + va_end(argList); // The caller owns argList and will call va_end on it. + va_copy(argList, argListSaved); + #endif + + FormatLog(pBuffer, (size_t)result + 1, Log_Text, fmt, argList); + } + + Lock::Locker locker(&LogSubject::GetInstance()->logSubjectLock); + LogSubject::GetInstance()->logSubject.GetPtr()->Call(pBuffer, messageType); + + delete[] pAllocated; + } +} void Log::LogMessageVarg(LogMessageType messageType, const char* fmt, va_list argList) { @@ -62,9 +165,32 @@ void Log::LogMessageVarg(LogMessageType messageType, const char* fmt, va_list ar return; #endif - char buffer[MaxLogBufferMessageSize]; - FormatLog(buffer, MaxLogBufferMessageSize, messageType, fmt, argList); - DefaultLogOutput(buffer, IsDebugMessage(messageType)); + char buffer[MaxLogBufferMessageSize]; + char* pBuffer = buffer; + char* pAllocated = NULL; + + #if !defined(OVR_CC_MSVC) // Non-Microsoft compilers require you to save a copy of the va_list. + va_list argListSaved; + va_copy(argListSaved, argList); + #endif + + int result = FormatLog(pBuffer, MaxLogBufferMessageSize, messageType, fmt, argList); + + if(result >= MaxLogBufferMessageSize) // If there was insufficient capacity... + { + pAllocated = (char*)OVR_ALLOC(result + 1); // We assume C++ exceptions are disabled. FormatLog will handle the case that pAllocated is NULL. + pBuffer = pAllocated; + + #if !defined(OVR_CC_MSVC) + va_end(argList); // The caller owns argList and will call va_end on it. + va_copy(argList, argListSaved); + #endif + + FormatLog(pBuffer, (size_t)result + 1, messageType, fmt, argList); + } + + DefaultLogOutput(pBuffer, messageType, result); + delete[] pAllocated; } void OVR::Log::LogMessage(LogMessageType messageType, const char* pfmt, ...) @@ -76,35 +202,60 @@ void OVR::Log::LogMessage(LogMessageType messageType, const char* pfmt, ...) } -void Log::FormatLog(char* buffer, unsigned bufferSize, LogMessageType messageType, +// Return behavior is the same as ISO C vsnprintf: returns the required strlen of buffer (which will +// be >= bufferSize if bufferSize is insufficient) or returns a negative value because the input was bad. +int Log::FormatLog(char* buffer, size_t bufferSize, LogMessageType messageType, const char* fmt, va_list argList) -{ - bool addNewline = true; +{ + OVR_ASSERT(buffer && (bufferSize >= 10)); // Need to be able to at least print "Assert: \n" to it. + if(!buffer || (bufferSize < 10)) + return -1; + + int addNewline = 1; + int prefixLength = 0; switch(messageType) { - case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); break; - case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); break; - case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); break; - case Log_Text: buffer[0] = 0; addNewline = false; break; - case Log_DebugText: buffer[0] = 0; addNewline = false; break; - default: - buffer[0] = 0; - addNewline = false; - break; + case Log_Error: OVR_strcpy(buffer, bufferSize, "Error: "); prefixLength = 7; break; + case Log_Debug: OVR_strcpy(buffer, bufferSize, "Debug: "); prefixLength = 7; break; + case Log_Assert: OVR_strcpy(buffer, bufferSize, "Assert: "); prefixLength = 8; break; + case Log_Text: buffer[0] = 0; addNewline = 0; break; + case Log_DebugText: buffer[0] = 0; addNewline = 0; break; + default: buffer[0] = 0; addNewline = 0; break; } - UPInt prefixLength = OVR_strlen(buffer); - char *buffer2 = buffer + prefixLength; - OVR_vsprintf(buffer2, bufferSize - prefixLength, fmt, argList); + char* buffer2 = buffer + prefixLength; + size_t size2 = bufferSize - (size_t)prefixLength; + int messageLength = OVR_vsnprintf(buffer2, size2, fmt, argList); if (addNewline) - OVR_strcat(buffer, bufferSize, "\n"); -} + { + if (messageLength < 0) // If there was a format error... + { + // To consider: append <format error> to the buffer here. + buffer2[0] = '\n'; // We are guaranteed to have capacity for this. + buffer2[1] = '\0'; + } + else + { + // If the printed string used all of the capacity or required more than the capacity, + // Chop the output by one character so we can append the \n safely. + int messageEnd = (messageLength >= (int)(size2 - 1)) ? (int)(size2 - 2) : messageLength; + buffer2[messageEnd + 0] = '\n'; + buffer2[messageEnd + 1] = '\0'; + } + } + + if (messageLength >= 0) // If the format was OK... + return prefixLength + messageLength + addNewline; // Return the required strlen of buffer. + return messageLength; // Else we cannot know what the required strlen is and return the error to the caller. +} -void Log::DefaultLogOutput(const char* formattedText, bool debug) +void Log::DefaultLogOutput(const char* formattedText, LogMessageType messageType, int bufferSize) { + bool debug = IsDebugMessage(messageType); + OVR_UNUSED(bufferSize); #if defined(OVR_OS_WIN32) // Under Win32, output regular messages to console if it exists; debug window otherwise. @@ -116,12 +267,15 @@ void Log::DefaultLogOutput(const char* formattedText, bool debug) { ::OutputDebugStringA(formattedText); } - else - { - fputs(formattedText, stdout); - } + + fputs(formattedText, stdout); + +#elif defined(OVR_OS_MS) // Any other Microsoft OSs + + ::OutputDebugStringA(formattedText); #elif defined(OVR_OS_ANDROID) + // To do: use bufferSize to deal with the case that Android has a limited output length. __android_log_write(ANDROID_LOG_INFO, "OVR", formattedText); #else @@ -129,6 +283,24 @@ void Log::DefaultLogOutput(const char* formattedText, bool debug) #endif + if (messageType == Log_Error) + { +#if defined(OVR_OS_WIN32) + if (!ReportEventA(hEventSource, EVENTLOG_ERROR_TYPE, 0, 0, NULL, 1, 0, &formattedText, NULL)) + { + OVR_ASSERT(false); + } +#elif defined(OVR_OS_MS) // Any other Microsoft OSs + // TBD +#elif defined(OVR_OS_ANDROID) + // TBD +#elif defined(OVR_OS_MAC) || defined(OVR_OS_LINUX) + syslog(LOG_ERR, "%s", formattedText); +#else + // TBD +#endif + } + // Just in case. OVR_UNUSED2(formattedText, debug); } @@ -161,16 +333,37 @@ Log* Log::GetDefaultLog() //----------------------------------------------------------------------------------- // ***** Global Logging functions +#if !defined(OVR_CC_MSVC) +// The reason for va_copy is because you can't use va_start twice on Linux +#define OVR_LOG_FUNCTION_IMPL(Name) \ + void Log##Name(const char* fmt, ...) \ + { \ + if (OVR_GlobalLog) \ + { \ + va_list argList1; \ + va_start(argList1, fmt); \ + va_list argList2; \ + va_copy(argList2, argList1); \ + OVR_GlobalLog->LogMessageVargInt(Log_##Name, fmt, argList2); \ + va_end(argList2); \ + OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList1); \ + va_end(argList1); \ + } \ + } +#else #define OVR_LOG_FUNCTION_IMPL(Name) \ void Log##Name(const char* fmt, ...) \ { \ if (OVR_GlobalLog) \ { \ - va_list argList; va_start(argList, fmt); \ - OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList); \ - va_end(argList); \ + va_list argList1; \ + va_start(argList1, fmt); \ + OVR_GlobalLog->LogMessageVargInt(Log_##Name, fmt, argList1); \ + OVR_GlobalLog->LogMessageVarg(Log_##Name, fmt, argList1); \ + va_end(argList1); \ } \ } +#endif // #if !defined(OVR_OS_WIN32) OVR_LOG_FUNCTION_IMPL(Text) OVR_LOG_FUNCTION_IMPL(Error) @@ -181,4 +374,59 @@ OVR_LOG_FUNCTION_IMPL(Debug) OVR_LOG_FUNCTION_IMPL(Assert) #endif + + +// Assertion handler support +// To consider: Move this to an OVR_Types.cpp or OVR_Assert.cpp source file. + +static OVRAssertionHandler sOVRAssertionHandler = OVR::DefaultAssertionHandler; +static intptr_t sOVRAssertionHandlerUserParameter = 0; + +OVRAssertionHandler GetAssertionHandler(intptr_t* userParameter) +{ + if(userParameter) + *userParameter = sOVRAssertionHandlerUserParameter; + return sOVRAssertionHandler; +} + +void SetAssertionHandler(OVRAssertionHandler assertionHandler, intptr_t userParameter) +{ + sOVRAssertionHandler = assertionHandler; + sOVRAssertionHandlerUserParameter = userParameter; +} + +intptr_t DefaultAssertionHandler(intptr_t /*userParameter*/, const char* title, const char* message) +{ + if(OVRIsDebuggerPresent()) + { + OVR_DEBUG_BREAK; + } + else + { + #if defined(OVR_BUILD_DEBUG) + // Print a stack trace of all threads. + OVR::String s; + OVR::String threadListOutput; + static OVR::SymbolLookup symbolLookup; + + s = "Failure: "; + s += message; + + if(symbolLookup.Initialize() && symbolLookup.ReportThreadCallstack(threadListOutput, 4)) // This '4' is there to skip our internal handling and retrieve starting at the assertion location (our caller) only. + { + s += "\r\n\r\n"; + s += threadListOutput; + } + + OVR::Util::DisplayMessageBox(title, s.ToCStr()); + #else + OVR::Util::DisplayMessageBox(title, message); + #endif + } + + return 0; +} + + + } // OVR |