From 1010052ea6af27b616d2efdf0b1f779d477fa6a2 Mon Sep 17 00:00:00 2001 From: Daviw Williams Date: Tue, 14 May 2013 16:52:16 +0200 Subject: [PATCH] Reworking the logging system. --- examples/OpenGL/main.cpp | 25 ---- library/PolyVoxCore/CMakeLists.txt | 2 - .../include/PolyVoxCore/BaseVolume.h | 1 - .../include/PolyVoxCore/Impl/Config.h | 1 + .../include/PolyVoxCore/Impl/ErrorHandling.h | 36 ++++- .../include/PolyVoxCore/LargeVolume.h | 1 - library/PolyVoxCore/include/PolyVoxCore/Log.h | 63 -------- .../include/PolyVoxCore/RawVolume.h | 1 - .../include/PolyVoxCore/SimpleVolume.h | 1 - .../PolyVoxCore/source/Impl/ErrorHandling.cpp | 139 ++++++++++++------ library/PolyVoxCore/source/Log.cpp | 29 ---- 11 files changed, 134 insertions(+), 165 deletions(-) delete mode 100644 library/PolyVoxCore/include/PolyVoxCore/Log.h delete mode 100644 library/PolyVoxCore/source/Log.cpp diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index ca348bb5..5ed191f5 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -21,7 +21,6 @@ freely, subject to the following restrictions: distribution. *******************************************************************************/ -#include "PolyVoxCore/Log.h" #include "PolyVoxCore/MaterialDensityPair.h" #include "PolyVoxCore/LargeVolume.h" #include "PolyVoxCore/LowPassFilter.h" @@ -47,32 +46,8 @@ using namespace std; using namespace PolyVox; using namespace std; -void exampleLog(string message, int severity) -{ - //Identify how severe the mesage is - switch(severity) - { - case LS_DEBUG: - cout << "DEBUG: "; - break; - case LS_INFO: - cout << "INFO: "; - break; - case LS_WARN: - cout << "WARN: "; - break; - case LS_ERROR: - cout << "ERROR: "; - break; - } - - //Print the message - cout << message << endl; -} - int main(int argc, char *argv[]) { - logHandler = &exampleLog; LargeVolume volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(g_uVolumeSideLength-1, g_uVolumeSideLength-1, g_uVolumeSideLength-1))); //Make our volume contain a sphere in the center. diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index b9cdf8c4..2c352a50 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -32,7 +32,6 @@ SET(CORE_SRC_FILES source/ArraySizes.cpp source/AStarPathfinder.cpp source/MinizCompressor.cpp - source/Log.cpp source/Region.cpp source/VertexTypes.cpp ) @@ -66,7 +65,6 @@ SET(CORE_INC_FILES include/PolyVoxCore/LargeVolume.h include/PolyVoxCore/LargeVolume.inl include/PolyVoxCore/LargeVolumeSampler.inl - include/PolyVoxCore/Log.h include/PolyVoxCore/LowPassFilter.h include/PolyVoxCore/LowPassFilter.inl include/PolyVoxCore/MarchingCubesSurfaceExtractor.h diff --git a/library/PolyVoxCore/include/PolyVoxCore/BaseVolume.h b/library/PolyVoxCore/include/PolyVoxCore/BaseVolume.h index f0e3f4ad..04d70756 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/BaseVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/BaseVolume.h @@ -24,7 +24,6 @@ freely, subject to the following restrictions: #ifndef __PolyVox_BaseVolume_H__ #define __PolyVox_BaseVolume_H__ -#include "PolyVoxCore/Log.h" #include "PolyVoxCore/Region.h" #include "PolyVoxCore/Vector.h" diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Config.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Config.h index e97e27af..eb27c6a2 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Config.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Config.h @@ -25,6 +25,7 @@ freely, subject to the following restrictions: #define __PolyVox_Config_H__ #define POLYVOX_ASSERTS_ENABLED +#define POLYVOX_LOGGING_ENABLED #define POLYVOX_THROW_ENABLED #endif diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h index 0fb01f31..3e80d6b7 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h @@ -55,6 +55,37 @@ freely, subject to the following restrictions: #define POLYVOX_UNUSED(x) do { (void)sizeof(x); } while(0) +/* + * Logging + * -------- + * PolyVox provides basic logging facilities which can be redirected by your application. + */ + +namespace PolyVox +{ + class LogLevels + { + public: + enum LogLevel + { + Debug, + Info, + Warning, + Error, + Fatal + }; + }; + typedef LogLevels::LogLevel LogLevel; + + typedef void (*LogHandler)(const std::string& message, LogLevel logLevel); + + LogHandler getLogHandler(); + void setLogHandler(LogHandler newHandler); + + //The actual logging function + void log(const std::string& message, LogLevel logLevel); +} + /* * Assertions * ---------- @@ -134,7 +165,9 @@ freely, subject to the following restrictions: * ... */ #ifdef POLYVOX_THROW_ENABLED - #define POLYVOX_THROW(type, message) throw type((message)) + #define POLYVOX_THROW(type, message) \ + log(message, LogLevels::Error); \ + throw type((message)) #else namespace PolyVox { @@ -145,6 +178,7 @@ freely, subject to the following restrictions: } #define POLYVOX_THROW(type, message) \ + log(message, LogLevels::Error); \ type except = (type)((message)); \ getThrowHandler()((except), __FILE__, __LINE__) #endif diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h index adbe3d58..2be3f87d 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h @@ -27,7 +27,6 @@ freely, subject to the following restrictions: #include "PolyVoxCore/BaseVolume.h" #include "Impl/Block.h" #include "PolyVoxCore/Compressor.h" -#include "PolyVoxCore/Log.h" #include "PolyVoxCore/Region.h" #include "PolyVoxCore/Vector.h" diff --git a/library/PolyVoxCore/include/PolyVoxCore/Log.h b/library/PolyVoxCore/include/PolyVoxCore/Log.h deleted file mode 100644 index 9aeb46d8..00000000 --- a/library/PolyVoxCore/include/PolyVoxCore/Log.h +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#ifndef __PolyVox_Log_H__ -#define __PolyVox_Log_H__ - -#include "Impl/TypeDef.h" - -#include - -//Note: The functions in this file are not for the user to call - they are -//intended for internal use only. The only exception is that you may set the -//logHandler pointer to point at your own handling funtion for printing, etc. - -namespace PolyVox -{ - //////////////////////////////////////////////////////////////////////////////// - /// Log levels for filtering logging events - //////////////////////////////////////////////////////////////////////////////// - enum LogSeverity - { - LS_DEBUG, ///< Only displayed if it is a debug build - LS_INFO, - LS_WARN, - LS_ERROR - }; - - POLYVOX_API extern void (*logHandler)(std::string, int severity); -} - -//Debug severity messages are only used if we are a debug build -#ifdef _DEBUG - #define POLYVOX_LOG_DEBUG(message) if(logHandler){logHandler(message, LS_DEBUG);} -#else - #define POLYVOX_LOG_DEBUG(message) -#endif - -//Other severity levels work in both debug and release -#define POLYVOX_LOG_INFO(message) if(logHandler){logHandler(message, LS_INFO);} -#define POLYVOX_LOG_WARN(message) if(logHandler){logHandler(message, LS_WARN);} -#define POLYVOX_LOG_ERROR(message) if(logHandler){logHandler(message, LS_ERROR);} - -#endif diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h index ed213fe2..e4549e1f 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h @@ -25,7 +25,6 @@ freely, subject to the following restrictions: #define __PolyVox_RawVolume_H__ #include "PolyVoxCore/BaseVolume.h" -#include "PolyVoxCore/Log.h" #include "PolyVoxCore/Region.h" #include "PolyVoxCore/Vector.h" diff --git a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h index 66f24cc3..8ce2083e 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h @@ -27,7 +27,6 @@ freely, subject to the following restrictions: #include "Impl/Utility.h" #include "PolyVoxCore/BaseVolume.h" -#include "PolyVoxCore/Log.h" #include "PolyVoxCore/Region.h" #include "PolyVoxCore/Vector.h" diff --git a/library/PolyVoxCore/source/Impl/ErrorHandling.cpp b/library/PolyVoxCore/source/Impl/ErrorHandling.cpp index bc7dc2c5..06472c1e 100644 --- a/library/PolyVoxCore/source/Impl/ErrorHandling.cpp +++ b/library/PolyVoxCore/source/Impl/ErrorHandling.cpp @@ -23,49 +23,106 @@ freely, subject to the following restrictions: #include "PolyVoxCore/Impl/ErrorHandling.h" -#ifndef POLYVOX_THROW_ENABLED - namespace PolyVox +namespace PolyVox +{ + void defaultLogHandler(const std::string& message, LogLevel logLevel) { - void defaultThrowHandler(std::exception& e, const char* file, int line) + switch(logLevel) { - std::cerr << std::endl << std::endl; \ - std::cerr << " PolyVox exception thrown!" << std::endl; \ - std::cerr << " =========================" << std::endl; \ - std::cerr << " PolyVox has tried to throw an exception but it was built without support" << std::endl; \ - std::cerr << " for exceptions. In this scenario PolyVox will call a 'throw handler'" << std::endl; \ - std::cerr << " and this message is being printed by the default throw handler." << std::endl << std::endl; \ - - std::cerr << " If you don't want to enable exceptions then you should try to determine why" << std::endl; \ - std::cerr << " this exception was thrown and make sure it doesn't happen again. If it was" << std::endl; \ - std::cerr << " due to something like an invalid argument to a function then you should be" << std::endl; \ - std::cerr << " able to fix it quite easily by validating parameters as appropriate. More" << std::endl; \ - std::cerr << " complex exception scenarios (out of memory, etc) might be harder to fix and" << std::endl; \ - std::cerr << " you should replace this default handler with something which is more" << std::endl; \ - std::cerr << " meaningful to your users." << std::endl << std::endl; \ - - std::cerr << " Exception details" << std::endl; \ - std::cerr << " -----------------" << std::endl; \ - std::cerr << " Line: " << line << std::endl; \ - std::cerr << " File: " << file << std::endl; \ - std::cerr << " Message: " << e.what() << std::endl << std::endl; \ - - POLYVOX_HALT(); \ - } - - ThrowHandler& getThrowHandlerInstance() - { - static ThrowHandler s_fThrowHandler = &defaultThrowHandler; - return s_fThrowHandler; - } - - ThrowHandler getThrowHandler() - { - return getThrowHandlerInstance(); - } - - void setThrowHandler(ThrowHandler fNewHandler) - { - getThrowHandlerInstance() = fNewHandler; + case LogLevels::Debug: + { + std::cout << "Debug: " << message.c_str() << std::endl; + break; + } + case LogLevels::Info: + { + std::cout << "Info: " << message.c_str() << std::endl; + break; + } + case LogLevels::Warning: + { + std::cerr << "Warning: " << message.c_str() << std::endl; + break; + } + case LogLevels::Error: + { + std::cerr << "Error: " << message.c_str() << std::endl; + break; + } + case LogLevels::Fatal: + { + std::cerr << "Fatal: " << message.c_str() << std::endl; + break; + } } } + + LogHandler& getLogHandlerInstance() + { + static LogHandler s_fLogHandler = &defaultLogHandler; + return s_fLogHandler; + } + + LogHandler getLogHandler() + { + return getLogHandlerInstance(); + } + + void setLogHandler(LogHandler fNewHandler) + { + getLogHandlerInstance() = fNewHandler; + } + + void log(const std::string& message, LogLevel logLevel) + { + LogHandler logHandler = getLogHandler(); + if(logHandler) + { + logHandler(message, logLevel); + } + } + +#ifndef POLYVOX_THROW_ENABLED + void defaultThrowHandler(std::exception& e, const char* file, int line) + { + std::cerr << std::endl << std::endl; \ + std::cerr << " PolyVox exception thrown!" << std::endl; \ + std::cerr << " =========================" << std::endl; \ + std::cerr << " PolyVox has tried to throw an exception but it was built without support" << std::endl; \ + std::cerr << " for exceptions. In this scenario PolyVox will call a 'throw handler'" << std::endl; \ + std::cerr << " and this message is being printed by the default throw handler." << std::endl << std::endl; \ + + std::cerr << " If you don't want to enable exceptions then you should try to determine why" << std::endl; \ + std::cerr << " this exception was thrown and make sure it doesn't happen again. If it was" << std::endl; \ + std::cerr << " due to something like an invalid argument to a function then you should be" << std::endl; \ + std::cerr << " able to fix it quite easily by validating parameters as appropriate. More" << std::endl; \ + std::cerr << " complex exception scenarios (out of memory, etc) might be harder to fix and" << std::endl; \ + std::cerr << " you should replace this default handler with something which is more" << std::endl; \ + std::cerr << " meaningful to your users." << std::endl << std::endl; \ + + std::cerr << " Exception details" << std::endl; \ + std::cerr << " -----------------" << std::endl; \ + std::cerr << " Line: " << line << std::endl; \ + std::cerr << " File: " << file << std::endl; \ + std::cerr << " Message: " << e.what() << std::endl << std::endl; \ + + POLYVOX_HALT(); \ + } + + ThrowHandler& getThrowHandlerInstance() + { + static ThrowHandler s_fThrowHandler = &defaultThrowHandler; + return s_fThrowHandler; + } + + ThrowHandler getThrowHandler() + { + return getThrowHandlerInstance(); + } + + void setThrowHandler(ThrowHandler fNewHandler) + { + getThrowHandlerInstance() = fNewHandler; + } #endif +} diff --git a/library/PolyVoxCore/source/Log.cpp b/library/PolyVoxCore/source/Log.cpp deleted file mode 100644 index 929fd951..00000000 --- a/library/PolyVoxCore/source/Log.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#include "PolyVoxCore/Log.h" - -namespace PolyVox -{ - void (*logHandler)(std::string, int severity) = 0; -}