-
Notifications
You must be signed in to change notification settings - Fork 29
/
logger.hpp
117 lines (102 loc) · 2.46 KB
/
logger.hpp
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
#ifndef LOGGER_HPP
#define LOGGER_HPP
#include <iostream>
#include <cstdarg>
#include <memory>
#include <chrono>
enum class LogLevel
{
FATAL = 0, // printed and stops program
DEBUG = 1,
WARNING = 2,
INFO = 3,
VERBOSE = 4,
};
class AbstractLogger
{
public:
AbstractLogger(std::ostream* target = &std::cerr) : out(target), log_level(LogLevel::INFO), log_time(true) {}
virtual ~AbstractLogger() {}
virtual void Log(LogLevel level, const char* format, va_list ap);
virtual void DoLog(LogLevel level, const char* format, va_list ap) {}
void SetLogTarget(std::ostream* stream)
{
out = stream;
}
void SetLogLevel(LogLevel level)
{
log_level = level;
}
void SetLogTime(bool logging_time)
{
log_time = logging_time;
}
protected:
std::ostream* out;
LogLevel log_level;
bool log_time;
};
class Logger : public AbstractLogger
{
public:
virtual void DoLog(LogLevel level, const char* format, va_list ap);
};
extern std::unique_ptr<AbstractLogger> logger;
void SetLogger(AbstractLogger* logobj);
static inline void Log(LogLevel level, const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
logger->Log(level, format, argptr);
va_end(argptr);
}
static inline void Log(LogLevel level, const char* format, va_list arg)
{
logger->Log(level, format, arg);
}
static inline void FatalLog(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
Log(LogLevel::FATAL, format, argptr);
va_end(argptr);
}
static inline void DebugLog(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
Log(LogLevel::DEBUG, format, argptr);
va_end(argptr);
}
static inline void WarnLog(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
Log(LogLevel::WARNING, format, argptr);
va_end(argptr);
}
static inline void InfoLog(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
Log(LogLevel::INFO, format, argptr);
va_end(argptr);
}
static inline void VerboseLog(const char* format, ...)
{
va_list argptr;
va_start(argptr, format);
Log(LogLevel::VERBOSE, format, argptr);
va_end(argptr);
}
/** Object that only exists to print out start and end of event call in a function */
class EventLog
{
public:
EventLog(const char* function);
~EventLog();
private:
const char* func;
std::chrono::time_point<std::chrono::system_clock> startTime;
};
#endif