forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogbuffer.cpp
More file actions
47 lines (39 loc) · 1.19 KB
/
Copy pathlogbuffer.cpp
File metadata and controls
47 lines (39 loc) · 1.19 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
#include "logbuffer.hpp"
#include <ctime>
#include <deque>
#include <mutex>
namespace
{
std::mutex g_log_mutex;
std::deque<LogEntry> g_log_lines;
constexpr std::size_t MaxLogLines = 512;
}
void pkgi_log_buffer_append(LogLevel level, const char* line)
{
if (!line)
return;
std::string text(line);
while (!text.empty() &&
(text.back() == '\n' || text.back() == '\r'))
{
text.pop_back();
}
// Prepend HH:MM:SS [LEVL] timestamp from system clock
char ts[10];
std::time_t now = std::time(nullptr);
std::tm* tm_now = std::localtime(&now);
std::strftime(ts, sizeof(ts), "%H:%M:%S", tm_now);
const char* lvl = level == LogLevel::Error ? "ERR "
: level == LogLevel::Warn ? "WARN"
: "INFO";
text = std::string(ts) + " [" + lvl + "] " + text;
std::lock_guard<std::mutex> lock(g_log_mutex);
g_log_lines.push_back({level, std::move(text)});
while (g_log_lines.size() > MaxLogLines)
g_log_lines.pop_front();
}
std::vector<LogEntry> pkgi_log_buffer_snapshot()
{
std::lock_guard<std::mutex> lock(g_log_mutex);
return {g_log_lines.begin(), g_log_lines.end()};
}