-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts_logger.cpp
More file actions
104 lines (87 loc) · 2.75 KB
/
ts_logger.cpp
File metadata and controls
104 lines (87 loc) · 2.75 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
#include "ts_logger.h"
namespace utilities
{
struct logger::impl
{
std::ofstream log_file;
log_level level = log_level::default_level;
std::queue<std::string> message_queue;
std::mutex queue_mutex;
std::condition_variable cv;
std::jthread writer_thread;
std::atomic<bool> shutdown_requested{false};
impl()
{
log_file.open("test_log.txt", std::ios::app);
writer_thread = std::jthread([this] { writer_loop(); });
}
~impl()
{
shutdown();
writer_thread.join();
log_file.close();
}
void writer_loop()
{
while (true) {
std::unique_lock<std::mutex> lock(queue_mutex);
cv.wait(lock, [this] { return !message_queue.empty() || shutdown_requested.load(); });
if (shutdown_requested.load() && message_queue.empty()) {
break;
}
std::string message = std::move(message_queue.front());
message_queue.pop();
lock.unlock();
log_file << message << std::endl;
std::cout << "Writing " << message << " to the log file." << std::endl;
}
}
void shutdown()
{
{
std::lock_guard<std::mutex> lock(queue_mutex);
shutdown_requested.store(true);
}
cv.notify_one();
}
std::string get_current_time_string()
{
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
std::string format_message(log_level level, const std::string& msg)
{
std::stringstream ss;
ss << "[" << get_current_time_string() << "] [" << static_cast<int>(level) << "] " << msg;
return ss.str();
}
};
logger::logger() : pimpl(std::make_unique<impl>()) {}
logger::~logger() = default;
logger& logger::get_instance()
{
static logger instance;
return instance;
}
void logger::set_level(log_level level)
{
pimpl->level = level;
}
log_level logger::get_level() const
{
return pimpl->level;
}
void logger::write_impl(log_level level, const std::string& msg)
{
std::lock_guard<std::mutex> lock(pimpl->queue_mutex);
pimpl->message_queue.push(pimpl->format_message(level, msg));
pimpl->cv.notify_one();
}
void logger::shutdown()
{
pimpl->shutdown();
}
}