-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.h
135 lines (98 loc) · 4.57 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
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
//
// Created by scherkesov on 11.10.2020.
//
#ifndef GFB_LOGGER_H
#define GFB_LOGGER_H
#include <iostream>
#include <string>
#include <stdexcept>
#include <chrono>
#include <ctime>
#include <vector>
#include <map>
#include <fstream>
#include <stdio.h>
#ifdef _WIN32
#define COLOR_TEXT_RED 12
#define COLOR_TEXT_GREEN 10
#define COLOR_TEXT_YELLOW 14
#define COLOR_TEXT_MAGENTA 13
#else
#define COLOR_TEXT_RED 31
#define COLOR_TEXT_GREEN 32
#define COLOR_TEXT_YELLOW 33
#define COLOR_TEXT_MAGENTA 35
#endif
enum LogLevel {
LOG_LEVEL_DEBUG = 1,
LOG_LEVEL_INFO = 2,
LOG_LEVEL_WARNING = 3,
LOG_LEVEL_ERROR = 4,
LOG_LEVEL_CRITICAL = 5,
LOG_LEVEL_FATAL = 6,
};
class AbstractLoggerTarget {
public:
explicit AbstractLoggerTarget(LogLevel level);
bool applicable(LogLevel level);
virtual void write(LogLevel level, const std::string &message) = 0;
void enablePrint() { DisablePrint = false; }
void disablePrint() { DisablePrint = true; }
protected:
LogLevel mLevel;
bool DisablePrint = false;
};
class ConsoleLoggerTarget : public AbstractLoggerTarget {
public:
explicit ConsoleLoggerTarget(LogLevel level);
void write(LogLevel level, const std::string &message) override;
};
class FileLoggerTarget : public AbstractLoggerTarget {
public:
FileLoggerTarget(const std::string &filename, LogLevel level);
void write(LogLevel level, const std::string &message) override;
private:
std::ofstream mOut;
};
class MCVCLoggerTarget : public AbstractLoggerTarget {
public:
MCVCLoggerTarget(LogLevel level);
void write(LogLevel level, const std::string &message) override;
};
class CppLogger {
public:
static void registerTarget(AbstractLoggerTarget *t);
static void print(char const *function, char const *file, long line,
LogLevel level, const std::string &message);
static void DisablePrintAll();
static void EnablePrintAll();
private:
static std::vector<AbstractLoggerTarget *> mTargets;
static std::string getDateTime();
};
std::string strPrintf(const char* message, ...);
#define MacroStr(x) #x
#define MacroStr2(x) MacroStr(x)
#define ToDo(desc) __pragma(message(__FILE__ "(" MacroStr2(__LINE__) "): " #desc))
#ifdef __GNUC__
#define Logger_Debug(message) CppLogger::print(__PRETTY_FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_DEBUG), (message));
#define Logger_Info(message) CppLogger::print(__PRETTY_FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_INFO), (message));
#define Logger_Warn(message) CppLogger::print(__PRETTY_FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_WARNING), (message));
#define Logger_Error(message) CppLogger::print(__PRETTY_FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_ERROR), (message));
#define Logger_Critical(message) CppLogger::print(__PRETTY_FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_CRITICAL), (message));
#define Logger_Fatal(message) CppLogger::print(__PRETTY_FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_FATAL), (message));
#else
#define Logger_Debug(message) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_DEBUG), (message));
#define Logger_Info(message) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_INFO), (message));
#define Logger_Warn(message) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_WARNING), (message));
#define Logger_Error(message) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_ERROR), (message));
#define Logger_Critical(message) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_CRITICAL), (message));
#define Logger_Fatal(message) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_FATAL), (message));
#define Logger_Debug_F(message, ...) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_DEBUG), strPrintf(message, __VA_ARGS__).c_str());
#define Logger_Info_F(message, ...) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_INFO), strPrintf(message, __VA_ARGS__).c_str());
#define Logger_Warn_F(message, ...) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_WARNING), strPrintf(message, __VA_ARGS__).c_str());
#define Logger_Error_F(message, ...) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_ERROR), strPrintf(message, __VA_ARGS__).c_str());
#define Logger_Critical_F(message, ...) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_CRITICAL), strPrintf(message, __VA_ARGS__).c_str());
#define Logger_Fatal_F(message, ...) CppLogger::print(__FUNCTION__, __FILE__, __LINE__, (LOG_LEVEL_FATAL), strPrintf(message, __VA_ARGS__).c_str());
#endif
#endif //GFB_LOGGER_H