30 constexpr unsigned int ARG_COUNT_RESERVE_COUNT = 6;
32 constexpr unsigned int LOG_TIMESTAMP_CHAR_COUNT = 22;
33 constexpr unsigned int LOG_POSTPREFIX_CHAR_COUNT = 2;
42 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE,
43 BO_BLACK, BO_RED, BO_GREEN, BO_YELLOW, BO_BLUE, BO_MAGENTA, BO_CYAN, BO_WHITE,
44 BG_BLACK, BG_RED, BG_GREEN, BG_YELLOW, BG_BLUE, BG_MAGENTA, BG_CYAN, BG_WHITE,
45 LI_RED, LI_GREEN, LI_YELLOW, LI_BLUE, LI_MAGENTA, LI_CYAN, LI_WHITE, LI_BLACK,
47 extern const char* COLORS[];
135 Log(std::string logfile=
"log.log",
bool showLog=
true,
bool storeLog=
true, std::string&& prefix=
"",
Color prefixColor=RESET,
bool showTime=
true,
Color timeColor=RESET,
bool clearLogfileOnRestart=
true,
unsigned int writeAfterLines=100);
163 void log(Args&&... args);
176 void clog(
const std::vector<Color>& colors, Args&&... args);
187 log(std::forward<Args>(args)...);
200 clog({RED, WHITE},
"Error:", std::forward<Args>(args)...);
213 clog({YELLOW, WHITE},
"Warning:", std::forward<Args>(args)...);
224 inline void log0(Args&&... args);
229 inline void log1(Args&&... args);
234 inline void log2(Args&&... args);
239 inline void log3(Args&&... args);
245 inline void clog0(
const std::vector<Color>& colors, Args&&... args);
250 inline void clog1(
const std::vector<Color>& colors, Args&&... args);
255 inline void clog2(
const std::vector<Color>& colors, Args&&... args);
260 inline void clog3(
const std::vector<Color>& colors, Args&&... args);
267 void vlog(
const char* appendChars, T&& t, Args&&... args);
274 void vlog(
const char* appendChars) {};
290 bool clearLogfileOnRestart;
308 char time[LOG_TIMESTAMP_CHAR_COUNT];
313#ifdef LOG_MULTITHREAD
315 static std::mutex mtx;
325#ifdef LOG_MULTITHREAD
339 vlog(
" ", std::forward<Args>(args)...);
355#ifdef LOG_MULTITHREAD
362 void Log::clog(
const std::vector<Color>& colors, Args&&... args) {
363#ifdef LOG_MULTITHREAD
377 vlog(
" ", std::forward<Args>(args)...);
387 size_t maxI = std::min(colors.size(),
argsBegin.size() - 2);
388 for (
size_t i = 0; i < maxI; i++) {
398#ifdef LOG_MULTITHREAD
405 void Log::vlog(
const char* appendChars, T&& t, Args&&... args) {
409 vlog(
" ", std::forward<Args>(args)...);
414 argsBegin.emplace_back(logLines[iter].size());
416 logLines[iter] += appendChars;
417 vlog(
" ", std::forward<Args>(args)...);
421 template<Logable... Args>
424 this->
log(std::forward<Args>(args)...);
431 this->
log(std::forward<Args>(args)...);
438 this->
log(std::forward<Args>(args)...);
445 this->
log(std::forward<Args>(args)...);
451 inline void Log::clog0(
const std::vector<Color>& colors, Args&&... args) {
453 this->
clog(colors, std::forward<Args>(args)...);
458 inline void Log::clog1(
const std::vector<Color>& colors, Args&&... args) {
460 this->
clog(colors, std::forward<Args>(args)...);
465 inline void Log::clog2(
const std::vector<Color>& colors, Args&&... args) {
467 this->
clog(colors, std::forward<Args>(args)...);
472 inline void Log::clog3(
const std::vector<Color>& colors, Args&&... args) {
474 this->
clog(colors, std::forward<Args>(args)...);
Manages printing messages to stdout and to logfiles.
Definition: log.hpp:124
std::string logFile
Absolute path to the logfile.
Definition: log.hpp:292
std::vector< std::string > logLines
Where the lines are stored.
Definition: log.hpp:279
void clog1(const std::vector< Color > &colors, Args &&... args)
Enabled with LOG_LEVEL_1 or higher.
Definition: log.hpp:458
void warning(Args &&... args)
Log a warning.
Definition: log.hpp:212
std::vector< std::string::size_type > argsBegin
Used during log: string views into the single substrings in logLines[currentLine].
Definition: log.hpp:281
void clog0(const std::vector< Color > &colors, Args &&... args)
Enabled with LOG_LEVEL_0 or higher.
Definition: log.hpp:451
void clog2(const std::vector< Color > &colors, Args &&... args)
Enabled with LOG_LEVEL_2 or higher.
Definition: log.hpp:465
void log3(Args &&... args)
Enabled with LOG_LEVEL_3 or higher.
Definition: log.hpp:443
void vlog(const char *appendChars)
End for the recursion.
Definition: log.hpp:274
void operator()(Args &&... args)
Logs a message. Overload for convenience, same behavior as log()
Definition: log.hpp:186
void getTime()
Store the current time in yyyy-mm-dd hh:mm:ss format in time member.
Definition: log.cpp:116
void error(Args &&... args)
Log an error.
Definition: log.hpp:199
void log2(Args &&... args)
Enabled with LOG_LEVEL_2 or higher.
Definition: log.hpp:436
void log1(Args &&... args)
Enabled with LOG_LEVEL_1 or higher.
Definition: log.hpp:429
void clog3(const std::vector< Color > &colors, Args &&... args)
Enabled with LOG_LEVEL_3 or higher.
Definition: log.hpp:472
void log(Args &&... args)
Logs a message.
Definition: log.hpp:324
Log(std::string logfile="log.log", bool showLog=true, bool storeLog=true, std::string &&prefix="", Color prefixColor=RESET, bool showTime=true, Color timeColor=RESET, bool clearLogfileOnRestart=true, unsigned int writeAfterLines=100)
Creates a log object, which can print messages to stdout and/or write them to a log file.
Definition: log.cpp:55
unsigned int writeToFileAfterLines
Definition: log.hpp:289
void vlog(const char *appendChars, T &&t, Args &&... args)
Log anything that can be appendend to std::string.
Definition: log.hpp:405
void log0(Args &&... args)
Enabled with LOG_LEVEL_0 or higher.
Definition: log.hpp:422
char time[LOG_TIMESTAMP_CHAR_COUNT]
Stores the current time in yyyy-mm-dd hh:mm:ss format.
Definition: log.hpp:308
void clog(const std::vector< Color > &colors, Args &&... args)
Log a message in a certain color.
Definition: log.hpp:362
size_t iter
The current position in logLines.
Definition: log.hpp:283
Any type where gz::toString(t) exists.
Definition: to_string.hpp:409
Define types that can be logged with Log.
Definition: log.hpp:60
same as std::string, std::string_view or const char*
Definition: to_string.hpp:26
Color
Colors to be used in Log::clog.
Definition: log.hpp:40
constexpr unsigned int LOG_RESERVE_STRING_SIZE
Reserve a string size for each string in logArray. Set to 0 if you do not want to reserve memory for ...
Definition: log.hpp:29
Create info for a Log object.
Definition: log.hpp:65
bool clearLogfileOnRestart
If true, clear the logfile when initializing the log. That means only the log of most recent run is s...
Definition: log.hpp:81
unsigned int writeAfterLines
Actually write the log to the logfile after so many lines. Must be at least 1.
Definition: log.hpp:83
std::string logfile
Absolute or relative path to the logfile.
Definition: log.hpp:67
bool storeLog
Wether to save the messages to the logfile.
Definition: log.hpp:71
bool showTime
Wether to prepend a timestamp to the message.
Definition: log.hpp:77
bool showLog
Wether to print the messages to stdout.
Definition: log.hpp:69
std::string prefix
A prefix that comes between the timestamp and the message. ": " is automatically appended to the pref...
Definition: log.hpp:73
Color prefixColor
The color of the prefix.
Definition: log.hpp:75
Color timeColor
The color of the timestamp.
Definition: log.hpp:79
Contains functions to convert types to string.
std::string toString(const T &s)
Declaration of toString in global namespace, so that concepts can use it.