2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CLogger.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2013-04-05 13:29:46 +03:00
|
|
|
#include "StdInc.h"
|
2013-04-11 18:58:01 +03:00
|
|
|
#include "CLogger.h"
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
#include <android/log.h>
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
namespace ELogLevel
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
int toAndroid(ELogLevel logLevel)
|
|
|
|
{
|
|
|
|
switch (logLevel)
|
|
|
|
{
|
|
|
|
case TRACE: return ANDROID_LOG_VERBOSE;
|
|
|
|
case DEBUG: return ANDROID_LOG_DEBUG;
|
|
|
|
case INFO: return ANDROID_LOG_INFO;
|
|
|
|
case WARN: return ANDROID_LOG_WARN;
|
|
|
|
case ERROR: return ANDROID_LOG_ERROR;
|
2017-07-17 23:04:00 +02:00
|
|
|
default:
|
|
|
|
break;
|
2014-08-21 23:26:28 +03:00
|
|
|
}
|
|
|
|
return ANDROID_LOG_UNKNOWN;
|
|
|
|
}
|
2014-08-11 21:17:17 +03:00
|
|
|
}
|
2023-02-26 11:18:24 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
2021-03-13 12:47:06 +02:00
|
|
|
#elif defined(VCMI_IOS)
|
2022-08-15 15:29:11 +02:00
|
|
|
#import "iOS_utils.h"
|
2021-03-20 16:05:32 +02:00
|
|
|
extern "C" {
|
2021-03-13 12:47:06 +02:00
|
|
|
#include <os/log.h>
|
|
|
|
}
|
2014-08-21 23:26:28 +03:00
|
|
|
#endif
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
namespace vstd
|
|
|
|
{
|
|
|
|
|
|
|
|
CLoggerBase::~CLoggerBase() = default;
|
|
|
|
|
|
|
|
|
|
|
|
CTraceLogger::CTraceLogger(const CLoggerBase * logger, const std::string & beginMessage, const std::string & endMessage)
|
|
|
|
: logger(logger), endMessage(endMessage)
|
|
|
|
{
|
|
|
|
logger->trace(beginMessage);
|
|
|
|
}
|
2017-08-12 13:36:04 +02:00
|
|
|
|
|
|
|
CTraceLogger::~CTraceLogger()
|
|
|
|
{
|
|
|
|
logger->trace(endMessage);
|
|
|
|
}
|
2017-08-11 19:03:05 +02:00
|
|
|
|
|
|
|
}//namespace vstd
|
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
const std::string CLoggerDomain::DOMAIN_GLOBAL = "global";
|
|
|
|
|
|
|
|
CLoggerDomain::CLoggerDomain(std::string name) : name(std::move(name))
|
2014-08-11 21:17:17 +03:00
|
|
|
{
|
|
|
|
if (this->name.empty())
|
|
|
|
throw std::runtime_error("Logger domain cannot be empty.");
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CLoggerDomain CLoggerDomain::getParent() const
|
|
|
|
{
|
2014-08-11 21:17:17 +03:00
|
|
|
if(isGlobalDomain())
|
|
|
|
return *this;
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const size_t pos = name.find_last_of(".");
|
2013-04-14 22:24:31 +03:00
|
|
|
if(pos != std::string::npos)
|
|
|
|
return CLoggerDomain(name.substr(0, pos));
|
2014-08-11 21:17:17 +03:00
|
|
|
return CLoggerDomain(DOMAIN_GLOBAL);
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
bool CLoggerDomain::isGlobalDomain() const { return name == DOMAIN_GLOBAL; }
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const std::string& CLoggerDomain::getName() const { return name; }
|
2013-04-09 17:31:36 +03:00
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
boost::recursive_mutex CLogger::smx;
|
2013-04-19 15:26:57 +03:00
|
|
|
boost::recursive_mutex CLogManager::smx;
|
2013-04-09 17:31:36 +03:00
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
DLL_LINKAGE vstd::CLoggerBase * logGlobal = CLogger::getGlobalLogger();
|
2013-04-09 17:31:36 +03:00
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
DLL_LINKAGE vstd::CLoggerBase * logBonus = CLogger::getLogger(CLoggerDomain("bonus"));
|
|
|
|
DLL_LINKAGE vstd::CLoggerBase * logNetwork = CLogger::getLogger(CLoggerDomain("network"));
|
|
|
|
DLL_LINKAGE vstd::CLoggerBase * logAi = CLogger::getLogger(CLoggerDomain("ai"));
|
|
|
|
DLL_LINKAGE vstd::CLoggerBase * logAnim = CLogger::getLogger(CLoggerDomain("animation"));
|
2017-08-26 04:06:47 +02:00
|
|
|
DLL_LINKAGE vstd::CLoggerBase * logMod = CLogger::getLogger(CLoggerDomain("mod"));
|
2013-04-10 19:28:14 +03:00
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
CLogger * CLogger::getLogger(const CLoggerDomain & domain)
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2014-05-10 22:12:33 +03:00
|
|
|
TLockGuardRec _(smx);
|
2013-04-09 17:31:36 +03:00
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
CLogger * logger = CLogManager::get().getLogger(domain);
|
2014-08-11 21:17:17 +03:00
|
|
|
if(!logger) // Create new logger
|
2013-04-14 22:24:31 +03:00
|
|
|
{
|
2013-04-11 18:58:01 +03:00
|
|
|
logger = new CLogger(domain);
|
2013-04-14 22:24:31 +03:00
|
|
|
if(domain.isGlobalDomain())
|
2016-10-02 20:21:20 +02:00
|
|
|
{
|
2013-07-29 13:08:02 +03:00
|
|
|
logger->setLevel(ELogLevel::TRACE);
|
2016-10-02 20:21:20 +02:00
|
|
|
}
|
2013-04-14 22:24:31 +03:00
|
|
|
CLogManager::get().addLogger(logger);
|
2016-10-02 20:21:20 +02:00
|
|
|
if (logGlobal != nullptr)
|
|
|
|
{
|
2016-10-03 20:32:01 +02:00
|
|
|
logGlobal->debug("Created logger %s", domain.getName());
|
2016-10-02 20:21:20 +02:00
|
|
|
}
|
2013-04-14 22:24:31 +03:00
|
|
|
}
|
2014-08-11 21:17:17 +03:00
|
|
|
return logger;
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
CLogger * CLogger::getGlobalLogger()
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
return getLogger(CLoggerDomain(CLoggerDomain::DOMAIN_GLOBAL));
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
CLogger::CLogger(const CLoggerDomain & domain) : domain(domain)
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
if(domain.isGlobalDomain())
|
|
|
|
{
|
2013-07-29 13:08:02 +03:00
|
|
|
level = ELogLevel::TRACE;
|
2013-04-14 22:24:31 +03:00
|
|
|
parent = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level = ELogLevel::NOT_SET;
|
|
|
|
parent = getLogger(domain.getParent());
|
|
|
|
}
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
void CLogger::log(ELogLevel::ELogLevel level, const std::string & message) const
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
if(getEffectiveLevel() <= level)
|
|
|
|
callTargets(LogRecord(domain, level, message));
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2016-11-28 18:34:20 +02:00
|
|
|
void CLogger::log(ELogLevel::ELogLevel level, const boost::format & fmt) const
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
log(level, fmt.str());
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2023-01-08 18:23:25 +02:00
|
|
|
log(ELogLevel::ERROR, "Invalid log format!");
|
2016-11-28 18:34:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
ELogLevel::ELogLevel CLogger::getLevel() const
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
|
|
|
return level;
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
void CLogger::setLevel(ELogLevel::ELogLevel level)
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
2014-08-11 21:17:17 +03:00
|
|
|
if (!domain.isGlobalDomain() || level != ELogLevel::NOT_SET)
|
|
|
|
this->level = level;
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const CLoggerDomain & CLogger::getDomain() const { return domain; }
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
void CLogger::addTarget(std::unique_ptr<ILogTarget> && target)
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
|
|
|
targets.push_back(std::move(target));
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
ELogLevel::ELogLevel CLogger::getEffectiveLevel() const
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-11 18:58:01 +03:00
|
|
|
for(const CLogger * logger = this; logger != nullptr; logger = logger->parent)
|
2014-08-11 21:17:17 +03:00
|
|
|
if(logger->getLevel() != ELogLevel::NOT_SET)
|
|
|
|
return logger->getLevel();
|
2013-04-05 13:29:46 +03:00
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
// This shouldn't be reached, as the root logger must have set a log level
|
|
|
|
return ELogLevel::INFO;
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
void CLogger::callTargets(const LogRecord & record) const
|
2013-04-05 13:29:46 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
2013-04-11 18:58:01 +03:00
|
|
|
for(const CLogger * logger = this; logger != nullptr; logger = logger->parent)
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & target : logger->targets)
|
2013-04-14 22:24:31 +03:00
|
|
|
target->write(record);
|
2013-04-05 13:29:46 +03:00
|
|
|
}
|
2013-04-06 20:45:02 +03:00
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
void CLogger::clearTargets()
|
2013-04-06 20:45:02 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
|
|
|
targets.clear();
|
2013-04-06 20:45:02 +03:00
|
|
|
}
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
bool CLogger::isDebugEnabled() const { return getEffectiveLevel() <= ELogLevel::DEBUG; }
|
|
|
|
bool CLogger::isTraceEnabled() const { return getEffectiveLevel() <= ELogLevel::TRACE; }
|
2013-04-10 19:28:14 +03:00
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
CLogManager & CLogManager::get()
|
2013-04-07 13:17:55 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuardRec _(smx);
|
|
|
|
static CLogManager instance;
|
|
|
|
return instance;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
CLogManager::CLogManager() = default;
|
2013-04-07 13:17:55 +03:00
|
|
|
CLogManager::~CLogManager()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & i : loggers)
|
2013-04-14 22:24:31 +03:00
|
|
|
delete i.second;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
void CLogManager::addLogger(CLogger * logger)
|
2013-04-07 13:17:55 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
|
|
|
loggers[logger->getDomain().getName()] = logger;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2013-04-11 18:58:01 +03:00
|
|
|
CLogger * CLogManager::getLogger(const CLoggerDomain & domain)
|
2013-04-07 13:17:55 +03:00
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
|
|
|
auto it = loggers.find(domain.getName());
|
|
|
|
if(it != loggers.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return nullptr;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:21:20 +02:00
|
|
|
std::vector<std::string> CLogManager::getRegisteredDomains() const
|
|
|
|
{
|
|
|
|
std::vector<std::string> domains;
|
|
|
|
for (auto& pair : loggers)
|
|
|
|
{
|
|
|
|
domains.push_back(pair.second->getDomain().getName());
|
|
|
|
}
|
2019-01-19 12:52:02 +02:00
|
|
|
return domains;
|
2016-10-02 20:21:20 +02:00
|
|
|
}
|
|
|
|
|
2019-01-15 05:00:00 +02:00
|
|
|
CLogFormatter::CLogFormatter()
|
|
|
|
: CLogFormatter("%m")
|
|
|
|
{
|
|
|
|
}
|
2014-08-11 21:17:17 +03:00
|
|
|
|
2019-01-15 05:00:00 +02:00
|
|
|
CLogFormatter::CLogFormatter(const std::string & pattern)
|
|
|
|
: pattern(pattern)
|
2013-04-07 13:17:55 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
CLogFormatter::CLogFormatter(const CLogFormatter & c) : pattern(c.pattern) { }
|
|
|
|
CLogFormatter::CLogFormatter(CLogFormatter && m) : pattern(std::move(m.pattern)) { }
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
CLogFormatter & CLogFormatter::operator=(const CLogFormatter & c)
|
2014-04-01 22:46:47 +03:00
|
|
|
{
|
2014-08-11 21:17:17 +03:00
|
|
|
pattern = c.pattern;
|
|
|
|
return *this;
|
2014-04-01 22:46:47 +03:00
|
|
|
}
|
2014-08-11 21:17:17 +03:00
|
|
|
CLogFormatter & CLogFormatter::operator=(CLogFormatter && m)
|
2014-04-01 22:46:47 +03:00
|
|
|
{
|
2014-08-11 21:17:17 +03:00
|
|
|
pattern = std::move(m.pattern);
|
2014-04-01 22:46:47 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-04-07 13:17:55 +03:00
|
|
|
std::string CLogFormatter::format(const LogRecord & record) const
|
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
std::string message = pattern;
|
|
|
|
|
2015-08-30 19:26:19 +02:00
|
|
|
//Format date
|
2019-01-15 05:00:00 +02:00
|
|
|
// boost::algorithm::replace_first(message, "%d", boost::posix_time::to_simple_string (record.timeStamp));
|
2013-04-14 22:24:31 +03:00
|
|
|
|
2016-08-12 14:51:14 +02:00
|
|
|
//Format log level
|
2013-04-14 22:24:31 +03:00
|
|
|
std::string level;
|
|
|
|
switch(record.level)
|
|
|
|
{
|
|
|
|
case ELogLevel::TRACE:
|
|
|
|
level = "TRACE";
|
|
|
|
break;
|
|
|
|
case ELogLevel::DEBUG:
|
|
|
|
level = "DEBUG";
|
|
|
|
break;
|
|
|
|
case ELogLevel::INFO:
|
|
|
|
level = "INFO";
|
|
|
|
break;
|
|
|
|
case ELogLevel::WARN:
|
|
|
|
level = "WARN";
|
|
|
|
break;
|
|
|
|
case ELogLevel::ERROR:
|
|
|
|
level = "ERROR";
|
|
|
|
break;
|
|
|
|
}
|
2014-04-01 22:46:47 +03:00
|
|
|
boost::algorithm::replace_first(message, "%l", level);
|
2013-04-14 22:24:31 +03:00
|
|
|
|
2015-08-30 19:26:19 +02:00
|
|
|
//Format name, thread id and message
|
2014-04-01 22:46:47 +03:00
|
|
|
boost::algorithm::replace_first(message, "%n", record.domain.getName());
|
2015-08-30 19:26:19 +02:00
|
|
|
boost::algorithm::replace_first(message, "%t", record.threadId);
|
2014-04-01 22:46:47 +03:00
|
|
|
boost::algorithm::replace_first(message, "%m", record.message);
|
2023-01-08 18:23:07 +02:00
|
|
|
boost::algorithm::replace_first(message, "%c", boost::posix_time::to_simple_string(record.timeStamp));
|
2013-04-14 22:24:31 +03:00
|
|
|
|
2015-08-30 19:26:19 +02:00
|
|
|
//return boost::to_string (boost::format("%d %d %d[%d] - %d") % dateStream.str() % level % record.domain.getName() % record.threadId % record.message);
|
|
|
|
|
2013-04-14 22:24:31 +03:00
|
|
|
return message;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
void CLogFormatter::setPattern(const std::string & pattern) { this->pattern = pattern; }
|
|
|
|
void CLogFormatter::setPattern(std::string && pattern) { this->pattern = std::move(pattern); }
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const std::string & CLogFormatter::getPattern() const { return pattern; }
|
2013-04-07 13:17:55 +03:00
|
|
|
|
|
|
|
CColorMapping::CColorMapping()
|
|
|
|
{
|
2013-04-14 22:24:31 +03:00
|
|
|
// Set default mappings
|
|
|
|
auto & levelMap = map[CLoggerDomain::DOMAIN_GLOBAL];
|
|
|
|
levelMap[ELogLevel::TRACE] = EConsoleTextColor::GRAY;
|
|
|
|
levelMap[ELogLevel::DEBUG] = EConsoleTextColor::WHITE;
|
|
|
|
levelMap[ELogLevel::INFO] = EConsoleTextColor::GREEN;
|
|
|
|
levelMap[ELogLevel::WARN] = EConsoleTextColor::YELLOW;
|
|
|
|
levelMap[ELogLevel::ERROR] = EConsoleTextColor::RED;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CColorMapping::setColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level, EConsoleTextColor::EConsoleTextColor color)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(level != ELogLevel::NOT_SET);
|
2013-04-14 22:24:31 +03:00
|
|
|
map[domain.getName()][level] = color;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
EConsoleTextColor::EConsoleTextColor CColorMapping::getColorFor(const CLoggerDomain & domain, ELogLevel::ELogLevel level) const
|
|
|
|
{
|
2014-08-11 21:17:17 +03:00
|
|
|
CLoggerDomain currentDomain = domain;
|
2013-04-14 22:24:31 +03:00
|
|
|
while(true)
|
|
|
|
{
|
2014-08-11 21:17:17 +03:00
|
|
|
const auto & loggerPair = map.find(currentDomain.getName());
|
2013-04-14 22:24:31 +03:00
|
|
|
if(loggerPair != map.end())
|
|
|
|
{
|
|
|
|
const auto & levelMap = loggerPair->second;
|
|
|
|
const auto & levelPair = levelMap.find(level);
|
|
|
|
if(levelPair != levelMap.end())
|
|
|
|
return levelPair->second;
|
|
|
|
}
|
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
if (currentDomain.isGlobalDomain())
|
2013-04-14 22:24:31 +03:00
|
|
|
break;
|
2014-08-11 21:17:17 +03:00
|
|
|
|
|
|
|
currentDomain = currentDomain.getParent();
|
2013-04-14 22:24:31 +03:00
|
|
|
}
|
2014-08-11 21:17:17 +03:00
|
|
|
|
2013-11-09 16:49:36 +03:00
|
|
|
throw std::runtime_error("failed to find color for requested domain/level pair");
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2022-09-27 06:05:10 +02:00
|
|
|
CLogConsoleTarget::CLogConsoleTarget(CConsoleHandler * console) :
|
2023-02-27 12:00:13 +02:00
|
|
|
#if !defined(VCMI_MOBILE)
|
2023-01-08 18:23:25 +02:00
|
|
|
console(console),
|
2022-09-27 06:05:10 +02:00
|
|
|
#endif
|
2023-01-08 18:23:25 +02:00
|
|
|
threshold(ELogLevel::INFO), coloredOutputEnabled(true)
|
2013-04-07 13:17:55 +03:00
|
|
|
{
|
2013-04-11 22:24:14 +03:00
|
|
|
formatter.setPattern("%m");
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogConsoleTarget::write(const LogRecord & record)
|
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
if(threshold > record.level)
|
|
|
|
return;
|
2013-04-14 22:24:31 +03:00
|
|
|
|
|
|
|
std::string message = formatter.format(record);
|
2014-02-20 21:53:18 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
#ifdef VCMI_ANDROID
|
2023-01-08 18:23:25 +02:00
|
|
|
__android_log_write(ELogLevel::toAndroid(record.level), ("VCMI-" + record.domain.getName()).c_str(), message.c_str());
|
2021-03-13 12:47:06 +02:00
|
|
|
#elif defined(VCMI_IOS)
|
2022-08-15 15:31:58 +02:00
|
|
|
os_log_type_t type;
|
|
|
|
switch (record.level)
|
|
|
|
{
|
|
|
|
case ELogLevel::TRACE:
|
|
|
|
type = OS_LOG_TYPE_DEBUG;
|
|
|
|
break;
|
|
|
|
case ELogLevel::DEBUG:
|
|
|
|
type = OS_LOG_TYPE_DEFAULT;
|
|
|
|
break;
|
|
|
|
case ELogLevel::INFO:
|
|
|
|
type = OS_LOG_TYPE_INFO;
|
|
|
|
break;
|
|
|
|
case ELogLevel::WARN:
|
|
|
|
type = OS_LOG_TYPE_ERROR;
|
|
|
|
break;
|
|
|
|
case ELogLevel::ERROR:
|
|
|
|
type = OS_LOG_TYPE_FAULT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
os_log_t currentLog;
|
|
|
|
static std::unordered_map<std::string, decltype(currentLog)> logs;
|
|
|
|
const auto& domainName = record.domain.getName();
|
|
|
|
auto logIt = logs.find(domainName);
|
|
|
|
if (logIt != logs.end())
|
|
|
|
currentLog = logIt->second;
|
|
|
|
else
|
|
|
|
{
|
2022-08-15 15:29:11 +02:00
|
|
|
currentLog = os_log_create(iOS_utils::bundleIdentifier(), domainName.c_str());
|
2022-08-15 15:31:58 +02:00
|
|
|
logs.insert({domainName, currentLog});
|
|
|
|
}
|
|
|
|
os_log_with_type(currentLog, type, "%{public}s", message.c_str());
|
2017-05-25 19:57:20 +02:00
|
|
|
#else
|
2014-02-20 21:53:18 +03:00
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
const bool printToStdErr = record.level >= ELogLevel::WARN;
|
2013-04-14 22:24:31 +03:00
|
|
|
if(console)
|
|
|
|
{
|
2014-08-11 21:17:17 +03:00
|
|
|
const EConsoleTextColor::EConsoleTextColor textColor =
|
|
|
|
coloredOutputEnabled ? colorMapping.getColorFor(record.domain, record.level) : EConsoleTextColor::DEFAULT;
|
2016-08-12 14:51:14 +02:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
console->print(message, true, textColor, printToStdErr);
|
2013-04-14 22:24:31 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TLockGuard _(mx);
|
|
|
|
if(printToStdErr)
|
|
|
|
std::cerr << message << std::endl;
|
|
|
|
else
|
|
|
|
std::cout << message << std::endl;
|
|
|
|
}
|
2017-05-25 19:57:20 +02:00
|
|
|
#endif
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
bool CLogConsoleTarget::isColoredOutputEnabled() const { return coloredOutputEnabled; }
|
|
|
|
void CLogConsoleTarget::setColoredOutputEnabled(bool coloredOutputEnabled) { this->coloredOutputEnabled = coloredOutputEnabled; }
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
ELogLevel::ELogLevel CLogConsoleTarget::getThreshold() const { return threshold; }
|
|
|
|
void CLogConsoleTarget::setThreshold(ELogLevel::ELogLevel threshold) { this->threshold = threshold; }
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const CLogFormatter & CLogConsoleTarget::getFormatter() const { return formatter; }
|
|
|
|
void CLogConsoleTarget::setFormatter(const CLogFormatter & formatter) { this->formatter = formatter; }
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const CColorMapping & CLogConsoleTarget::getColorMapping() const { return colorMapping; }
|
|
|
|
void CLogConsoleTarget::setColorMapping(const CColorMapping & colorMapping) { this->colorMapping = colorMapping; }
|
2013-04-07 13:17:55 +03:00
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
CLogFileTarget::CLogFileTarget(boost::filesystem::path filePath, bool append)
|
2014-08-11 22:24:31 +03:00
|
|
|
: file(std::move(filePath), append ? std::ios_base::app : std::ios_base::out)
|
2013-04-07 13:17:55 +03:00
|
|
|
{
|
2019-01-15 05:00:00 +02:00
|
|
|
// formatter.setPattern("%d %l %n [%t] - %m");
|
|
|
|
formatter.setPattern("%l %n [%t] - %m");
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogFileTarget::write(const LogRecord & record)
|
|
|
|
{
|
2015-08-30 19:26:19 +02:00
|
|
|
std::string message = formatter.format(record); //formatting is slow, do it outside the lock
|
2013-04-14 22:24:31 +03:00
|
|
|
TLockGuard _(mx);
|
2015-08-30 19:26:19 +02:00
|
|
|
file << message << std::endl;
|
2013-04-07 13:17:55 +03:00
|
|
|
}
|
|
|
|
|
2014-08-11 21:17:17 +03:00
|
|
|
const CLogFormatter & CLogFileTarget::getFormatter() const { return formatter; }
|
2014-09-23 16:01:43 +03:00
|
|
|
void CLogFileTarget::setFormatter(const CLogFormatter & formatter) { this->formatter = formatter; }
|
2019-05-10 11:34:37 +02:00
|
|
|
|
2020-05-17 10:21:49 +02:00
|
|
|
CLogFileTarget::~CLogFileTarget()
|
|
|
|
{
|
2019-05-10 11:34:37 +02:00
|
|
|
file.close();
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|