From 65b0d1c3c5dd7c0f47bd2d38ec9bd8a8fcf0d3e5 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 9 May 2015 08:52:30 +0200 Subject: [PATCH] Moved parts of the logging code into the public API (so users can redirect logs). --- include/PolyVox/Impl/Assertions.h | 1 + include/PolyVox/Impl/LoggingImpl.h | 65 ++++--------------------- include/PolyVox/Logging.h | 76 ++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 57 deletions(-) create mode 100644 include/PolyVox/Logging.h diff --git a/include/PolyVox/Impl/Assertions.h b/include/PolyVox/Impl/Assertions.h index be2b7ee5..cf20efad 100644 --- a/include/PolyVox/Impl/Assertions.h +++ b/include/PolyVox/Impl/Assertions.h @@ -26,6 +26,7 @@ distribution. #include "PolyVox/Impl/Config.h" #include "PolyVox/Impl/LoggingImpl.h" // Asserts can log when they fire. +#include "PolyVox/Impl/PlatformDefinitions.h" // The code below implements a custom assert function called POLYVOX_ASSERT which has a number of advantages compared // to the standard C/C++ assert(). It is inspired by http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ diff --git a/include/PolyVox/Impl/LoggingImpl.h b/include/PolyVox/Impl/LoggingImpl.h index 13690ee2..3a6db1f3 100644 --- a/include/PolyVox/Impl/LoggingImpl.h +++ b/include/PolyVox/Impl/LoggingImpl.h @@ -24,64 +24,15 @@ freely, subject to the following restrictions: #ifndef __PolyVox_LoggingImpl_H__ #define __PolyVox_LoggingImpl_H__ +#include "PolyVox/Logging.h" + #include "PolyVox/Impl/Config.h" -#include #include -/* - * Logging - * -------- - * PolyVox provides basic logging facilities which can be redirected by your application. - */ namespace PolyVox { - class Logger - { - public: - // Passing zero to the null stream constructor guarentees it will discard all input. See - // here http://stackoverflow.com/a/8244052 and here http://stackoverflow.com/a/6240980 - Logger() {}; - virtual ~Logger() {}; - - virtual void logTraceMessage(const std::string& message) = 0; - virtual void logDebugMessage(const std::string& message) = 0; - virtual void logInfoMessage(const std::string& message) = 0; - virtual void logWarningMessage(const std::string& message) = 0; - virtual void logErrorMessage(const std::string& message) = 0; - virtual void logFatalMessage(const std::string& message) = 0; - }; - - class DefaultLogger : public Logger - { - public: - DefaultLogger() : Logger() {} - virtual ~DefaultLogger() {} - - // Appending the 'std::endl' forces the stream to be flushed. - void logTraceMessage(const std::string& /*message*/) { } - void logDebugMessage(const std::string& /*message*/) { } - void logInfoMessage(const std::string& message) { std::cout << message << std::endl; } - void logWarningMessage(const std::string& message) { std::cerr << message << std::endl; } - void logErrorMessage(const std::string& message) { std::cerr << message << std::endl; } - void logFatalMessage(const std::string& message) { std::cerr << message << std::endl; } - }; - - namespace Impl - { - inline Logger*& getLoggerInstance() - { - static Logger* s_pLogger = new DefaultLogger; - return s_pLogger; - } - } - - inline void setLogger(Logger* pLogger) - { - Impl::getLoggerInstance() = pLogger; - } - namespace Impl { // Used for building the log messages - convert a list of variables into a string. @@ -100,7 +51,7 @@ namespace PolyVox void logTraceMessage(Args const& ... messageArgs) { std::string message = argListToString(messageArgs...); - PolyVox::Impl::getLoggerInstance()->logTraceMessage(message); + getLoggerInstance()->logTraceMessage(message); } template< typename ... Args > @@ -114,7 +65,7 @@ namespace PolyVox void logDebugMessage(Args const& ... messageArgs) { std::string message = argListToString(messageArgs...); - PolyVox::Impl::getLoggerInstance()->logDebugMessage(message); + getLoggerInstance()->logDebugMessage(message); } template< typename ... Args > @@ -128,7 +79,7 @@ namespace PolyVox void logInfoMessage(Args const& ... messageArgs) { std::string message = argListToString(messageArgs...); - PolyVox::Impl::getLoggerInstance()->logInfoMessage(message); + getLoggerInstance()->logInfoMessage(message); } template< typename ... Args > @@ -142,7 +93,7 @@ namespace PolyVox void logWarningMessage(Args const& ... messageArgs) { std::string message = argListToString(messageArgs...); - PolyVox::Impl::getLoggerInstance()->logWarningMessage(message); + getLoggerInstance()->logWarningMessage(message); } template< typename ... Args > @@ -156,7 +107,7 @@ namespace PolyVox void logErrorMessage(Args const& ... messageArgs) { std::string message = argListToString(messageArgs...); - PolyVox::Impl::getLoggerInstance()->logErrorMessage(message); + getLoggerInstance()->logErrorMessage(message); } template< typename ... Args > @@ -170,7 +121,7 @@ namespace PolyVox void logFatalMessage(Args const& ... messageArgs) { std::string message = argListToString(messageArgs...); - PolyVox::Impl::getLoggerInstance()->logFatalMessage(message); + getLoggerInstance()->logFatalMessage(message); } template< typename ... Args > diff --git a/include/PolyVox/Logging.h b/include/PolyVox/Logging.h new file mode 100644 index 00000000..2e72e443 --- /dev/null +++ b/include/PolyVox/Logging.h @@ -0,0 +1,76 @@ +/******************************************************************************* +Copyright (c) 2005-2009 David Williams and Matthew 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_Logging_H__ +#define __PolyVox_Logging_H__ + +#include +#include + +// We expose the logger class to the user so that they can provide custom implementations +// to redirect PolyVox's log messages. However, it is not expected that user code will make +// use of PolyVox's logging macros s these are part of the private implementation. +namespace PolyVox +{ + class Logger + { + public: + Logger() {}; + virtual ~Logger() {}; + + virtual void logTraceMessage(const std::string& message) = 0; + virtual void logDebugMessage(const std::string& message) = 0; + virtual void logInfoMessage(const std::string& message) = 0; + virtual void logWarningMessage(const std::string& message) = 0; + virtual void logErrorMessage(const std::string& message) = 0; + virtual void logFatalMessage(const std::string& message) = 0; + }; + + class DefaultLogger : public Logger + { + public: + DefaultLogger() : Logger() {} + virtual ~DefaultLogger() {} + + // Appending the 'std::endl' forces the stream to be flushed. + void logTraceMessage(const std::string& /*message*/) { } + void logDebugMessage(const std::string& /*message*/) { } + void logInfoMessage(const std::string& message) { std::cout << message << std::endl; } + void logWarningMessage(const std::string& message) { std::cerr << message << std::endl; } + void logErrorMessage(const std::string& message) { std::cerr << message << std::endl; } + void logFatalMessage(const std::string& message) { std::cerr << message << std::endl; } + }; + + inline Logger*& getLoggerInstance() + { + static Logger* s_pLogger = new DefaultLogger; + return s_pLogger; + } + + inline void setLoggerInstance(Logger* pLogger) + { + getLoggerInstance() = pLogger; + } +} + +#endif //__PolyVox_Logging_H__