-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.h
More file actions
51 lines (41 loc) · 1.12 KB
/
logger.h
File metadata and controls
51 lines (41 loc) · 1.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
// Logger.h
#pragma once
#include <string>
#include <fstream>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <thread>
enum class LogLevel {
INFO,
WARNING,
ERROR
};
class Logger {
public:
static Logger& getInstance();
void init(const std::string& filename = "logs.txt", LogLevel minLevel = LogLevel::INFO);
void log(const std::string& message, LogLevel level = LogLevel::INFO);
void shutdown();
~Logger();
private:
Logger();
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
void workerThread();
void rotateLogFile();
std::string timestamp();
std::string getTimestampForFilename();
std::size_t getCurrentFileSize();
std::string colorForLevel(LogLevel level);
static constexpr std::size_t MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
std::mutex mtx;
std::condition_variable cv;
std::queue<std::string> logQueue;
std::ofstream outFile;
std::thread worker;
bool exitFlag = false;
bool isShutdown = false;
LogLevel currentLogLevel = LogLevel::INFO;
std::string logFileName = "logs.txt";
};