2013-04-05 13:29:46 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CBasicLogConfigurator.h"
|
|
|
|
|
|
|
|
#include "../CConfigHandler.h"
|
|
|
|
|
2013-04-13 14:07:32 +03:00
|
|
|
CBasicLogConfigurator::CBasicLogConfigurator(const std::string & filePath, CConsoleHandler * console) : filePath(filePath),
|
2013-04-14 22:24:31 +03:00
|
|
|
console(console), appendToLogFile(false)
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-10 20:18:01 +03:00
|
|
|
void CBasicLogConfigurator::configureDefault()
|
2013-04-09 17:31:36 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
CLogger::getGlobalLogger()->addTarget(make_unique<CLogConsoleTarget>(console));
|
|
|
|
CLogger::getGlobalLogger()->addTarget(make_unique<CLogFileTarget>(filePath, appendToLogFile));
|
|
|
|
appendToLogFile = true;
|
2013-04-09 17:31:36 +03:00
|
|
|
}
|
|
|
|
|
2013-04-10 20:18:01 +03:00
|
|
|
void CBasicLogConfigurator::configure()
|
2013-04-09 17:31:36 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const JsonNode & loggingNode = settings["logging"];
|
|
|
|
if(loggingNode.isNull()) throw std::runtime_error("Settings haven't been loaded.");
|
2013-04-10 20:18:01 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
// Configure loggers
|
|
|
|
const JsonNode & loggers = loggingNode["loggers"];
|
|
|
|
if(!loggers.isNull())
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(auto & loggerNode, loggers.Vector())
|
|
|
|
{
|
|
|
|
// Get logger
|
|
|
|
std::string name = loggerNode["domain"].String();
|
|
|
|
CLogger * logger = CLogger::getLogger(CLoggerDomain(name));
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
// Set log level
|
|
|
|
logger->setLevel(getLogLevel(loggerNode["level"].String()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CLogger::getGlobalLogger()->clearTargets();
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
// Add console target
|
|
|
|
auto consoleTarget = make_unique<CLogConsoleTarget>(console);
|
|
|
|
const JsonNode & consoleNode = loggingNode["console"];
|
|
|
|
if(!consoleNode.isNull())
|
|
|
|
{
|
|
|
|
const JsonNode & consoleFormatNode = consoleNode["format"];
|
|
|
|
if(!consoleFormatNode.isNull()) consoleTarget->setFormatter(CLogFormatter(consoleFormatNode.String()));
|
|
|
|
const JsonNode & consoleThresholdNode = consoleNode["threshold"];
|
|
|
|
if(!consoleThresholdNode.isNull()) consoleTarget->setThreshold(getLogLevel(consoleThresholdNode.String()));
|
|
|
|
const JsonNode & coloredConsoleEnabledNode = consoleNode["coloredOutputEnabled"];
|
|
|
|
consoleTarget->setColoredOutputEnabled(coloredConsoleEnabledNode.Bool());
|
2013-04-10 20:18:01 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
CColorMapping colorMapping;
|
|
|
|
const JsonNode & colorMappingNode = consoleNode["colorMapping"];
|
|
|
|
if(!colorMappingNode.isNull())
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const JsonNode & mappingNode, colorMappingNode.Vector())
|
|
|
|
{
|
|
|
|
std::string domain = mappingNode["domain"].String();
|
|
|
|
std::string level = mappingNode["level"].String();
|
|
|
|
std::string color = mappingNode["color"].String();
|
|
|
|
colorMapping.setColorFor(CLoggerDomain(domain), getLogLevel(level), getConsoleColor(color));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consoleTarget->setColorMapping(colorMapping);
|
|
|
|
}
|
|
|
|
CLogger::getGlobalLogger()->addTarget(std::move(consoleTarget));
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
// Add file target
|
|
|
|
auto fileTarget = make_unique<CLogFileTarget>(filePath, appendToLogFile);
|
|
|
|
const JsonNode & fileNode = loggingNode["file"];
|
|
|
|
if(!fileNode.isNull())
|
|
|
|
{
|
|
|
|
const JsonNode & fileFormatNode = fileNode["format"];
|
|
|
|
if(!fileFormatNode.isNull()) fileTarget->setFormatter(CLogFormatter(fileFormatNode.String()));
|
|
|
|
}
|
|
|
|
CLogger::getGlobalLogger()->addTarget(std::move(fileTarget));
|
|
|
|
appendToLogFile = true;
|
|
|
|
}
|
|
|
|
catch(const std::exception & e)
|
|
|
|
{
|
|
|
|
logGlobal->errorStream() << "Could not initialize the logging system due to configuration error/s."
|
|
|
|
<< "The logging system can be in a corrupted state. " << e.what();
|
|
|
|
}
|
2013-04-10 20:18:01 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
logGlobal->infoStream() << "Initialized logging system based on settings successfully.";
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ELogLevel::ELogLevel CBasicLogConfigurator::getLogLevel(const std::string & level) const
|
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
static const std::map<std::string, ELogLevel::ELogLevel> levelMap = boost::assign::map_list_of
|
|
|
|
("trace", ELogLevel::TRACE)
|
|
|
|
("debug", ELogLevel::DEBUG)
|
|
|
|
("info", ELogLevel::INFO)
|
|
|
|
("warn", ELogLevel::WARN)
|
|
|
|
("error", ELogLevel::ERROR);
|
|
|
|
const auto & levelPair = levelMap.find(level);
|
|
|
|
if(levelPair != levelMap.end())
|
|
|
|
{
|
|
|
|
return levelPair->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Log level " + level + " unknown.");
|
|
|
|
}
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EConsoleTextColor::EConsoleTextColor CBasicLogConfigurator::getConsoleColor(const std::string & colorName) const
|
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
static const std::map<std::string, EConsoleTextColor::EConsoleTextColor> colorMap = boost::assign::map_list_of
|
|
|
|
("default", EConsoleTextColor::DEFAULT)
|
|
|
|
("green", EConsoleTextColor::GREEN)
|
|
|
|
("red", EConsoleTextColor::RED)
|
|
|
|
("magenta", EConsoleTextColor::MAGENTA)
|
|
|
|
("yellow", EConsoleTextColor::YELLOW)
|
|
|
|
("white", EConsoleTextColor::WHITE)
|
|
|
|
("gray", EConsoleTextColor::GRAY)
|
|
|
|
("teal", EConsoleTextColor::TEAL);
|
|
|
|
const auto & colorPair = colorMap.find(colorName);
|
|
|
|
if(colorPair != colorMap.end())
|
|
|
|
{
|
|
|
|
return colorPair->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Color " + colorName + " unknown.");
|
|
|
|
}
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|