File: CLoggerBase.h

package info (click to toggle)
vcmi 0.99%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 10,264 kB
  • ctags: 16,826
  • sloc: cpp: 121,945; objc: 248; sh: 193; makefile: 28; python: 13; ansic: 9
file content (134 lines) | stat: -rw-r--r-- 2,744 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * 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
	};

	inline std::string to_string(ELogLevel level)
	{
		switch (level) {
			case NOT_SET:
				return "not set";
			case TRACE:
				return "trace";
			case DEBUG:
				return "debug";
			case INFO:
				return "info";
			case WARN:
				return "warn";
			case ERROR:
				return "error";
			default:
				return std::string("invalid (") + std::to_string(level) + ")";
		}
	}
}

namespace vstd
{
	class DLL_LINKAGE CLoggerBase
	{
	public:
		virtual ~CLoggerBase(){};

		virtual void log(ELogLevel::ELogLevel level, const std::string & message) const = 0;

		template<typename T, typename ... Args>
		void log(ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args) const
		{
			boost::format fmt(format);
			makeFormat(fmt, t, args...);
			log(level, fmt.str());
		}

		/// Log methods for various log levels
		inline void error(const std::string & message) const
		{
			log(ELogLevel::ERROR, message);
		};

		template<typename T, typename ... Args>
		void error(const std::string & format, T t, Args ... args) const
		{
			log(ELogLevel::ERROR, format, t, args...);
		}

		inline void warn(const std::string & message) const
		{
			log(ELogLevel::WARN, message);
		};

		template<typename T, typename ... Args>
		void warn(const std::string & format, T t, Args ... args) const
		{
			log(ELogLevel::WARN, format, t, args...);
		}

		inline void info(const std::string & message) const
		{
			log(ELogLevel::INFO, message);
		};

		template<typename T, typename ... Args>
		void info(const std::string & format, T t, Args ... args) const
		{
			log(ELogLevel::INFO, format, t, args...);
		}

		inline void debug(const std::string & message) const
		{
			log(ELogLevel::DEBUG, message);
		};


		template<typename T, typename ... Args>
		void debug(const std::string & format, T t, Args ... args) const
		{
			log(ELogLevel::DEBUG, format, t, args...);
		}

		inline void trace(const std::string & message) const
		{
			log(ELogLevel::TRACE, message);
		};

		template<typename T, typename ... Args>
		void trace(const std::string & format, T t, Args ... args) const
		{
			log(ELogLevel::TRACE, format, t, args...);
		}

	private:
		template <typename T>
		void makeFormat(boost::format & fmt, T t) const
		{
			fmt % t;
		}

		template <typename T, typename ... Args>
		void makeFormat(boost::format & fmt, T t, Args ... args) const
		{
			fmt % t;
			makeFormat(fmt, args...);
		}
	};
}