-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.h
87 lines (76 loc) · 2.47 KB
/
logging.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
#include <iostream>
#include <string>
#include <functional>
#include <ctime>
#include <iomanip>
// Define logging levels
enum class LogLevel {
Debug,
Info,
Warning,
Error
};
// Define a logging class
class Logger {
public:
Logger(const std::string& level = "Info", const std::string& min_level = "Info") :
m_level(stringToLevel(level)),
m_min_level(stringToLevel(min_level))
{}
virtual ~Logger() = default;
// Set the logging level
void setLogLevel(const std::string& level) {
m_level = stringToLevel(level);
}
// Set the minimum logging level
void setMinLogLevel(const std::string& level) {
m_min_level = stringToLevel(level);
}
// Log a message at the specified level and return a function object with a new logging level
std::function<Logger&(const std::string&)> log(LogLevel level) {
return [this, level](const std::string& message) -> Logger& {
if (level >= m_level && level >= m_min_level) {
std::time_t now = std::time(nullptr);
std::tm timeinfo = *std::localtime(&now);
std::cout << "[" << levelToString(level) << "] " << std::put_time(&timeinfo, "%c")
<< " [DEBUG " << levelToString(m_level) << "] " << message << std::endl;
}
return *this;
};
}
// Convert a logging level to a string
static std::string levelToString(LogLevel level) {
switch (level) {
case LogLevel::Debug:
return "DEBUG";
case LogLevel::Info:
return "INFO";
case LogLevel::Warning:
return "WARNING";
case LogLevel::Error:
return "ERROR";
default:
return "UNKNOWN";
}
}
private:
LogLevel m_level;
LogLevel m_min_level;
// Convert a string to a logging level
LogLevel stringToLevel(const std::string& level) {
std::string lowercase_level = level;
std::transform(lowercase_level.begin(), lowercase_level.end(), lowercase_level.begin(),
[](unsigned char c) { return std::tolower(c); });
if (lowercase_level == "debug") {
return LogLevel::Debug;
} else if (lowercase_level == "info") {
return LogLevel::Info;
} else if (lowercase_level == "warning") {
return LogLevel::Warning;
} else if (lowercase_level == "error") {
return LogLevel::Error;
} else {
return LogLevel::Info;
}
}
};