mirror of
https://github.com/vcmi/vcmi.git
synced 2026-05-16 09:28:24 +02:00
extracted base logger class to vstd
This commit is contained in:
@@ -242,7 +242,8 @@ template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
|
|||||||
/* ---------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------- */
|
||||||
/* VCMI standard library */
|
/* VCMI standard library */
|
||||||
/* ---------------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------------- */
|
||||||
#include "lib/logging/CLogger.h"
|
#include <vstd/CLoggerBase.h>
|
||||||
|
#include "lib/logging/CLogger.h" //todo: remove
|
||||||
|
|
||||||
void inline handleException()
|
void inline handleException()
|
||||||
{
|
{
|
||||||
@@ -252,15 +253,15 @@ void inline handleException()
|
|||||||
}
|
}
|
||||||
catch(const std::exception & ex)
|
catch(const std::exception & ex)
|
||||||
{
|
{
|
||||||
logGlobal->errorStream() << ex.what();
|
logGlobal->error(ex.what());
|
||||||
}
|
}
|
||||||
catch(const std::string & ex)
|
catch(const std::string & ex)
|
||||||
{
|
{
|
||||||
logGlobal->errorStream() << ex;
|
logGlobal->error(ex);
|
||||||
}
|
}
|
||||||
catch(...)
|
catch(...)
|
||||||
{
|
{
|
||||||
logGlobal->errorStream() << "Sorry, caught unknown exception type. No more info available.";
|
logGlobal->error("Sorry, caught unknown exception type. No more info available.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* CLoggerBase.h, 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
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ELogLevel
|
||||||
|
{
|
||||||
|
enum ELogLevel
|
||||||
|
{
|
||||||
|
NOT_SET = 0,
|
||||||
|
TRACE,
|
||||||
|
DEBUG,
|
||||||
|
INFO,
|
||||||
|
WARN,
|
||||||
|
ERROR
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace vstd
|
||||||
|
{
|
||||||
|
class DLL_LINKAGE CLoggerBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~CLoggerBase(){};
|
||||||
|
|
||||||
|
virtual void log(ELogLevel::ELogLevel level, const std::string & message) const = 0;
|
||||||
|
|
||||||
|
/// Log methods for various log levels
|
||||||
|
inline void trace(const std::string & message) const
|
||||||
|
{
|
||||||
|
log(ELogLevel::TRACE, message);
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void debug(const std::string & message) const
|
||||||
|
{
|
||||||
|
log(ELogLevel::DEBUG, message);
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void info(const std::string & message) const
|
||||||
|
{
|
||||||
|
log(ELogLevel::INFO, message);
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void warn(const std::string & message) const
|
||||||
|
{
|
||||||
|
log(ELogLevel::WARN, message);
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void error(const std::string & message) const
|
||||||
|
{
|
||||||
|
log(ELogLevel::ERROR, message);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
+26
-18
@@ -28,41 +28,49 @@ namespace vstd
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Logger, typename ... Args>
|
//TODO: Put into CLogger after log api extract
|
||||||
void logFormat(Logger * logger, ELogLevel::ELogLevel level, const std::string & format, Args ... args)
|
|
||||||
|
template<typename Logger>
|
||||||
|
void log(Logger * logger, ELogLevel::ELogLevel level, const std::string & message)
|
||||||
|
{
|
||||||
|
logger->log(level, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Logger, typename T, typename ... Args>
|
||||||
|
void log(Logger * logger, ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args)
|
||||||
{
|
{
|
||||||
boost::format fmt(format);
|
boost::format fmt(format);
|
||||||
detail::makeFormat(fmt, args...);
|
detail::makeFormat(fmt, t, args...);
|
||||||
logger->log(level, fmt.str());
|
logger->log(level, fmt.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Logger, typename ... Args>
|
template<typename Logger, typename T, typename ... Args>
|
||||||
void logErrorFormat(Logger * logger, const std::string & format, Args ... args)
|
void logError(Logger * logger, const std::string & format, T t, Args ... args)
|
||||||
{
|
{
|
||||||
logFormat(logger, ELogLevel::ERROR, format, args...);
|
log(logger, ELogLevel::ERROR, format, t, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Logger, typename ... Args>
|
template<typename Logger, typename T, typename ... Args>
|
||||||
void logWarnFormat(Logger * logger, const std::string & format, Args ... args)
|
void logWarn(Logger * logger, const std::string & format, T t, Args ... args)
|
||||||
{
|
{
|
||||||
logFormat(logger, ELogLevel::WARN, format, args...);
|
log(logger, ELogLevel::WARN, format, t, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Logger, typename ... Args>
|
template<typename Logger, typename T, typename ... Args>
|
||||||
void logInfoFormat(Logger * logger, const std::string & format, Args ... args)
|
void logInfo(Logger * logger, const std::string & format, T t, Args ... args)
|
||||||
{
|
{
|
||||||
logFormat(logger, ELogLevel::INFO, format, args...);
|
log(logger, ELogLevel::INFO, format, t, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Logger, typename ... Args>
|
template<typename Logger, typename T, typename ... Args>
|
||||||
void logDebugFormat(Logger * logger, const std::string & format, Args ... args)
|
void logDebug(Logger * logger, const std::string & format, T t, Args ... args)
|
||||||
{
|
{
|
||||||
logFormat(logger, ELogLevel::DEBUG, format, args...);
|
log(logger, ELogLevel::DEBUG, format, t, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Logger, typename ... Args>
|
template<typename Logger, typename T, typename ... Args>
|
||||||
void logTraceFormat(Logger * logger, const std::string & format, Args ... args)
|
void logTrace(Logger * logger, const std::string & format, T t, Args ... args)
|
||||||
{
|
{
|
||||||
logFormat(logger, ELogLevel::TRACE, format, args...);
|
log(logger, ELogLevel::TRACE, format, t, args...);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,14 +78,14 @@ const PlayerState * CGameInfoCallback::getPlayer(PlayerColor color, bool verbose
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (verbose)
|
if (verbose)
|
||||||
vstd::logErrorFormat(logGlobal, "Cannot access player %d info!", color);
|
vstd::logError(logGlobal, "Cannot access player %d info!", color);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (verbose)
|
if (verbose)
|
||||||
vstd::logErrorFormat(logGlobal, "Cannot find player %d info!", color);
|
vstd::logError(logGlobal, "Cannot find player %d info!", color);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -842,7 +842,7 @@ const TeamState * CGameInfoCallback::getTeam( TeamID teamID ) const
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vstd::logErrorFormat(logGlobal, "Cannot find info for team %d", teamID);
|
vstd::logError(logGlobal, "Cannot find info for team %d", teamID);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,6 +121,7 @@
|
|||||||
<Add directory="$(#zlib.lib)" />
|
<Add directory="$(#zlib.lib)" />
|
||||||
</Linker>
|
</Linker>
|
||||||
<Unit filename="../Global.h" />
|
<Unit filename="../Global.h" />
|
||||||
|
<Unit filename="../include/vstd/CLoggerBase.h" />
|
||||||
<Unit filename="../include/vstd/LogFormat.h" />
|
<Unit filename="../include/vstd/LogFormat.h" />
|
||||||
<Unit filename="AI_Base.h" />
|
<Unit filename="AI_Base.h" />
|
||||||
<Unit filename="BattleAction.cpp" />
|
<Unit filename="BattleAction.cpp" />
|
||||||
|
|||||||
@@ -101,12 +101,6 @@ CLogger::CLogger(const CLoggerDomain & domain) : domain(domain)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLogger::trace(const std::string & message) const { log(ELogLevel::TRACE, message); }
|
|
||||||
void CLogger::debug(const std::string & message) const { log(ELogLevel::DEBUG, message); }
|
|
||||||
void CLogger::info(const std::string & message) const { log(ELogLevel::INFO, message); }
|
|
||||||
void CLogger::warn(const std::string & message) const { log(ELogLevel::WARN, message); }
|
|
||||||
void CLogger::error(const std::string & message) const { log(ELogLevel::ERROR, message); }
|
|
||||||
|
|
||||||
CLoggerStream CLogger::traceStream() const { return CLoggerStream(*this, ELogLevel::TRACE); }
|
CLoggerStream CLogger::traceStream() const { return CLoggerStream(*this, ELogLevel::TRACE); }
|
||||||
CLoggerStream CLogger::debugStream() const { return CLoggerStream(*this, ELogLevel::DEBUG); }
|
CLoggerStream CLogger::debugStream() const { return CLoggerStream(*this, ELogLevel::DEBUG); }
|
||||||
CLoggerStream CLogger::infoStream() const { return CLoggerStream(*this, ELogLevel::INFO); }
|
CLoggerStream CLogger::infoStream() const { return CLoggerStream(*this, ELogLevel::INFO); }
|
||||||
@@ -233,7 +227,7 @@ std::string CLogFormatter::format(const LogRecord & record) const
|
|||||||
//Format date
|
//Format date
|
||||||
boost::algorithm::replace_first(message, "%d", boost::posix_time::to_simple_string (record.timeStamp));
|
boost::algorithm::replace_first(message, "%d", boost::posix_time::to_simple_string (record.timeStamp));
|
||||||
|
|
||||||
//Format log level
|
//Format log level
|
||||||
std::string level;
|
std::string level;
|
||||||
switch(record.level)
|
switch(record.level)
|
||||||
{
|
{
|
||||||
@@ -331,7 +325,7 @@ void CLogConsoleTarget::write(const LogRecord & record)
|
|||||||
{
|
{
|
||||||
const EConsoleTextColor::EConsoleTextColor textColor =
|
const EConsoleTextColor::EConsoleTextColor textColor =
|
||||||
coloredOutputEnabled ? colorMapping.getColorFor(record.domain, record.level) : EConsoleTextColor::DEFAULT;
|
coloredOutputEnabled ? colorMapping.getColorFor(record.domain, record.level) : EConsoleTextColor::DEFAULT;
|
||||||
|
|
||||||
console->print(message, true, textColor, printToStdErr);
|
console->print(message, true, textColor, printToStdErr);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
+3
-19
@@ -18,18 +18,9 @@ class CLogger;
|
|||||||
struct LogRecord;
|
struct LogRecord;
|
||||||
class ILogTarget;
|
class ILogTarget;
|
||||||
|
|
||||||
|
|
||||||
namespace ELogLevel
|
namespace ELogLevel
|
||||||
{
|
{
|
||||||
enum ELogLevel
|
|
||||||
{
|
|
||||||
NOT_SET = 0,
|
|
||||||
TRACE,
|
|
||||||
DEBUG,
|
|
||||||
INFO,
|
|
||||||
WARN,
|
|
||||||
ERROR
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef VCMI_ANDROID
|
#ifdef VCMI_ANDROID
|
||||||
int toAndroid(ELogLevel logLevel);
|
int toAndroid(ELogLevel logLevel);
|
||||||
#endif
|
#endif
|
||||||
@@ -79,7 +70,7 @@ private:
|
|||||||
|
|
||||||
/// The logger is used to log messages to certain targets of a specific domain/name.
|
/// The logger is used to log messages to certain targets of a specific domain/name.
|
||||||
/// It is thread-safe and can be used concurrently by several threads.
|
/// It is thread-safe and can be used concurrently by several threads.
|
||||||
class DLL_LINKAGE CLogger
|
class DLL_LINKAGE CLogger: public vstd::CLoggerBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline ELogLevel::ELogLevel getLevel() const;
|
inline ELogLevel::ELogLevel getLevel() const;
|
||||||
@@ -90,13 +81,6 @@ public:
|
|||||||
static CLogger * getLogger(const CLoggerDomain & domain);
|
static CLogger * getLogger(const CLoggerDomain & domain);
|
||||||
static CLogger * getGlobalLogger();
|
static CLogger * getGlobalLogger();
|
||||||
|
|
||||||
/// Log methods for various log levels
|
|
||||||
void trace(const std::string & message) const;
|
|
||||||
void debug(const std::string & message) const;
|
|
||||||
void info(const std::string & message) const;
|
|
||||||
void warn(const std::string & message) const;
|
|
||||||
void error(const std::string & message) const;
|
|
||||||
|
|
||||||
/// Log streams for various log levels
|
/// Log streams for various log levels
|
||||||
CLoggerStream traceStream() const;
|
CLoggerStream traceStream() const;
|
||||||
CLoggerStream debugStream() const;
|
CLoggerStream debugStream() const;
|
||||||
@@ -104,7 +88,7 @@ public:
|
|||||||
CLoggerStream warnStream() const;
|
CLoggerStream warnStream() const;
|
||||||
CLoggerStream errorStream() const;
|
CLoggerStream errorStream() const;
|
||||||
|
|
||||||
void log(ELogLevel::ELogLevel level, const std::string & message) const;
|
void log(ELogLevel::ELogLevel level, const std::string & message) const override;
|
||||||
|
|
||||||
void addTarget(std::unique_ptr<ILogTarget> && target);
|
void addTarget(std::unique_ptr<ILogTarget> && target);
|
||||||
void clearTargets();
|
void clearTargets();
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ const CSpell::LevelInfo & CSpell::getLevelInfo(const int level) const
|
|||||||
{
|
{
|
||||||
if(level < 0 || level >= GameConstants::SPELL_SCHOOL_LEVELS)
|
if(level < 0 || level >= GameConstants::SPELL_SCHOOL_LEVELS)
|
||||||
{
|
{
|
||||||
vstd::logErrorFormat(logGlobal, "CSpell::getLevelInfo invalid school level %d", level);
|
vstd::logError(logGlobal, "CSpell::getLevelInfo invalid school level %d", level);
|
||||||
throw new std::runtime_error("Invalid school level");
|
throw new std::runtime_error("Invalid school level");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@
|
|||||||
<Add option="-Wno-unused-parameter" />
|
<Add option="-Wno-unused-parameter" />
|
||||||
<Add option="-Wno-overloaded-virtual" />
|
<Add option="-Wno-overloaded-virtual" />
|
||||||
<Add directory="$(#boost.include)" />
|
<Add directory="$(#boost.include)" />
|
||||||
<Add directory="../include" />
|
<Add directory="../../include" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
<Linker>
|
<Linker>
|
||||||
<Add option="-lboost_program_options$(#boost.libsuffix)" />
|
<Add option="-lboost_program_options$(#boost.libsuffix)" />
|
||||||
|
|||||||
Reference in New Issue
Block a user