olp-cpp-sdk  1.22.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
olp::logging::MessageFormatter Class Reference

Specifies how messages are formatted. More...

#include <MessageFormatter.h>

Classes

struct  Element
 An element to print out in the final formatted message. More...
 

Public Types

enum class  ElementType {
  String , Level , Tag , Message ,
  File , Line , Function , FullFunction ,
  Time , TimeMs , ThreadId , ContextValue
}
 The type of the element to print out. More...
 
enum class  Timezone { Local , Utc }
 The timezone used to print timestamps. More...
 
using LevelNameMap = std::array< std::string, levelCount >
 Maps the log level to its name. More...
 

Public Member Functions

 MessageFormatter ()
 The default constructor. More...
 
 MessageFormatter (std::vector< Element > elements, LevelNameMap levelNameMap=defaultLevelNameMap(), Timezone timezone=Timezone::Local)
 Creates the MessageFormatter instance with the elements, level name mapping, and timezone. More...
 
 MessageFormatter (const MessageFormatter &)=default
 The default copy constructor.
 
MessageFormatteroperator= (const MessageFormatter &)=default
 The default copy assignment operator.
 
 MessageFormatter (MessageFormatter &&other) noexcept
 The move constructor overload.
 
MessageFormatteroperator= (MessageFormatter &&other) noexcept
 The default move assignment operator overload.
 
const std::vector< Element > & getElements () const
 Gets the elements for the format. More...
 
MessageFormattersetElements (std::vector< Element > elements)
 Sets the elements for the format. More...
 
const LevelNameMapgetLevelNameMap () const
 Gets the level name map. More...
 
MessageFormattersetLevelNameMap (LevelNameMap map)
 Sets the level name map. More...
 
Timezone getTimezone () const
 Gets the timezone for timestamps. More...
 
MessageFormattersetTimezone (Timezone timezone)
 Sets the timezone for timestamps. More...
 
std::string format (const LogMessage &message) const
 Formats a log message. More...
 

Static Public Member Functions

static const LevelNameMapdefaultLevelNameMap ()
 Gets the default level name map. More...
 
static MessageFormatter createDefault ()
 Creates the default message formatter. More...
 

Detailed Description

Specifies how messages are formatted.

It provides a common way to declare any message format, which can be re-used across appenders that utilize this class.

Example: if you want the message to be set to "LOG: level tag - file:line [time] message" where the file is limited to 30 characters (cutting off on the left), the line always prints up to 5 characters, and time is set to HH:MM in UTC time:

MessageFormatter formatter(
{
MessageFormatter::Element(MessageFormatter::ElementType::String,
"LOG: "), MessageFormatter::Element(MessageFormatter::ElementType::Level, "%s
"), MessageFormatter::Element(MessageFormatter::ElementType::Tag, "%s - "),
MessageFormatter::Element(MessageFormatter::ElementType::File, "%s:",
-30), MessageFormatter::Element(MessageFormatter::ElementType::Line, "%5u "),
MessageFormatter::Element(MessageFormatter::ElementType::Time,
"[%H:%M] "),
MessageFormatter::Element(MessageFormatter::ElementType::Message)
@ Message
The log message. It is formatted as a string.
@ Level
The log level for the message. It is formatted as a string.
static const LevelNameMap & defaultLevelNameMap()
Gets the default level name map.
MessageFormatter()
The default constructor.
Definition: MessageFormatter.h:315
@ Utc
Prints time in the UTC standard.

Member Typedef Documentation

◆ LevelNameMap

using olp::logging::MessageFormatter::LevelNameMap = std::array<std::string, levelCount>

Maps the log level to its name.

Casts the log level enum value to size_t to use as an index. Off is not a valid name.

Member Enumeration Documentation

◆ ElementType

The type of the element to print out.

Enumerator
String 

The string literal.

Level 

The log level for the message. It is formatted as a string.

Tag 

The tag for the log component. It is formatted as a string. This element is omitted if the tag is null or an empty string.

Message 

The log message. It is formatted as a string.

File 

The file that generated the message. It is formatted as a string.

Line 

The line in the file where the message was logged. It is formatted as an unsigned integer.

Function 

The function that generated the message. It is formatted as a string.

FullFunction 

The fully qualified function that generated the message. It is formatted as a string.

Time 

The timestamp of the message. It is formatted as a time using strftime().

TimeMs 

The millisecond portion of the timestamp. It is formatted as an unsigned integer.

ThreadId 

The thread ID of the thread that generated the message. It is formatted as an unsigned long.

ContextValue 

A key/value literal from LogContext; 'format' is the key to look up.

◆ Timezone

The timezone used to print timestamps.

Enumerator
Local 

Prints time in the local time standard.

Utc 

Prints time in the UTC standard.

Constructor & Destructor Documentation

◆ MessageFormatter() [1/2]

olp::logging::MessageFormatter::MessageFormatter ( )
inline

The default constructor.

The element list is empty, the level name map is set to default, and the timezone is set to local.

◆ MessageFormatter() [2/2]

olp::logging::MessageFormatter::MessageFormatter ( std::vector< Element elements,
LevelNameMap  levelNameMap = defaultLevelNameMap(),
Timezone  timezone = Timezone::Local 
)
inlineexplicit

Creates the MessageFormatter instance with the elements, level name mapping, and timezone.

Parameters
elementsThe elements for the formatted message.
levelNameMapThe level name map.
timezoneThe timezone for timestamps. Defaults to local time.

Member Function Documentation

◆ createDefault()

static MessageFormatter olp::logging::MessageFormatter::createDefault ( )
static

Creates the default message formatter.

Format: "level tag - message".

◆ defaultLevelNameMap()

static const LevelNameMap& olp::logging::MessageFormatter::defaultLevelNameMap ( )
static

Gets the default level name map.

Returns
The level name map.

◆ format()

std::string olp::logging::MessageFormatter::format ( const LogMessage message) const

Formats a log message.

Parameters
messageThe message to format.
Returns
The formatted message.

◆ getElements()

const std::vector< MessageFormatter::Element > & olp::logging::MessageFormatter::getElements ( ) const
inline

Gets the elements for the format.

Returns
The elements.

◆ getLevelNameMap()

const MessageFormatter::LevelNameMap & olp::logging::MessageFormatter::getLevelNameMap ( ) const
inline

Gets the level name map.

Returns
The level name map.

◆ getTimezone()

MessageFormatter::Timezone olp::logging::MessageFormatter::getTimezone ( ) const
inline

Gets the timezone for timestamps.

Returns
The timezone.

◆ setElements()

MessageFormatter & olp::logging::MessageFormatter::setElements ( std::vector< Element elements)
inline

Sets the elements for the format.

Parameters
elementsThe elements.

◆ setLevelNameMap()

MessageFormatter & olp::logging::MessageFormatter::setLevelNameMap ( LevelNameMap  map)
inline

Sets the level name map.

Parameters
mapThe level name map.

◆ setTimezone()

MessageFormatter & olp::logging::MessageFormatter::setTimezone ( Timezone  timezone)
inline

Sets the timezone for timestamps.

Parameters
timezoneThe timezone.

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