forked from OpenApoc/OpenApoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.h
89 lines (78 loc) · 3.7 KB
/
logger.h
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
#pragma once
#include "library/strings.h"
#include "library/strings_format.h"
#if defined(_MSC_VER) && _MSC_VER > 1400
#include <sal.h>
#endif
#define XSTR(s) STR(s)
#define STR(s) #s
#if defined(_MSC_VER)
#define NORETURN_FUNCTION __declspec(noreturn)
#else
#define NORETURN_FUNCTION __attribute__((noreturn))
#endif
/* The logger is global state as we want it to be available even if the framework hasn't been
* successfully initialised */
namespace OpenApoc
{
/* MSVC doesn't ahve __PRETTY_FUNCTION__ but __FUNCSIG__? */
// FIXME: !__GNUC__ isn't the same as MSVC
#ifndef __GNUC__
#define LOGGER_PREFIX __FUNCSIG__
#define PRINTF_ATTR(fmt_string_no, vararg_start_no)
#else
#define LOGGER_PREFIX __PRETTY_FUNCTION__
#define PRINTF_ATTR(fmt_string_no, vararg_start_no) \
__attribute__((format(printf, fmt_string_no, vararg_start_no)))
#endif
enum class LogLevel : int
{
Nothing = 0,
Error = 1,
Warning = 2,
Info = 3,
Debug = 3,
};
void Log(LogLevel level, UString prefix, const UString &text);
NORETURN_FUNCTION void _logAssert(UString prefix, UString string, int line, UString file);
// All logger output will be UTF8
}; // namespace OpenApoc
#define LogAssert(X) \
do \
{ \
if (!(X)) \
OpenApoc::_logAssert(LOGGER_PREFIX, STR(X), __LINE__, __FILE__); \
} while (0)
//#ifndef __ANDROID__
#if defined(__GNUC__)
// GCC has an extension if __VA_ARGS__ are not supplied to 'remove' the precending comma
#define LogDebug(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Debug, OpenApoc::UString(LOGGER_PREFIX), \
::OpenApoc::format(f, ##__VA_ARGS__))
#define LogInfo(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Info, OpenApoc::UString(LOGGER_PREFIX), \
::OpenApoc::format(f, ##__VA_ARGS__))
#define LogWarning(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Warning, OpenApoc::UString(LOGGER_PREFIX), \
::OpenApoc::format(f, ##__VA_ARGS__))
#define LogError(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Error, OpenApoc::UString(LOGGER_PREFIX), \
::OpenApoc::format(f, ##__VA_ARGS__))
#else
// At least msvc automatically removes the comma
#define LogDebug(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Debug, LOGGER_PREFIX, ::OpenApoc::format(f, __VA_ARGS__))
#define LogInfo(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Info, LOGGER_PREFIX, ::OpenApoc::format(f, __VA_ARGS__))
#define LogWarning(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Warning, LOGGER_PREFIX, ::OpenApoc::format(f, __VA_ARGS__))
#define LogError(f, ...) \
OpenApoc::Log(OpenApoc::LogLevel::Error, LOGGER_PREFIX, ::OpenApoc::format(f, __VA_ARGS__))
#endif
//#else
#if 0
#define LogInfo(f, ...)
#define LogWarning(f, ...)
#define LogError(f, ...)
#endif
//#endif