gz-cpp-util 1.3
A c++20 library containing various utilities
|
Manages printing messages to stdout and to logfiles. More...
#include <log.hpp>
Public Member Functions | |
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. More... | |
Log (LogCreateInfo &&createInfo) | |
Creates a log object, which can print messages to stdout and/or write them to a log file. More... | |
Logging | |
template<Logable... Args> | |
void | log (Args &&... args) |
Logs a message. More... | |
template<Logable... Args> | |
void | clog (const std::vector< Color > &colors, Args &&... args) |
Log a message in a certain color. More... | |
template<Logable... Args> | |
void | operator() (Args &&... args) |
Logs a message. Overload for convenience, same behavior as log() | |
template<Logable... Args> | |
void | error (Args &&... args) |
Log an error. More... | |
template<Logable... Args> | |
void | warning (Args &&... args) |
Log a warning. More... | |
Logging at different levels | |
template<Logable... Args> | |
void | log0 (Args &&... args) |
Enabled with LOG_LEVEL_0 or higher. | |
template<Logable... Args> | |
void | log1 (Args &&... args) |
Enabled with LOG_LEVEL_1 or higher. | |
template<Logable... Args> | |
void | log2 (Args &&... args) |
Enabled with LOG_LEVEL_2 or higher. | |
template<Logable... Args> | |
void | log3 (Args &&... args) |
Enabled with LOG_LEVEL_3 or higher. | |
template<Logable... Args> | |
void | clog0 (const std::vector< Color > &colors, Args &&... args) |
Enabled with LOG_LEVEL_0 or higher. | |
template<Logable... Args> | |
void | clog1 (const std::vector< Color > &colors, Args &&... args) |
Enabled with LOG_LEVEL_1 or higher. | |
template<Logable... Args> | |
void | clog2 (const std::vector< Color > &colors, Args &&... args) |
Enabled with LOG_LEVEL_2 or higher. | |
template<Logable... Args> | |
void | clog3 (const std::vector< Color > &colors, Args &&... args) |
Enabled with LOG_LEVEL_3 or higher. | |
Private Member Functions | |
template<util::Stringy T, Logable... Args> requires (!util::Stringy<T>) | |
void | vlog (const char *appendChars, T &&t, Args &&... args) |
Log anything that can be appendend to std::string. More... | |
template<ConvertibleToString T, Logable... Args> requires (!util::Stringy<T>) | |
void | vlog (const char *appendChars, T &&t, Args &&... args) |
Log anything where toString exists. | |
void | vlog (const char *appendChars) |
End for the recursion. | |
void | init () |
Private Attributes | |
std::vector< std::string > | logLines |
Where the lines are stored. | |
std::vector< std::string::size_type > | argsBegin |
Used during log: string views into the single substrings in logLines[currentLine]. | |
size_t | iter = 0 |
The current position in logLines. | |
bool | showLog |
Color | prefixColor |
std::string | prefix |
Writing to file | |
unsigned int | writeToFileAfterLines |
bool | clearLogfileOnRestart |
std::string | logFile |
Absolute path to the logfile. | |
bool | storeLog |
void | writeLog () |
Time | |
bool | showTime |
Color | timeColor |
char | time [LOG_TIMESTAMP_CHAR_COUNT] |
Stores the current time in yyyy-mm-dd hh:mm:ss format. | |
void | getTime () |
Store the current time in yyyy-mm-dd hh:mm:ss format in time member. | |
Manages printing messages to stdout and to logfiles.
Log uses concepts to determine if a type is logable and how it should be logged. See the documentation for concept Logable for more details.
If you want your custom data type to be logable, write an overload for gz::toString()
Log can use a static mutex for thread safety. To use this feature, you have to #define LOG_MULTITHREAD
before including log.hpp
. Note that log uses the default std::cout buffer, so you should make sure it is not being used while logging something.
The logs can be written to a logfile, which can be specified in the constructor. The logs are not continuously written to the logfile, since file operations are expensive. Instead, the log is stored in memory and written to the file if a certain number of lines is reached, which you can specify in the constructor. If you want the log to be continuously written to the file, set writeAfterLines
to 1.
There are 4 different log levels (0-3), where the lower ones include the higher ones. To set the log level to X
, where X
is one of {0, 1, 2, 3}, define #define LOG_LEVEL_X
before including log.hpp
. You can then use logX or clogX.
If logX function log level is lower than the set log level, the function call will be a noop and thus optimized away be the compiler.
You can think of:
LOG_LEVEL_0
as "trace"LOG_LEVEL_1
as "debug"LOG_LEVEL_2
as "info"LOG_LEVEL_3
as "error/important"gz::Log::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.
By creating multiple instances with different parameters, logs can be easily turned on/off for different usages.
The overload using LogCreateInfo might be more clear, so I recommend using that.
gz::Log::Log | ( | LogCreateInfo && | createInfo | ) |
Creates a log object, which can print messages to stdout and/or write them to a log file.
By creating multiple instances with different parameters, logs can be easily turned on/off for different usages.
void gz::Log::clog | ( | const std::vector< Color > & | colors, |
Args &&... | args | ||
) |
Log a message in a certain color.
The message will look like this: <time>: <prefix>: <message0> <message1>... where time will be white, prefix in prefixColor, and messageI in colors[I]. If there are less colors than message arguments, the last color is used for all remaining messages.
args | Any number of arguments that satisfy concept Logable |
colors | Vector of colors, where the nth color refers to the nth arg |
|
inline |
Log an error.
Prints the message with a red "Error: " prefix. The message will look like this: <time>: <prefix>: Error: <message> where time will be white, prefix in prefixColor, Error in red and message white.
args | Any number of arguments that satisfy concept Logable |
void gz::Log::log | ( | Args &&... | args | ) |
Logs a message.
Depending on the settings of the log instance, the message will be printed to stdout and/or written to the logfile. The current date and time is placed before the message. The message will look like this: <time>: <prefix>: <message> where time will be white, prefix in prefixColor and message white.
args | Any number of arguments that satisfy concept Logable |
|
private |
|
inline |
Log a warning.
Prints the message with a yellow "Warning: " prefix. The message will look like this: <time>: <prefix>: Warning: <message> where time will be white, prefix in prefixColor, Warning in yellow and message white.
args | Any number of arguments that satisfy concept Logable |
|
private |
When iter reaches writeToFileAfterLines, write log to file