Moved parts of the logging code into the public API (so users can redirect logs).

This commit is contained in:
David Williams 2015-05-09 08:52:30 +02:00
parent 040dc37057
commit 65b0d1c3c5
3 changed files with 85 additions and 57 deletions

View File

@ -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/

View File

@ -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 <iostream>
#include <sstream>
/*
* 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 >

76
include/PolyVox/Logging.h Normal file
View File

@ -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 <iostream>
#include <string>
// 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__