File: CLoggerBase.h

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (202 lines) | stat: -rw-r--r-- 5,082 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * 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

VCMI_LIB_NAMESPACE_BEGIN

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:
#ifdef NO_STD_TOSTRING
				return "invalid";
#else
				return std::string("invalid (") + std::to_string(level) + ")";
#endif
		}
	}
}

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

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

	/// Returns true if a debug/trace log message will be logged, false if not.
	/// Useful if performance is important and concatenating the log message is a expensive task.
	virtual bool isDebugEnabled() const = 0;
	virtual bool isTraceEnabled() const = 0;

	template<typename T, typename ... Args>
	void log(ELogLevel::ELogLevel level, const std::string & format, T t, Args ... args) const
	{
		if (getEffectiveLevel() <= level)
		{
			try
			{
				boost::format fmt(format);
				makeFormat(fmt, t, args...);
				log(level, fmt);
			}
			catch(...)
			{
				log(ELogLevel::ERROR, "Log formatting failed, format was:");
				log(ELogLevel::ERROR, format);
			}
		}
	}

	/// 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...);
	}
};

/// RAII class for tracing the program execution.
/// It prints "Leaving function XYZ" automatically when the object gets destructed.
class DLL_LINKAGE CTraceLogger
{
public:
	CTraceLogger(const CLoggerBase * logger, const std::string & beginMessage, const std::string & endMessage);
	CTraceLogger(const CTraceLogger & other) = delete;
	~CTraceLogger();

private:
	const CLoggerBase * logger;
	std::string endMessage;
};

}//namespace vstd


/// Macros for tracing the control flow of the application conveniently. If the LOG_TRACE macro is used it should be
/// the first statement in the function. Logging traces via this macro have almost no impact when the trace is disabled.
///
#define RAII_TRACE(logger, onEntry, onLeave)								\
	std::unique_ptr<vstd::CTraceLogger> ctl00 = logger->isTraceEnabled() ?	\
	std::make_unique<vstd::CTraceLogger>(logger, onEntry, onLeave) :		\
    std::unique_ptr<vstd::CTraceLogger>()

#define LOG_TRACE(logger) RAII_TRACE(logger,								\
		boost::str(boost::format("Entering %s.") % BOOST_CURRENT_FUNCTION),	\
		boost::str(boost::format("Leaving %s.") % BOOST_CURRENT_FUNCTION))


#define LOG_TRACE_PARAMS(logger, formatStr, params) RAII_TRACE(logger,		\
		boost::str(boost::format("Entering %s: " + std::string(formatStr) + ".") % BOOST_CURRENT_FUNCTION % params), \
		boost::str(boost::format("Leaving %s.") % BOOST_CURRENT_FUNCTION))


extern DLL_LINKAGE vstd::CLoggerBase * logGlobal;
extern DLL_LINKAGE vstd::CLoggerBase * logBonus;
extern DLL_LINKAGE vstd::CLoggerBase * logNetwork;
extern DLL_LINKAGE vstd::CLoggerBase * logAi;
extern DLL_LINKAGE vstd::CLoggerBase * logAnim;
extern DLL_LINKAGE vstd::CLoggerBase * logMod;
extern DLL_LINKAGE vstd::CLoggerBase * logRng;

VCMI_LIB_NAMESPACE_END