-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.h
More file actions
251 lines (214 loc) · 9.12 KB
/
Copy pathlogger.h
File metadata and controls
251 lines (214 loc) · 9.12 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* ************************************************************************
* Copyright (c) 2025, Dhanush H V. All rights reserved.
* Licensed under the MIT License. See the LICENSE file for more details
* *************************************************************************
*/
#pragma once
#ifndef LOGGER_H
#define LOGGER_H
#include <iostream>
#include <fstream>
#include <chrono>
#include <map>
#include <stdexcept> // Required for std::runtime_error
// Handle file system for specific platform
#ifdef _WIN32
#include <direct.h>
#define MKDIR(path) _mkdir(path)
#else
#include <sys/stat.h>
#define MKDIR(path) mkdir(path, 0777)
#endif
// Logger namespace with utilities
namespace LOGGER {
// Define logger colors
namespace COLOR {
const std::string RED = "\x1B[31m";
const std::string GREEN = "\x1B[32m";
const std::string YELLOW = "\x1B[33m";
const std::string ORANGE = "\x1B[33m";
const std::string BLUE = "\x1B[34m";
const std::string MAGENTA = "\x1B[35m";
const std::string CYAN = "\x1B[36m";
const std::string WHITE = "\x1B[37m";
const std::string RESET = "\x1B[0m";
}
// Different text styles
namespace TEXT {
const std::string BOLD = "\x1B[1m";
const std::string UNDERLINE = "\x1B[4m";
const std::string BLINK = "\x1B[5m";
const std::string REVERSE = "\x1B[7m";
}
// Define the different types of log levels
namespace LEVEL {
const std::string DEBUG = "DEBUG";
const std::string INFO = "INFO";
const std::string WARNING = "WARNING";
const std::string ERROR = "ERROR";
const std::string FATAL = "FATAL";
const std::string MESSAGE = "MESSAGE";
const std::string UNKNOWN = "UNKNOWN";
}
}
// Logger level handler
class LevelHandler {
public:
std::string level;
std::string color;
int count;
LevelHandler(
const std::string &level,
const std::string &color,
const int count = 0
) {
this->level = level;
this->color = color;
this->count = count;
}
};
class Logger {
protected:
std::string name;
std::string root_dir;
std::string file_name;
std::ofstream log_file;
std::chrono::system_clock::time_point start_time;
std::chrono::system_clock::time_point end_time;
std::map<std::string, LevelHandler> handler;
bool debug;
int log_count;
public:
Logger(const std::string name, const std::string &root_dir, const std::string &file_name, const bool debug = false) {
this->name = name;
this->root_dir = root_dir;
this->file_name = file_name;
this->debug = debug;
this->start_time = std::chrono::system_clock::now();
this->end_time = std::chrono::system_clock::now();
this->log_count = 0;
// Set the default levels
this->handler.insert(std::pair(LOGGER::LEVEL::DEBUG, LevelHandler(LOGGER::LEVEL::DEBUG, LOGGER::COLOR::GREEN, 0)));
this->handler.insert(std::pair(LOGGER::LEVEL::INFO, LevelHandler(LOGGER::LEVEL::INFO, LOGGER::COLOR::CYAN, 0)));
this->handler.insert(std::pair(LOGGER::LEVEL::WARNING, LevelHandler(LOGGER::LEVEL::WARNING, LOGGER::COLOR::YELLOW, 0)));
this->handler.insert(std::pair(LOGGER::LEVEL::ERROR, LevelHandler(LOGGER::LEVEL::ERROR, LOGGER::COLOR::ORANGE, 0)));
this->handler.insert(std::pair(LOGGER::LEVEL::FATAL, LevelHandler(LOGGER::LEVEL::FATAL, LOGGER::COLOR::RED, 0)));
this->handler.insert(std::pair(LOGGER::LEVEL::MESSAGE, LevelHandler(LOGGER::LEVEL::MESSAGE, LOGGER::COLOR::BLUE, 0)));
this->handler.insert(std::pair(LOGGER::LEVEL::UNKNOWN, LevelHandler(LOGGER::LEVEL::UNKNOWN, LOGGER::COLOR::WHITE, 0)));
// Create the logger folder
MKDIR(root_dir.c_str());
// Create the logger log file
if (this->root_dir[this->root_dir.size() - 1] == '/' ||
this->root_dir[this->root_dir.size() - 1] == '\\'
) {
this->log_file.open(this->root_dir + file_name);
} else {
this->log_file.open(this->root_dir + "/" + file_name);
}
// Check if the log file was opened successfully
if (!this->log_file.is_open()) {
throw std::runtime_error("Failed to open log file at: " + (this->root_dir[this->root_dir.size() - 1] == '/' || this->root_dir[this->root_dir.size() - 1] == '\\' ? this->root_dir : this->root_dir + "/") + file_name);
}
// update the status of logger
this->log_file << ">>> Logger " << this->name << " initiated at " << std::chrono::system_clock::now() << " <<<\n";
std::cout << LOGGER::COLOR::CYAN << ">>> Logger " << this->name << " initiated at " << std::chrono::system_clock::now() << " <<<" << LOGGER::COLOR::RESET << "\n";
}
// Destructor to ensure exit_logger is called
~Logger() {
if (is_active()) {
exit_logger();
}
}
// Check if the logger is active or running
bool is_active() const {
return this->log_file.is_open();
}
// Get the root dir of the logger
std::string get_root_dir() const {
return this->root_dir;
}
// Get the file name of logger
std::string get_file_name() const {
return this->file_name;
}
// Get the time of when logger started
std::chrono::system_clock::time_point get_start_time() const {
return this->start_time;
}
// Get the time of when logger did exit
std::chrono::system_clock::time_point get_end_time() const {
return this->end_time;
}
// Get the total log count
int get_log_count() const {
return this->log_count;
}
// Get the log count of specific log level
int get_log_level_count(const std::string &level) const {
if (this->handler.contains(level)) {
return this->handler.at(level).count;
}
throw std::invalid_argument("[ERROR]: Invalid logger level: " + level);
}
// Get the logger handler
std::map<std::string, LevelHandler> get_handler() const {
return this->handler;
}
// Add the main log method
void log(const std::string &message, const std::string &level = LOGGER::LEVEL::INFO, const bool debug_once = false) {
if (!is_active()) {
std::cerr << "[ERROR]: Logger " << this->name << " not active\n";
exit(EXIT_FAILURE);
}
if (this->debug || debug_once) {
print_log(message, level);
}
// Check for invalid log level before accessing handler
if (this->handler.find(level) == this->handler.end()) {
// To prevent recursive error logging if this itself is part of an error log,
// we print to cerr and throw a different, more specific error.
// Or, simply throw, assuming this method won't be called with invalid levels internally.
// For now, let's throw a runtime_error.
// A more advanced strategy might log this specific error to a default/emergency logger
// or use a predefined "UNKNOWN" level if we want to record the attempt.
// However, throwing is safest to indicate misuse of the API.
throw std::runtime_error("Invalid log level used: " + level);
}
this->log_file << std::chrono::system_clock::now() << " " << level << " " << message << "\n";
this->handler.at(level).count++; // Now safe to use .at() due to the check above, or use find result.
this->log_count++;
}
// Exit logger if any active logger are running
void exit_logger() {
if (!this->is_active()) {
std::cerr << "NO ACTIVE LOGGER EXIST\n";
return;
}
this->end_time = std::chrono::system_clock::now();
this->log_file << ">>> Logger " << this->name << " exited at " << this->end_time << " <<<\n";
this->log_file.close();
std::cout << LOGGER::COLOR::CYAN << ">>> Exited Logger " << this->name << " at " << this->end_time << " <<<" << LOGGER::COLOR::RESET << "\n";
}
// Display all the log levels for better DevEx
// Print the log in a colored format
static void print_log(const std::string &message, const std::string& level) {
std::string color;
if (level == LOGGER::LEVEL::INFO)
color = LOGGER::COLOR::CYAN;
else if (level == LOGGER::LEVEL::WARNING)
color = LOGGER::COLOR::YELLOW;
else if (level == LOGGER::LEVEL::ERROR)
color = LOGGER::COLOR::ORANGE;
else if (level == LOGGER::LEVEL::FATAL)
color = LOGGER::COLOR::RED;
else if (level == LOGGER::LEVEL::MESSAGE)
color = LOGGER::COLOR::BLUE;
else if (level == LOGGER::LEVEL::DEBUG)
color = LOGGER::COLOR::GREEN;
else
color = LOGGER::COLOR::WHITE;
std::cout << color << std::chrono::system_clock::now() << " " << level << " " << message << LOGGER::COLOR::RESET << "\n";
}
};
#endif //LOGGER_H