Serenity Logger
A very fast and effiecient logging library. This library currently can log to files, rotate files when logging, log to the terminal, and is aware if the terminal is being piped during logging.
serenity::targets::ColorConsole Class Reference

This class is in charge of logging to the terminal and supports color logging and the ability to log without color.
This class inherits from the TargetBase class for common logging functions and logging settings. More...

+ Inheritance diagram for serenity::targets::ColorConsole:

Public Member Functions

 ColorConsole ()
 Default constructor that will set the logger name to "Console_Logger". All other settings will be set to their defaults.
 
 ColorConsole (std::string_view name)
 Constructor that will set the logger name to the parameter name passed in. All other settings will be set to their defaults. More...
 
 ColorConsole (std::string_view name, std::string_view msgPattern)
 Constructor that will set the logger name to the parameter name passed in and set the format pattern via the parameter msgPattern passed in. All other settings will be set to their defaults. More...
 
 ~ColorConsole ()
 If the output wasn't directed to a terminal, will flush the contents to disk. More...
 
std::string_view GetMsgColor (LoggerLevel level)
 Returns the log level based color for the current message to use. More...
 
void SetMsgColor (LoggerLevel level, std::string_view color)
 Sets color specified for the log level specified. More...
 
void SetConsoleInterface (console_interface mode)
 Sets console mode. Console modes mirror standard outputs. More...
 
const console_interface ConsoleInterface ()
 Returns the current console mode being used.
 
void ColorizeOutput (bool colorize)
 Enables/Disables colored text output.
 
void SetOriginalColors ()
 Initializes the default colors to use for the log levels. More...
 
bool IsTerminalType ()
 Checks to see if output handle is referring to a terminal type device or not. More...
 
bool IsValidHandle ()
 Checks to see if output handle is valid and not empty. More...
 
void PrintMessage (std::string_view formatted) override
 Outputs the message (formatted) to the destination output device. More...
 
void SetFlushPolicy (serenity::experimental::Flush_Policy pPolicy)
 Sets the current policy in use to defer to the policy passed in from "pPolicy". More...
 
serenity::experimental::Flush_PolicyPolicy ()
 Returns the current policy in use.
 
const std::string LoggerName ()
 Returns the logger's name.
 
void SetPattern (std::string_view pattern)
 Calls the handle to the Message_Formatter's SetPattern( ) function. More...
 
void ResetPatternToDefault ()
 Resets the current format pattern in use to the default format pattern.
 
void SetLogLevel (LoggerLevel level)
 Sets the log level that messages should be logged at. More...
 
void WriteToBaseBuffer (bool fmtToBuf=true)
 Enables/Disables writing to a buffer. More...
 
bool isWriteToBuf ()
 Returns true if buffer writes are enabled and false if they are disabled.
 
std::string * Buffer ()
 Returns a pointer to the buffer container.
 
const LoggerLevel Level ()
 Returns the current log level setting (the threshold of whether to log a message or not).
 
void SetLoggerName (std::string_view name)
 Sets the name of the logger. More...
 
template<typename... Args>
void Trace (std::string_view msg, Args &&...args)
 Logs a message giving the message a LoggerLevel::trace setting. More...
 
template<typename... Args>
void Info (std::string_view msg, Args &&...args)
 Logs a message giving the message a LoggerLevel::info setting. More...
 
template<typename... Args>
void Debug (std::string_view msg, Args &&...args)
 Logs a message giving the message a LoggerLevel::debug setting. More...
 
template<typename... Args>
void Warn (std::string_view msg, Args &&...args)
 Logs a message giving the message a LoggerLevel::warning setting. More...
 
template<typename... Args>
void Error (std::string_view msg, Args &&...args)
 Logs a message giving the message a LoggerLevel::error setting. More...
 
template<typename... Args>
void Fatal (std::string_view msg, Args &&...args)
 Logs a message giving the message a LoggerLevel::fatal setting. More...
 

Protected Member Functions

virtual void PolicyFlushOn ()
 Virtual function that can be omitted if derived class has no need to implement and is used to handle how and when the derived target should flush its contents to disk. More...
 
msg_details::Message_FormatterMsgFmt ()
 Returns a pointer to the handle for the Message_Formatter class instance.
 
msg_details::Message_InfoMsgInfo ()
 Returns a pointer to the handle for the Message_Info class instance.
 

Detailed Description

For all Console Target Constructors:

  • will set the console mode to "console_interface::std_out"
  • Initializes the default colors to use for each message level
  • If output is a terminal and hasn't been redirected and if the output handle is valid, enables color output
  • Sets "ENABLE_VIRTUAL_TERMINAL_PROCESSING" flag if on Windows Platform

Constructor & Destructor Documentation

◆ ColorConsole() [1/2]

serenity::targets::ColorConsole::ColorConsole ( std::string_view  name)
explicit
Parameters
namethe name that the logger itself will use and be identified by

◆ ColorConsole() [2/2]

serenity::targets::ColorConsole::ColorConsole ( std::string_view  name,
std::string_view  msgPattern 
)
explicit
Parameters
namethe name that the logger itself will use and be identified by
msgPatternthe format pattern that determines how the prepended text will be displayed before the log message

◆ ~ColorConsole()

serenity::targets::ColorConsole::~ColorConsole ( )

If on Windows platform, this function will also reset the console mode to default in order to to clear "ENABLE_VIRTUAL_TERMINAL_PROCESSING" flag.

Member Function Documentation

◆ Debug()

template<typename... Args>
serenity::targets::TargetBase::Debug ( std::string_view  msg,
Args &&...  args 
)
inherited

Checks if the message should be logged via the log level threshold setting. If it isn't, immediately returns. If the message should be logged, this function will then perform a quick check on whether or not writes to the buffer were enabled and will write to the buffer if they were. If writes to the buffers weren't enabled, then PrintMessage( ) will be called and the derived target handles how this is implemented. After writing the message, follows the derived target's PolicyFlushOn( ) implementation

Template Parameters
msgThe message being passed in. Follows C++20's format library and libfmt's substitution model using "{}" to replace any arguments from the args parameter.
argsVariadic placeholder for any number of and any type of arguments to use in substituion.

◆ Error()

template<typename... Args>
serenity::targets::TargetBase::Error ( std::string_view  msg,
Args &&...  args 
)
inherited

Checks if the message should be logged via the log level threshold setting. If it isn't, immediately returns. If the message should be logged, this function will then perform a quick check on whether or not writes to the buffer were enabled and will write to the buffer if they were. If writes to the buffers weren't enabled, then PrintMessage( ) will be called and the derived target handles how this is implemented. After writing the message, follows the derived target's PolicyFlushOn( ) implementation

Template Parameters
msgThe message being passed in. Follows C++20's format library and libfmt's substitution model using "{}" to replace any arguments from the args parameter.
argsVariadic placeholder for any number of and any type of arguments to use in substituion.

◆ Fatal()

template<typename... Args>
serenity::targets::TargetBase::Fatal ( std::string_view  msg,
Args &&...  args 
)
inherited

Checks if the message should be logged via the log level threshold setting. If it isn't, immediately returns. If the message should be logged, this function will then perform a quick check on whether or not writes to the buffer were enabled and will write to the buffer if they were. If writes to the buffers weren't enabled, then PrintMessage( ) will be called and the derived target handles how this is implemented. After writing the message, follows the derived target's PolicyFlushOn( ) implementation

Template Parameters
msgThe message being passed in. Follows C++20's format library and libfmt's substitution model using "{}" to replace any arguments from the args parameter.
argsVariadic placeholder for any number of and any type of arguments to use in substituion.

◆ GetMsgColor()

std::string_view serenity::targets::ColorConsole::GetMsgColor ( LoggerLevel  level)
Parameters
levelthe level used to recieve the color code for that level. Logger levels are trace, info, debug, warning, error, and fatal.
Returns
The ansi color code for the level passed in

◆ Info()

template<typename... Args>
serenity::targets::TargetBase::Info ( std::string_view  msg,
Args &&...  args 
)
inherited

Checks if the message should be logged via the log level threshold setting. If it isn't, immediately returns. If the message should be logged, this function will then perform a quick check on whether or not writes to the buffer were enabled and will write to the buffer if they were. If writes to the buffers weren't enabled, then PrintMessage( ) will be called and the derived target handles how this is implemented. After writing the message, follows the derived target's PolicyFlushOn( ) implementation

Template Parameters
msgThe message being passed in. Follows C++20's format library and libfmt's substitution model using "{}" to replace any arguments from the args parameter.
argsVariadic placeholder for any number of and any type of arguments to use in substituion.

◆ IsTerminalType()

bool serenity::targets::ColorConsole::IsTerminalType ( )
Returns
True if output is terminal, false otherwise

◆ IsValidHandle()

bool serenity::targets::ColorConsole::IsValidHandle ( )
Returns
True if handle to output isn't a nullptr, or if on Windows, that the output handle isn't INVALID_HANDLE_VALUE. Returns false otherwise.

◆ PolicyFlushOn()

serenity::targets::TargetBase::PolicyFlushOn ( )
inlineprotectedvirtualinherited

Reimplemented in serenity::targets::FileTarget.

◆ PrintMessage()

void serenity::targets::ColorConsole::PrintMessage ( std::string_view  formatted)
overridevirtual

If output handle is valid, will write the message to the output. If color is enabled, will write the message in the color specified for the log level used if the output is to a terminal.
For windows, uses WriteConsole( ) unless the output is not a terminal (in which case, uses WriteFile( ) instead ).

Parameters
formattedThe actual message in its entirety to send to the output destination.

Implements serenity::targets::TargetBase.

◆ SetConsoleInterface()

void serenity::targets::ColorConsole::SetConsoleInterface ( console_interface  mode)

For Windows, sets "ENABLE_VIRTUAL_TERMINAL_PROCESSING". (If not defined, a macro is used to define this value). For any other platfrom, sets the standard output to use.
The console_interface values (std_out, std_err, std_log) represent std::out, std::err, and std::clog respectively.

Parameters
modeCan be one of the following: console_interface::std_out, console_interface::std_err, or console_interface::std_log

◆ SetFlushPolicy()

void serenity::targets::TargetBase::SetFlushPolicy ( serenity::experimental::Flush_Policy  pPolicy)
inherited
Parameters
pPolicyrefers to any settings that are added or changed by the user, including whether to flush always, never, or periodically.

On top of having options to never flush, always flush, and periodically flush, the Flush_Policy class includes the settings for whether flushing should occur based on a time-interval or log level if the periodical flushing option is enabled.

◆ SetLoggerName()

void serenity::targets::TargetBase::SetLoggerName ( std::string_view  name)
inherited
Parameters
namethe name that the logger itself will use and be identified by

◆ SetLogLevel()

void serenity::targets::TargetBase::SetLogLevel ( LoggerLevel  level)
inherited

For example, if "SetLogLevel(LoggerLevel::Error);" is used, then no messages below LoggerLevel::Error will be logged, however once a Fatal or Error message is made, then they would be logged to the output destination

Parameters
level- the logger level threshold that will determine if a message should be logged or not

◆ SetMsgColor()

void serenity::targets::ColorConsole::SetMsgColor ( LoggerLevel  level,
std::string_view  color 
)
Parameters
levelThe logger level to bind the color code to from color variable
colorThe color code to bind to the level passed in. This color code is expected to be an ansi code.

◆ SetOriginalColors()

void serenity::targets::ColorConsole::SetOriginalColors ( )

The default colors used are as follows:

Logger Level Color Used
info Bright Green On Black
trace Bright White On Black
debug Bright Cyan On Black
warning Bright Yellow On Black
error Basic Red On Black
fatal Bright Yellow On Red
off Reset To Default Terminal Colors

◆ SetPattern()

void serenity::targets::TargetBase::SetPattern ( std::string_view  pattern)
inherited

Sets the format pattern variable and parses the format string for internal storage and usage of this pattern by initializing and moving the initialized formatter struct in charge of the respective flag to a Formatters container

Parameters
pattern- the format pattern to store. This pattern is what determines how the prepended text will be displayed before the log message

◆ Trace()

template<typename... Args>
serenity::targets::TargetBase::Trace ( std::string_view  msg,
Args &&...  args 
)
inherited

Checks if the message should be logged via the log level threshold setting. If it isn't, immediately returns. If the message should be logged, this function will then perform a quick check on whether or not writes to the buffer were enabled and will write to the buffer if they were. If writes to the buffers weren't enabled, then PrintMessage( ) will be called and the derived target handles how this is implemented. After writing the message, follows the derived target's PolicyFlushOn( ) implementation

Template Parameters
msgThe message being passed in. Follows C++20's format library and libfmt's substitution model using "{}" to replace any arguments from the args parameter.
argsVariadic placeholder for any number of and any type of arguments to use in substituion.

◆ Warn()

template<typename... Args>
serenity::targets::TargetBase::Warn ( std::string_view  msg,
Args &&...  args 
)
inherited

Checks if the message should be logged via the log level threshold setting. If it isn't, immediately returns. If the message should be logged, this function will then perform a quick check on whether or not writes to the buffer were enabled and will write to the buffer if they were. If writes to the buffers weren't enabled, then PrintMessage( ) will be called and the derived target handles how this is implemented. After writing the message, follows the derived target's PolicyFlushOn( ) implementation

Template Parameters
msgThe message being passed in. Follows C++20's format library and libfmt's substitution model using "{}" to replace any arguments from the args parameter.
argsVariadic placeholder for any number of and any type of arguments to use in substituion.

◆ WriteToBaseBuffer()

void serenity::targets::TargetBase::WriteToBaseBuffer ( bool  fmtToBuf = true)
inherited

When writing to the buffer, messages will be appended with the platform-specific end of line before being added to the buffer. When Flush( ) is called, if writing to the buffer was enabled, the buffer contents will now be written to the file and then flushed to disk. Disabled by default.

Parameters
fmtToBufthe value that controls whether or not buffer writes are enabled/disabled

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