Răsfoiți Sursa

extracted base logger class to vstd

AlexVinS 9 ani în urmă
părinte
comite
599f4cfb55

+ 5 - 4
Global.h

@@ -242,7 +242,8 @@ template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
 /* ---------------------------------------------------------------------------- */
 /* VCMI standard library */
 /* ---------------------------------------------------------------------------- */
-#include "lib/logging/CLogger.h"
+#include <vstd/CLoggerBase.h>
+#include "lib/logging/CLogger.h" //todo: remove
 
 void inline handleException()
 {
@@ -252,15 +253,15 @@ void inline handleException()
 	}
 	catch(const std::exception & ex)
 	{
-		logGlobal->errorStream() << ex.what();
+		logGlobal->error(ex.what());
 	}
 	catch(const std::string & ex)
 	{
-		logGlobal->errorStream() << ex;
+		logGlobal->error(ex);
 	}
 	catch(...)
 	{
-		logGlobal->errorStream() << "Sorry, caught unknown exception type. No more info available.";
+		logGlobal->error("Sorry, caught unknown exception type. No more info available.");
 	}
 }
 

+ 61 - 0
include/vstd/CLoggerBase.h

@@ -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
include/vstd/LogFormat.h

@@ -28,41 +28,49 @@ namespace vstd
 		}
 	}
 
-    template<typename Logger, typename ... Args>
-    void logFormat(Logger * logger, ELogLevel::ELogLevel level, const std::string & format, Args ... args)
+	//TODO: Put into CLogger after log api extract
+
+    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);
-		detail::makeFormat(fmt, args...);
+		detail::makeFormat(fmt, t, args...);
 		logger->log(level, fmt.str());
     }
 
-    template<typename Logger, typename ... Args>
-    void logErrorFormat(Logger * logger, const std::string & format, Args ... args)
+    template<typename Logger, typename T, typename ... 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>
-    void logWarnFormat(Logger * logger, const std::string & format, Args ... args)
+    template<typename Logger, typename T, typename ... 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>
-    void logInfoFormat(Logger * logger, const std::string & format, Args ... args)
+    template<typename Logger, typename T, typename ... 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>
-    void logDebugFormat(Logger * logger, const std::string & format, Args ... args)
+    template<typename Logger, typename T, typename ... 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>
-    void logTraceFormat(Logger * logger, const std::string & format, Args ... args)
+    template<typename Logger, typename T, typename ... 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...);
     }
 }

+ 3 - 3
lib/CGameInfoCallback.cpp

@@ -78,14 +78,14 @@ const PlayerState * CGameInfoCallback::getPlayer(PlayerColor color, bool verbose
 		else
 		{
 			if (verbose)
-				vstd::logErrorFormat(logGlobal, "Cannot access player %d info!", color);
+				vstd::logError(logGlobal, "Cannot access player %d info!", color);
 			return nullptr;
 		}
 	}
 	else
 	{
 		if (verbose)
-			vstd::logErrorFormat(logGlobal, "Cannot find player %d info!", color);
+			vstd::logError(logGlobal, "Cannot find player %d info!", color);
 		return nullptr;
 	}
 }
@@ -842,7 +842,7 @@ const TeamState * CGameInfoCallback::getTeam( TeamID teamID ) const
 	}
 	else
 	{
-		vstd::logErrorFormat(logGlobal, "Cannot find info for team %d", teamID);
+		vstd::logError(logGlobal, "Cannot find info for team %d", teamID);
 		return nullptr;
 	}
 }

+ 1 - 0
lib/VCMI_lib.cbp

@@ -121,6 +121,7 @@
 			<Add directory="$(#zlib.lib)" />
 		</Linker>
 		<Unit filename="../Global.h" />
+		<Unit filename="../include/vstd/CLoggerBase.h" />
 		<Unit filename="../include/vstd/LogFormat.h" />
 		<Unit filename="AI_Base.h" />
 		<Unit filename="BattleAction.cpp" />

+ 2 - 8
lib/logging/CLogger.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::debugStream() const { return CLoggerStream(*this, ELogLevel::DEBUG); }
 CLoggerStream CLogger::infoStream() const { return CLoggerStream(*this, ELogLevel::INFO); }
@@ -233,7 +227,7 @@ std::string CLogFormatter::format(const LogRecord & record) const
 	//Format date
 	boost::algorithm::replace_first(message, "%d", boost::posix_time::to_simple_string (record.timeStamp));
 
-	//Format log level 
+	//Format log level
 	std::string level;
 	switch(record.level)
 	{
@@ -331,7 +325,7 @@ void CLogConsoleTarget::write(const LogRecord & record)
 	{
 		const EConsoleTextColor::EConsoleTextColor textColor =
 			coloredOutputEnabled ? colorMapping.getColorFor(record.domain, record.level) : EConsoleTextColor::DEFAULT;
-		
+
 		console->print(message, true, textColor, printToStdErr);
 	}
 	else

+ 3 - 19
lib/logging/CLogger.h

@@ -18,18 +18,9 @@ class CLogger;
 struct LogRecord;
 class ILogTarget;
 
+
 namespace ELogLevel
 {
-	enum ELogLevel
-	{
-		NOT_SET = 0,
-		TRACE,
-		DEBUG,
-		INFO,
-		WARN,
-		ERROR
-	};
-
 	#ifdef VCMI_ANDROID
 		int toAndroid(ELogLevel logLevel);
 	#endif
@@ -79,7 +70,7 @@ private:
 
 /// 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.
-class DLL_LINKAGE CLogger
+class DLL_LINKAGE CLogger: public vstd::CLoggerBase
 {
 public:
 	inline ELogLevel::ELogLevel getLevel() const;
@@ -90,13 +81,6 @@ public:
 	static CLogger * getLogger(const CLoggerDomain & domain);
 	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
 	CLoggerStream traceStream() const;
 	CLoggerStream debugStream() const;
@@ -104,7 +88,7 @@ public:
 	CLoggerStream warnStream() 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 clearTargets();

+ 1 - 1
lib/spells/CSpellHandler.cpp

@@ -129,7 +129,7 @@ const CSpell::LevelInfo & CSpell::getLevelInfo(const int level) const
 {
 	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");
 	}
 

+ 1 - 1
scripting/erm/ERM.cbp

@@ -62,7 +62,7 @@
 			<Add option="-Wno-unused-parameter" />
 			<Add option="-Wno-overloaded-virtual" />
 			<Add directory="$(#boost.include)" />
-			<Add directory="../include" />
+			<Add directory="../../include" />
 		</Compiler>
 		<Linker>
 			<Add option="-lboost_program_options$(#boost.libsuffix)" />