Switching to a stream-based interface for logging.
This commit is contained in:
parent
db1c2bf0f8
commit
f3b0183013
@ -62,36 +62,19 @@ freely, subject to the following restrictions:
|
|||||||
* PolyVox provides basic logging facilities which can be redirected by your application.
|
* PolyVox provides basic logging facilities which can be redirected by your application.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define LOG_DECLARATION(name) \
|
||||||
|
std::ostream& log(name)(void); \
|
||||||
|
void set(name)Stream(std::ostream& nameStream);
|
||||||
|
|
||||||
namespace PolyVox
|
namespace PolyVox
|
||||||
{
|
{
|
||||||
class LogLevels
|
std::ostream& logError(void);
|
||||||
{
|
std::ostream& logFatal(void);
|
||||||
public:
|
|
||||||
enum LogLevel
|
|
||||||
{
|
|
||||||
Debug,
|
|
||||||
Info,
|
|
||||||
Warning,
|
|
||||||
Error,
|
|
||||||
Fatal
|
|
||||||
};
|
|
||||||
};
|
|
||||||
typedef LogLevels::LogLevel LogLevel;
|
|
||||||
|
|
||||||
typedef void (*LogHandler)(const std::string& message, LogLevel logLevel);
|
// These take pointers rather than references to emphasise that the
|
||||||
|
// user needs to keep the target alive as long as PolyVox is writing data.
|
||||||
LogHandler getLogHandler();
|
void setErrorStream(std::ostream* errorStream);
|
||||||
void setLogHandler(LogHandler newHandler);
|
void setFatalStream(std::ostream* fatalStream);
|
||||||
|
|
||||||
// The actual logging function
|
|
||||||
void log(const std::string& message, LogLevel logLevel);
|
|
||||||
|
|
||||||
// Some handy wrappers
|
|
||||||
void logDebug (const std::string& message);
|
|
||||||
void logInfo (const std::string& message);
|
|
||||||
void logWarning(const std::string& message);
|
|
||||||
void logError (const std::string& message);
|
|
||||||
void logFatal (const std::string& message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -114,14 +97,12 @@ namespace PolyVox
|
|||||||
{ \
|
{ \
|
||||||
if (!(condition)) \
|
if (!(condition)) \
|
||||||
{ \
|
{ \
|
||||||
std::stringstream ss; \
|
logFatal() << std::endl << std::endl; \
|
||||||
ss << std::endl << std::endl; \
|
logFatal() << " PolyVox Assertion Failed!" << std::endl; \
|
||||||
ss << " PolyVox Assertion Failed!" << std::endl; \
|
logFatal() << " =========================" << std::endl; \
|
||||||
ss << " =========================" << std::endl; \
|
logFatal() << " Condition: " << #condition << std::endl; \
|
||||||
ss << " Condition: " << #condition << std::endl; \
|
logFatal() << " Message: " << (message) << std::endl; \
|
||||||
ss << " Message: " << (message) << std::endl; \
|
logFatal() << " Location: " << "Line " << __LINE__ << " of " << __FILE__ << std::endl << std::endl; \
|
||||||
ss << " Location: " << "Line " << __LINE__ << " of " << __FILE__ << std::endl << std::endl; \
|
|
||||||
PolyVox::logFatal(ss.str()); \
|
|
||||||
POLYVOX_HALT(); \
|
POLYVOX_HALT(); \
|
||||||
} \
|
} \
|
||||||
} while(0) \
|
} while(0) \
|
||||||
@ -176,7 +157,7 @@ namespace PolyVox
|
|||||||
*/
|
*/
|
||||||
#ifdef POLYVOX_THROW_ENABLED
|
#ifdef POLYVOX_THROW_ENABLED
|
||||||
#define POLYVOX_THROW(type, message) \
|
#define POLYVOX_THROW(type, message) \
|
||||||
PolyVox::logError(message); \
|
logError() << (message); \
|
||||||
throw type((message))
|
throw type((message))
|
||||||
#else
|
#else
|
||||||
namespace PolyVox
|
namespace PolyVox
|
||||||
@ -188,7 +169,7 @@ namespace PolyVox
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define POLYVOX_THROW(type, message) \
|
#define POLYVOX_THROW(type, message) \
|
||||||
PolyVox::logError(message); \
|
logError() << (message); \
|
||||||
type except = (type)((message)); \
|
type except = (type)((message)); \
|
||||||
getThrowHandler()((except), __FILE__, __LINE__)
|
getThrowHandler()((except), __FILE__, __LINE__)
|
||||||
#endif
|
#endif
|
||||||
|
@ -25,115 +25,63 @@ freely, subject to the following restrictions:
|
|||||||
|
|
||||||
namespace PolyVox
|
namespace PolyVox
|
||||||
{
|
{
|
||||||
void defaultLogHandler(const std::string& message, LogLevel logLevel)
|
// Error stream for logging
|
||||||
|
std::ostream*& getErrorStreamInstance()
|
||||||
{
|
{
|
||||||
switch(logLevel)
|
static std::ostream* s_pErrorStream = &(std::cerr);
|
||||||
{
|
return s_pErrorStream;
|
||||||
case LogLevels::Debug:
|
|
||||||
{
|
|
||||||
// Debug messages are not output by this default log handler.
|
|
||||||
// Provide a custom handler if you want to process them.
|
|
||||||
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()
|
void setErrorStream(std::ostream* errorStream)
|
||||||
{
|
{
|
||||||
static LogHandler s_fLogHandler = &defaultLogHandler;
|
getErrorStreamInstance() = errorStream;
|
||||||
return s_fLogHandler;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LogHandler getLogHandler()
|
std::ostream& logError(void)
|
||||||
{
|
{
|
||||||
return getLogHandlerInstance();
|
return *(getErrorStreamInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLogHandler(LogHandler fNewHandler)
|
// Fatal stream for logging
|
||||||
|
std::ostream*& getFatalStreamInstance()
|
||||||
{
|
{
|
||||||
getLogHandlerInstance() = fNewHandler;
|
static std::ostream* s_pFatalStream = &(std::cerr);
|
||||||
|
return s_pFatalStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
void log(const std::string& message, LogLevel logLevel)
|
void setFatalStream(std::ostream* fatalStream)
|
||||||
{
|
{
|
||||||
LogHandler logHandler = getLogHandler();
|
getFatalStreamInstance() = fatalStream;
|
||||||
if(logHandler)
|
|
||||||
{
|
|
||||||
logHandler(message, logLevel);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some handy wrappers
|
std::ostream& logFatal(void)
|
||||||
void logDebug(const std::string& message)
|
|
||||||
{
|
|
||||||
log(message, LogLevels::Debug );
|
|
||||||
}
|
|
||||||
|
|
||||||
void logInfo(const std::string& message)
|
|
||||||
{
|
{
|
||||||
log(message, LogLevels::Info);
|
return *(getFatalStreamInstance());
|
||||||
}
|
|
||||||
|
|
||||||
void logWarning(const std::string& message)
|
|
||||||
{
|
|
||||||
log(message, LogLevels::Warning);
|
|
||||||
}
|
|
||||||
|
|
||||||
void logError(const std::string& message)
|
|
||||||
{
|
|
||||||
log(message, LogLevels::Error);
|
|
||||||
}
|
|
||||||
|
|
||||||
void logFatal(const std::string& message)
|
|
||||||
{
|
|
||||||
log(message, LogLevels::Fatal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef POLYVOX_THROW_ENABLED
|
#ifndef POLYVOX_THROW_ENABLED
|
||||||
void defaultThrowHandler(std::exception& e, const char* file, int line)
|
void defaultThrowHandler(std::exception& e, const char* file, int line)
|
||||||
{
|
{
|
||||||
std::stringstream ss; \
|
logFatal() << std::endl << std::endl; \
|
||||||
ss << std::endl << std::endl; \
|
logFatal() << " PolyVox exception thrown!" << std::endl; \
|
||||||
ss << " PolyVox exception thrown!" << std::endl; \
|
logFatal() << " =========================" << std::endl; \
|
||||||
ss << " =========================" << std::endl; \
|
logFatal() << " PolyVox has tried to throw an exception but it was built without support" << std::endl; \
|
||||||
ss << " PolyVox has tried to throw an exception but it was built without support" << std::endl; \
|
logFatal() << " for exceptions. In this scenario PolyVox will call a 'throw handler'" << std::endl; \
|
||||||
ss << " for exceptions. In this scenario PolyVox will call a 'throw handler'" << std::endl; \
|
logFatal() << " and this message is being printed by the default throw handler." << std::endl << std::endl; \
|
||||||
ss << " and this message is being printed by the default throw handler." << std::endl << std::endl; \
|
|
||||||
|
|
||||||
ss << " If you don't want to enable exceptions then you should try to determine why" << std::endl; \
|
logFatal() << " If you don't want to enable exceptions then you should try to determine why" << std::endl; \
|
||||||
ss << " this exception was thrown and make sure it doesn't happen again. If it was" << std::endl; \
|
logFatal() << " this exception was thrown and make sure it doesn't happen again. If it was" << std::endl; \
|
||||||
ss << " due to something like an invalid argument to a function then you should be" << std::endl; \
|
logFatal() << " due to something like an invalid argument to a function then you should be" << std::endl; \
|
||||||
ss << " able to fix it quite easily by validating parameters as appropriate. More" << std::endl; \
|
logFatal() << " able to fix it quite easily by validating parameters as appropriate. More" << std::endl; \
|
||||||
ss << " complex exception scenarios (out of memory, etc) might be harder to fix and" << std::endl; \
|
logFatal() << " complex exception scenarios (out of memory, etc) might be harder to fix and" << std::endl; \
|
||||||
ss << " you should replace this default handler with something which is more" << std::endl; \
|
logFatal() << " you should replace this default handler with something which is more" << std::endl; \
|
||||||
ss << " meaningful to your users." << std::endl << std::endl; \
|
logFatal() << " meaningful to your users." << std::endl << std::endl; \
|
||||||
|
|
||||||
ss << " Exception details" << std::endl; \
|
logFatal() << " Exception details" << std::endl; \
|
||||||
ss << " -----------------" << std::endl; \
|
logFatal() << " -----------------" << std::endl; \
|
||||||
ss << " Line: " << line << std::endl; \
|
logFatal() << " Line: " << line << std::endl; \
|
||||||
ss << " File: " << file << std::endl; \
|
logFatal() << " File: " << file << std::endl; \
|
||||||
ss << " Message: " << e.what() << std::endl << std::endl; \
|
logFatal() << " Message: " << e.what() << std::endl << std::endl; \
|
||||||
PolyVox::logFatal(ss.str()); \
|
|
||||||
|
|
||||||
POLYVOX_HALT(); \
|
POLYVOX_HALT(); \
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user