Botcraft 26.3
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
3
4#include <fstream>
5#include <iomanip>
6#include <iostream>
7
8namespace Botcraft
9{
11
13 {
14 filename = "";
15 file_buffer = "";
17 last_time_logged = std::chrono::steady_clock::now();
18 log_func = [this](const std::string& s)
19 {
20 std::cout << s;
21 std::cout.flush();
22 };
23 }
24
26 {
27 std::lock_guard<std::mutex> lock(mutex);
28 if (filename != "")
29 {
30 std::ofstream outfile(filename, std::ios::out | std::ios::app);
31 outfile << file_buffer;
32 }
33 file_buffer = "";
34 }
35
37 {
38 static Logger test;
39 return test;
40 }
41
42 void Logger::Log(const std::string& s, const LogLevel level)
43 {
44 std::lock_guard<std::mutex> lock(mutex);
45 log_func(s);
46
47 file_buffer += s;
48 auto now = std::chrono::steady_clock::now();
49 // Only write to file every 5 seconds, but always flush errors and above
50 if (level >= LogLevel::Error || now - last_time_logged > std::chrono::seconds(5))
51 {
52 if (filename != "")
53 {
54 std::ofstream outfile(filename, std::ios::out | std::ios::app);
55 outfile << file_buffer;
56 }
57 file_buffer = "";
58 last_time_logged = now;
59 }
60 }
61
62 void Logger::SetFilename(const std::string& s)
63 {
64 std::lock_guard<std::mutex> lock(mutex);
65 filename = s;
66 }
67
69 {
70 log_level = l;
71 }
72
74 {
75 return log_level;
76 }
77
78 void Logger::SetLogFunc(const std::function<void(const std::string&)>& f)
79 {
80 std::lock_guard<std::mutex> lock(mutex);
81 log_func = f;
82 }
83
84 std::stringstream Logger::GetDate() const
85 {
86 auto now = std::chrono::system_clock::now();
87 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
88
89 std::time_t t = std::chrono::system_clock::to_time_t(now);
90 std::tm tm;
91#ifdef _WIN32
92 localtime_s(&tm, &t);
93#else
94 localtime_r(&t, &tm);
95#endif
96
97 std::stringstream s;
98 s << '[' << std::put_time(&tm, "%Y-%m-%d %H:%M:%S")
99 << '.' << std::setfill('0') << std::setw(3) << ms.count() << ']';
100
101 return s;
102 }
103
104 void Logger::RegisterThread(const std::string& name)
105 {
106 std::lock_guard<std::mutex> lock(thread_mutex);
107 thread_names[std::this_thread::get_id()] = name;
108
109 thread_local struct ThreadExiter
110 {
111 ~ThreadExiter()
112 {
113 Logger::GetInstance().UnregisterThread(std::this_thread::get_id());
114 }
115 } exiter;
116 }
117
118 void Logger::RegisterThread(const std::thread::id id, const std::string& name)
119 {
120 std::lock_guard<std::mutex> lock(thread_mutex);
121 thread_names[id] = name;
122 }
123
124 std::string Logger::GetThreadName(const std::thread::id id)
125 {
126 std::lock_guard<std::mutex> lock(thread_mutex);
127 const auto it = thread_names.find(id);
128 return it == thread_names.end() ? "" : it->second;
129 }
130
131 void Logger::UnregisterThread(const std::thread::id id)
132 {
133 std::lock_guard<std::mutex> lock(thread_mutex);
134 thread_names.erase(id);
135 }
136}
#define DEFINE_ENUM_STRINGIFYER_RANGE(Enum, min_value, max_value)
void UnregisterThread(const std::thread::id id)
Remove a thread from the map.
Definition Logger.cpp:131
std::atomic< LogLevel > log_level
Definition Logger.hpp:115
void RegisterThread(const std::string &name)
Register the current thread in the map.
Definition Logger.cpp:104
LogLevel GetLogLevel() const
Definition Logger.cpp:73
std::stringstream GetDate() const
Definition Logger.cpp:84
static Logger & GetInstance()
Definition Logger.cpp:36
std::chrono::steady_clock::time_point last_time_logged
Definition Logger.hpp:118
std::string GetThreadName(const std::thread::id id)
Get the name of a given thread.
Definition Logger.cpp:124
std::string file_buffer
Definition Logger.hpp:119
void SetLogLevel(const LogLevel l)
Definition Logger.cpp:68
void Log(const std::string &s, const LogLevel level=LogLevel::Info)
Definition Logger.cpp:42
std::unordered_map< std::thread::id, std::string > thread_names
Definition Logger.hpp:122
std::mutex thread_mutex
Definition Logger.hpp:121
std::string filename
Definition Logger.hpp:114
std::function< void(const std::string &)> log_func
Definition Logger.hpp:116
void SetFilename(const std::string &s)
Definition Logger.cpp:62
void SetLogFunc(const std::function< void(const std::string &)> &f)
Definition Logger.cpp:78
std::mutex mutex
Definition Logger.hpp:113