Browse Source

Implemented boost::format based log proxy.

AlexVinS 9 years ago
parent
commit
d6178d0bb5
3 changed files with 50 additions and 2 deletions
  1. 48 0
      include/vstd/LogFormat.h
  2. 1 1
      lib/logging/CLogger.h
  3. 1 1
      lib/spells/CSpellHandler.cpp

+ 48 - 0
include/vstd/LogFormat.h

@@ -12,9 +12,57 @@
 
 namespace vstd
 {
+	namespace detail
+	{
+		template <typename T>
+		void makeFormat(boost::format & fmt, T t)
+		{
+			fmt % t;
+		}
+
+		template <typename T, typename ... Args>
+		void makeFormat(boost::format & fmt, T t, Args ... args)
+		{
+			fmt % t;
+			makeFormat(fmt, args...);
+		}
+	}
+
+    template<typename Logger, typename ... Args>
+    void logFormat(Logger * logger, ELogLevel::ELogLevel level, const std::string & format, Args ... args)
+    {
+		boost::format fmt(format);
+		detail::makeFormat(fmt, args...);
+		logger->log(level, fmt.str());
+    }
+
+    template<typename Logger, typename ... Args>
+    void logErrorFormat(Logger * logger, const std::string & format, Args ... args)
+    {
+    	logFormat(logger, ELogLevel::ERROR, format, args...);
+    }
+
+    template<typename Logger, typename ... Args>
+    void logWarnFormat(Logger * logger, const std::string & format, Args ... args)
+    {
+    	logFormat(logger, ELogLevel::WARN, format, args...);
+    }
+
+    template<typename Logger, typename ... Args>
+    void logInfoFormat(Logger * logger, const std::string & format, Args ... args)
+    {
+    	logFormat(logger, ELogLevel::INFO, format, args...);
+    }
+
     template<typename Logger, typename ... Args>
     void logDebugFormat(Logger * logger, const std::string & format, Args ... args)
     {
+    	logFormat(logger, ELogLevel::DEBUG, format, args...);
+    }
 
+    template<typename Logger, typename ... Args>
+    void logTraceFormat(Logger * logger, const std::string & format, Args ... args)
+    {
+    	logFormat(logger, ELogLevel::TRACE, format, args...);
     }
 }

+ 1 - 1
lib/logging/CLogger.h

@@ -104,7 +104,7 @@ public:
 	CLoggerStream warnStream() const;
 	CLoggerStream errorStream() const;
 
-	inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
+	void log(ELogLevel::ELogLevel level, const std::string & message) const;
 
 	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)
 	{
-		logGlobal->errorStream() << __FUNCTION__ << " invalid school level " << level;
+		vstd::logErrorFormat(logGlobal, "CSpell::getLevelInfo invalid school level %d", level);
 		throw new std::runtime_error("Invalid school level");
 	}