gz-cpp-util 1.3
A c++20 library containing various utilities
gz::Log Class Reference

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.
 

Detailed Description

Manages printing messages to stdout and to logfiles.

Logable types

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()

Thread safety

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.

Logfile

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.

Loglevels

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"
Note
operator()(), log(), clog(), warning() and error() are always 'on', regardless of which (if any) log level is defined.
Todo:

Exception policies

Use own ostream and not std::cout

Make colors cross platform

Constructor & Destructor Documentation

◆ Log() [1/2]

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.

Note
Colors will only be shown when written to stdout, not in the logfile.
Deprecated:
Use the overload using the LogCreateInfo struct

◆ Log() [2/2]

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.

Member Function Documentation

◆ clog()

template<Logable... Args>
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.

Parameters
argsAny number of arguments that satisfy concept Logable
colorsVector of colors, where the nth color refers to the nth arg

◆ error()

template<Logable... Args>
void gz::Log::error ( Args &&...  args)
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.

Parameters
argsAny number of arguments that satisfy concept Logable

◆ log()

template<Logable... Args>
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.

Parameters
argsAny number of arguments that satisfy concept Logable

◆ vlog()

template<ConvertibleToString T, Logable... Args>
requires (!util::Stringy<T>)
void gz::Log::vlog ( const char *  appendChars,
T &&  t,
Args &&...  args 
)
private

Log anything that can be appendend to std::string.

Log anything where toString exists.

◆ warning()

template<Logable... Args>
void gz::Log::warning ( Args &&...  args)
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.

Parameters
argsAny number of arguments that satisfy concept Logable

Member Data Documentation

◆ writeToFileAfterLines

unsigned int gz::Log::writeToFileAfterLines
private

When iter reaches writeToFileAfterLines, write log to file


The documentation for this class was generated from the following files: