1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

- Added stream-like logging - Schema refactoring

This commit is contained in:
beegee1 2013-04-06 17:45:02 +00:00
parent fc41d24164
commit de01ea54b0
3 changed files with 92 additions and 13 deletions

View File

@ -4,6 +4,12 @@
"type" : "object",
"$schema": "http://json-schema.org/draft-04/schema",
"required" : [ "general", "video", "adventure", "battle", "server", "logging" ],
"definitions" : {
"logLevelEnum" : {
"type" : "string",
"enum" : [ "trace", "debug", "info", "warn", "error" ]
}
},
"properties":
{
"general" : {
@ -146,9 +152,8 @@
"default" : "%l %n [%t] - %m"
},
"threshold" : {
"type" : "string",
"$ref" : "#/definitions/logLevelEnum",
"default" : "info",
"enum" : [ "trace", "debug", "info", "warn", "error" ]
},
"coloredOutputEnabled" : {
"type" : "boolean",
@ -168,7 +173,7 @@
"required" : [ "domain", "level", "color" ],
"properties" : {
"domain" : { "type" : "string" },
"level" : { "type" : "string", "enum" : [ "trace", "debug", "info", "warn", "error" ] },
"level" : { "$ref" : "#/definitions/logLevelEnum" },
"color" : { "type" : "string", "enum" : [ "default", "green", "red", "magenta", "yellow", "white", "gray", "teal" ] }
}
}
@ -191,8 +196,7 @@
"required" : [ "level" ],
"properties" : {
"level" : {
"type" : "string",
"enum" : [ "trace", "debug", "info", "warn", "error" ]
"$ref" : "#/definitions/logLevelEnum"
}
}

View File

@ -84,26 +84,51 @@ void CGLogger::trace(const std::string & message) const
log(ELogLevel::TRACE, message);
}
CLoggerStream CGLogger::traceStream() const
{
return CLoggerStream(*this, ELogLevel::TRACE);
}
void CGLogger::debug(const std::string & message) const
{
log(ELogLevel::DEBUG, message);
}
CLoggerStream CGLogger::debugStream() const
{
return CLoggerStream(*this, ELogLevel::DEBUG);
}
void CGLogger::info(const std::string & message) const
{
log(ELogLevel::INFO, message);
}
CLoggerStream CGLogger::infoStream() const
{
return CLoggerStream(*this, ELogLevel::INFO);
}
void CGLogger::warn(const std::string & message) const
{
log(ELogLevel::WARN, message);
}
CLoggerStream CGLogger::warnStream() const
{
return CLoggerStream(*this, ELogLevel::WARN);
}
void CGLogger::error(const std::string & message) const
{
log(ELogLevel::ERROR, message);
}
CLoggerStream CGLogger::errorStream() const
{
return CLoggerStream(*this, ELogLevel::ERROR);
}
void CGLogger::log(ELogLevel::ELogLevel level, const std::string & message) const
{
if(getEffectiveLevel() <= level)
@ -163,3 +188,18 @@ void CGLogger::callTargets(const LogRecord & record) const
}
}
}
CLoggerStream::CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level) : logger(logger), level(level), sbuffer(nullptr)
{
}
CLoggerStream::~CLoggerStream()
{
if(sbuffer)
{
logger.log(level, sbuffer->str());
delete sbuffer;
sbuffer = nullptr;
}
}

View File

@ -73,6 +73,31 @@ private:
std::string name;
};
class CGLogger;
/**
* The logger stream provides a stream-like way of logging messages.
*/
class DLL_LINKAGE CLoggerStream
{
public:
CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level);
~CLoggerStream();
template<typename T>
CLoggerStream & operator<<(const T & data)
{
if(!sbuffer) sbuffer = new std::stringstream();
(*sbuffer) << data;
return *this;
}
private:
const CGLogger & logger;
ELogLevel::ELogLevel level;
std::stringstream * sbuffer;
};
/**
* The logger is used to log messages to certain targets of a specific domain/name. Temporary name is
* CGLogger, should be renamed to CLogger after refactoring.
@ -102,6 +127,8 @@ public:
*/
void trace(const std::string & message) const;
CLoggerStream traceStream() const;
/**
* Logs a message with the debug level.
*
@ -109,6 +136,8 @@ public:
*/
void debug(const std::string & message) const;
CLoggerStream debugStream() const;
/**
* Logs a message with the info level.
*
@ -116,6 +145,8 @@ public:
*/
void info(const std::string & message) const;
CLoggerStream infoStream() const;
/**
* Logs a message with the warn level.
*
@ -123,6 +154,8 @@ public:
*/
void warn(const std::string & message) const;
CLoggerStream warnStream() const;
/**
* Logs a message with the error level.
*
@ -130,6 +163,16 @@ public:
*/
void error(const std::string & message) const;
CLoggerStream errorStream() const;
/**
* Logs a message of the given log level.
*
* @param level The log level of the message to log.
* @param message The message to log.
*/
inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
/**
* Gets the log level applied for this logger. The default level for the root logger is INFO.
*
@ -179,14 +222,6 @@ private:
*/
CGLogger * getParent() const;
/**
* Logs a message of the given log level.
*
* @param level The log level of the message to log.
* @param message The message to log.
*/
inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
/**
* Gets the effective log level.
*