-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLog.h
268 lines (233 loc) · 8.14 KB
/
Log.h
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// Created by fred on 05/10/16.
//
#ifndef FRLOG_H
#define FRLOG_H
#include <fstream>
#include <atomic>
#include <iostream>
#include <vector>
#include <algorithm>
#include <filesystem>
template<typename Test, template<typename...> class Ref>
struct is_specialization : std::false_type {};
template<template<typename...> class Ref, typename... Args>
struct is_specialization<Ref<Args...>, Ref>: std::true_type {};
class Log
{
public:
//Log importance levels
enum Level
{
info = 0,
warn = 1,
crit = 2,
count = 3,
};
enum End
{
end = 0,
};
//Disable copying/moving and whatnot
void operator=(const Log &l)=delete;
Log(const Log &l)=delete;
Log(const Log &&l)=delete;
/*!
* Initialises the logging class
*
* @param log_directory The directory to create logs in. If it doesn't exist then it will be created.
* @param replicate_to_stdout Should the log be repliacted to the standard output too?
* @param max_log_size Maximum log size in bytes before rotating. Default 1MiB. 0 = unlimited.
* @param log_retention Maximum log retention in iterations
* @return True on successful initialisation, false on failure
*/
bool init(std::string log_directory_, uint64_t max_log_size_ = 0x100000, uint64_t log_retention_ = 7, bool replicate_to_stdout = true)
{
log_directory = std::move(log_directory_);
stdout_replication = replicate_to_stdout;
max_log_size = max_log_size_;
log_retention = log_retention_;
if(log_directory.empty() || (log_directory.back() != '/' && log_directory.back() != '\\'))
{
log_directory += '/';
}
//Create logs directory
if(!std::filesystem::exists(log_directory))
{
if(!std::filesystem::create_directory(log_directory))
{
std::cout << "Failed to create '" << log_directory << "' directory. Exiting." << std::endl;
return false;
}
}
//Open a new log file
logpath = log_directory + suggest_log_filename();
logstream.open(logpath);
if(!logstream.is_open())
{
std::cout << "Failed to open " << logpath << " for logging!" << std::endl;
return false;
}
*this << " -- Logging initialised -- " << Log::end;
//Delete old logs
purge_old_logs();
return true;
}
//Required overloads for the '<<' operator
template<typename T>
inline Log &operator<<(const T &data)
{
if constexpr (is_specialization<T, std::atomic>())
{
*this << data.load();
}
else if constexpr (std::is_same<T, Level>())
{
//Spinlock using atomic flag, the inline asm is used to pause slightly so we don't waste too many CPU cycles
while(lock.test_and_set(std::memory_order_acquire))
asm volatile("pause\n": : :"memory");
static std::string log_levels[] = {"Info", "Warn", "Crit"}; //String log levels
commit_log("[" + get_current_timestamp() + " " + log_levels[data] + "]: "); //data = log level enum
}
else if constexpr (std::is_same<T, End>())
{
//End log and release lock
commit_log("\n", true);
lock.clear(std::memory_order_release);
}
else
{
commit_log(data);
}
return *this;
}
/*!
* Flush the log out stream
*/
void flush()
{
logstream.flush();
}
/*!
* Gets the logger instance
*
* @return The logger instance
*/
static Log &get_instance()
{
static Log logger;
return logger;
}
private:
//Constructor/destructor
Log()
: lock(ATOMIC_FLAG_INIT),
max_log_size(0),
log_retention(0),
stdout_replication(true)
{
}
/*!
* Returns the current timestamp in a string format
*
* @return The timestamp in format YYYY-MM-DD HH:MM:SS
*/
std::string get_current_timestamp()
{
//Get the current timestamp
std::time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm now = *std::localtime(&time);
now.tm_mon++; //Months start at 0 instead of 1 for some reason. But not days or years.
//Convert the required bits of information into strings, padding with 0's where needed
std::string day = now.tm_mday > 9 ? std::to_string(now.tm_mday) : "0" + std::to_string(now.tm_mday);
std::string month = now.tm_mon > 9 ? std::to_string(now.tm_mon) : "0" + std::to_string(now.tm_mon);
std::string year = now.tm_year > 9 ? std::to_string(1900 + now.tm_year) : "0" + std::to_string(1900 + now.tm_year);
std::string hour = now.tm_hour > 9 ? std::to_string(now.tm_hour) : "0" + std::to_string(now.tm_hour);
std::string min = now.tm_min > 9 ? std::to_string(now.tm_min) : "0" + std::to_string(now.tm_min);
std::string sec = now.tm_sec > 9 ? std::to_string(now.tm_sec) : "0" + std::to_string(now.tm_sec);
//Format data and return
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
}
/*!
* Generates a suggested log name, with characters
* safe on both Linux and Windows.
*
* @return A suitable log name
*/
std::string suggest_log_filename()
{
std::string timestamp = get_current_timestamp();
std::replace(timestamp.begin(), timestamp.end(), ':', '-');
return timestamp;
}
/*!
* Deletes logs which are out of retention
*/
void purge_old_logs()
{
if(log_retention != 0)
{
//Get a list of logs, and if there's more than retention allows, delete some
std::vector<std::filesystem::directory_entry> log_files;
auto iter = std::filesystem::directory_iterator(log_directory);
for(auto &file : iter)
{
log_files.emplace_back(file);
}
if(log_files.size() < log_retention)
return;
//Sort files by name, oldest should be first
std::sort(log_files.begin(), log_files.end(), [&](const auto &f1, const auto &f2){
return f1.path() < f2.path();
});
//Erase oldest ones
for(size_t a = 0; a < log_files.size() - log_retention; a++)
{
std::filesystem::remove(log_files[a]);
}
}
}
/*!
* Internal logging function, it's what all of the '<<' overloads feed into.
* It will automatically cycle the log if it gets too big.
*
* @param data The data to log
* @param may_cycle True of the logs should be cycled if over limit
*/
template<typename T>
inline void commit_log(const T &data, bool may_cycle = false)
{
//Print it
if(stdout_replication)
std::cout << data;
logstream << data;
//Cycle logs if needed
if(may_cycle && max_log_size != 0 && (size_t)logstream.tellp() > max_log_size)
do_log_cycle();
}
/*!
* Closes the current log,
* opens a new one.
*/
void do_log_cycle()
{
//Close current log file and open new one
logstream.close();
logpath = log_directory + suggest_log_filename();
logstream.open(logpath);
if(!logstream.is_open())
throw std::runtime_error("Failed to open new log file: " + logpath);
//Delete old logs if needbe
purge_old_logs();
}
std::atomic_flag lock; //Atomic flag used for thread safe logging using a spinlock
std::ofstream logstream; //Out output stream
std::string log_directory; //Directory to create logs in, ends in a /
std::string logpath; //The log filepath
uint64_t max_log_size; //Max allowed log size in bytes. 0 means infinite.
uint64_t log_retention; //Number of log iterations to keep
bool stdout_replication; //Should logs also be sent to stdout?
};
//Set frlog define shortcut
#define frlog Log::get_instance()
#endif //FRLOG_H