-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.hpp
130 lines (107 loc) · 3.8 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
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef __LOGGER_HPP__
#define __LOGGER_HPP__
//#include <benchmark/benchmark.h>
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdarg>
#include <vector>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <unistd.h>
#include <map>
#include <mutex>
namespace unit {
namespace log {
enum class LogLevel {
INFO,
DEBUG,
WARNING,
ERROR,
CUSTOM1,
CUSTOM2
};
class LogDefault {
private:
std::ofstream logFile;
std::string logFileName;
const std::size_t maxFileSize = 1024 * 1024; // 1 MB
std::mutex logMutex;
public:
LogDefault(const std::string &baseFileName) {
logFileName = baseFileName + "_" + std::to_string(getProcessId());
rotateLogFile();
}
~LogDefault() {
logFile.close();
}
void rotateLogFile() {
std::lock_guard<std::mutex> lock(logMutex); // Lock during file rotation
// std::time_t t = std::time(nullptr);
// std::stringstream ss;
// ss << std::put_time(std::gmtime(&t), "%Y%m%d%H%M%S");
// std::string timestamp = ss.str();
logFile.open(logFileName + /* "_" + timestamp + */ ".log");
}
template<typename... Args>
void log(const char *fmt, Args... args) {
log(LogLevel::DEBUG, fmt, args...);
}
void log(LogLevel level, const char *fmt, ...) {
std::lock_guard<std::mutex> lock(logMutex); // Lock during log write
if (logFile.tellp() > maxFileSize) {
rotateLogFile();
}
std::time_t t = std::time(nullptr);
char time_buf[100];
std::strftime(time_buf, sizeof time_buf, "%D %T", std::gmtime(&t));
va_list args1;
va_start(args1, fmt);
va_list args2;
va_copy(args2, args1);
std::vector<char> buf(1 + std::vsnprintf(nullptr, 0, fmt, args1));
va_end(args1);
std::vsnprintf(buf.data(), buf.size(), fmt, args2);
va_end(args2);
std::stringstream logMessage;
logMessage << time_buf << " [" << logLevelToString(level) << "]: " << buf.data();
logFile << logMessage.str() << std::endl;
}
private:
static std::string logLevelToString(LogLevel level) {
switch (level) {
case LogLevel::INFO:
return "info";
case LogLevel::DEBUG:
return "debug";
case LogLevel::WARNING:
return "warning";
case LogLevel::ERROR:
return "error";
default:
return "unknown";
}
}
static pid_t getProcessId() {
return getpid();
}
};
//const std::map<LogLevel, std::string> LogDefault::customLogLevelStrings = {
// {LogLevel::CUSTOM1, "custom1"},
// {LogLevel::CUSTOM2, "custom2"}
//};
//LogDefault logger("benchmark_log");
//void BM_WriteLogFile(benchmark::State& state) {
// // The actual benchmark loop
// for (auto _ : state) {
// logger.log(LogLevel::DEBUG, "Benchmark message: %d", state.iterations());
// }
//}
// Register the benchmark
//BENCHMARK(BM_WriteLogFile);
// Run the benchmark
//BENCHMARK_MAIN();
}
}
#endif // __LOGGER_HPP__