MADARA  3.4.1
madara::logger::Logger Class Reference

A multi-threaded logger for logging to one or more destinations. More...

#include <Logger.h>

Public Member Functions

 Logger (bool log_to_stderr=true)
 Constructor. More...
 
 ~Logger ()
 Destructor. More...
 
void add_file (const std::string &filename)
 Adds a file to the logger. More...
 
void add_syslog (void)
 Adds the system log. More...
 
void add_term (void)
 Adds terminal to logger outputs. More...
 
void clear (void)
 Clears all log targets. More...
 
int get_level (void)
 Gets the maximum logging detail level. More...
 
std::string get_tag (void)
 Gets the tag used for syslogs. More...
 
void log (int level, const char *message,...)
 Logs a message to all available loggers. More...
 
void set_level (int level)
 Sets the maximum logging detail level. More...
 
void set_tag (const std::string &tag)
 Sets the tag used for syslogs (e.g. More...
 
void set_timestamp_format (const std::string &format="%x %X: ")
 Sets timestamp format. More...
 

Static Public Member Functions

static int get_thread_level (void)
 Fetches thread local storage value for thread level. More...
 

Private Types

typedef std::vector< FILE * > FileVectors
 guard for access and changes More...
 

Private Member Functions

std::string search_and_insert_custom_tstamp (const std::string &buf, const std::string &ts_str)
 Set thread local storage value for hertz. More...
 
std::string strip_custom_tstamp (const std::string in_str, const std::string ts_str)
 Set thread local storage value for hertz. More...
 

Private Attributes

FileVectors files_
 list of all log outputs More...
 
std::atomic< int > level_
 the maximum detail level for logging More...
 
const char * MADARA_GET_TIME_MGT_ = "%MGT"
 constants for the thread local keystrings More...
 
const char * MADARA_THREAD_HERTZ_ = "%MTZ"
 key string cosntant for hertz value for local thread More...
 
const char * MADARA_THREAD_NAME_ = "%MTN"
 key string constant for thread name for local thread More...
 
MADARA_LOCK_TYPE mutex_
 mutex for changes More...
 
bool syslog_added_
 tracks whether the system log has been added More...
 
std::string tag_
 the tag used for logging to system logs More...
 
bool term_added_
 tracks whether terminal output has been added More...
 
std::string timestamp_format_
 the timestamp format. More...
 

Detailed Description

A multi-threaded logger for logging to one or more destinations.

A default logger is provided via madara::logger::global_logger in GlobalLogger.h

Definition at line 164 of file Logger.h.

Member Typedef Documentation

◆ FileVectors

typedef std::vector<FILE*> madara::logger::Logger::FileVectors
private

guard for access and changes

vector of file handles

Definition at line 308 of file Logger.h.

Constructor & Destructor Documentation

◆ Logger()

madara::logger::Logger::Logger ( bool  log_to_stderr = true)

Constructor.

Parameters
log_to_stderrif true, log to stderr by default

Definition at line 19 of file Logger.cpp.

◆ ~Logger()

madara::logger::Logger::~Logger ( )

Destructor.

Definition at line 33 of file Logger.cpp.

Member Function Documentation

◆ add_file()

void madara::logger::Logger::add_file ( const std::string &  filename)
inline

Adds a file to the logger.

Parameters
filenamethe file to open and add to writing

Definition at line 12 of file Logger.inl.

◆ add_syslog()

void madara::logger::Logger::add_syslog ( void  )
inline

Adds the system log.

Definition at line 111 of file Logger.inl.

◆ add_term()

void madara::logger::Logger::add_term ( void  )
inline

Adds terminal to logger outputs.

Definition at line 104 of file Logger.inl.

◆ clear()

void madara::logger::Logger::clear ( void  )
inline

Clears all log targets.

Definition at line 118 of file Logger.inl.

◆ get_level()

int madara::logger::Logger::get_level ( void  )
inline

Gets the maximum logging detail level.

Returns
the maximum level of log detail

Definition at line 99 of file Logger.inl.

◆ get_tag()

std::string madara::logger::Logger::get_tag ( void  )
inline

Gets the tag used for syslogs.

Returns
the tag used for any system logging

Definition at line 44 of file Logger.inl.

◆ get_thread_level()

int madara::logger::Logger::get_thread_level ( void  )
inlinestatic

Fetches thread local storage value for thread level.

Returns
the log level of the local thread

Definition at line 85 of file Logger.inl.

◆ log()

void madara::logger::Logger::log ( int  level,
const char *  message,
  ... 
)

Logs a message to all available loggers.

Parameters
levelthe logging level
See also
LogLevels
Parameters
messagethe message to log

Android seems to not handle printf arguments correctly as best I can tell

Prepare string to log for the timestamp prefix.

First, search and replace the custom key string for local thread. The return value is a copy of the potential prefix with the custom key string data embedded the number of times it was used.

Prepare string to write into copy of the message buffer.

Search and insert corresponding data for each of the custom key string.

Process the normal message buffer and write into final copy to return.

Definition at line 123 of file Logger.cpp.

◆ search_and_insert_custom_tstamp()

std::string madara::logger::Logger::search_and_insert_custom_tstamp ( const std::string &  buf,
const std::string &  ts_str 
)
private

Set thread local storage value for hertz.

Parameters
buf- message buffer that holds the proprietary key key string.
ts_str- The key string to identify which values to replace
Returns
the string containing the message data with key string data minus the actual key string

Take the ts_str (key string) and repeatedly search the incoming message buf.

If not found, npos is returned and exit the loop gracefully.

If found, compare via the series of if-else-if statements. Swap out the computed value with the key string using the copied message buffer

insert mgt text here get_time returns nsecs. need to convert into seconds.

insert this value into the buffer

convert the double into string format that can easily be written into the copy of the message buffer.

Definition at line 46 of file Logger.cpp.

◆ set_level()

void madara::logger::Logger::set_level ( int  level)
inline

Sets the maximum logging detail level.

Parameters
levelthe maximum level of log detail

Definition at line 39 of file Logger.inl.

◆ set_tag()

void madara::logger::Logger::set_tag ( const std::string &  tag)
inline

Sets the tag used for syslogs (e.g.

Android Log)

Parameters
tagthe tag to use for any system logging

Definition at line 92 of file Logger.inl.

◆ set_timestamp_format()

void madara::logger::Logger::set_timestamp_format ( const std::string &  format = "%x %X: ")
inline

Sets timestamp format.

Uses strftime for formatting time.

Parameters
formatthe format of the timestamp. See C++ strftime definition for common usage.

Definition at line 136 of file Logger.inl.

◆ strip_custom_tstamp()

std::string madara::logger::Logger::strip_custom_tstamp ( const std::string  in_str,
const std::string  ts_str 
)
private

Set thread local storage value for hertz.

Parameters
in_str- input string to find potentially multiple instances of the key string
ts_str- actual key string to find.
Returns
the string containing the message data with key string data

Definition at line 38 of file Logger.cpp.

Member Data Documentation

◆ files_

FileVectors madara::logger::Logger::files_
private

list of all log outputs

Definition at line 314 of file Logger.h.

◆ level_

std::atomic<int> madara::logger::Logger::level_
private

the maximum detail level for logging

Definition at line 317 of file Logger.h.

◆ MADARA_GET_TIME_MGT_

const char* madara::logger::Logger::MADARA_GET_TIME_MGT_ = "%MGT"
private

constants for the thread local keystrings

key string constant for clock seconds for local thread

Definition at line 334 of file Logger.h.

◆ MADARA_THREAD_HERTZ_

const char* madara::logger::Logger::MADARA_THREAD_HERTZ_ = "%MTZ"
private

key string cosntant for hertz value for local thread

Definition at line 340 of file Logger.h.

◆ MADARA_THREAD_NAME_

const char* madara::logger::Logger::MADARA_THREAD_NAME_ = "%MTN"
private

key string constant for thread name for local thread

Definition at line 337 of file Logger.h.

◆ mutex_

MADARA_LOCK_TYPE madara::logger::Logger::mutex_
mutableprivate

mutex for changes

Definition at line 311 of file Logger.h.

◆ syslog_added_

bool madara::logger::Logger::syslog_added_
private

tracks whether the system log has been added

Definition at line 323 of file Logger.h.

◆ tag_

std::string madara::logger::Logger::tag_
private

the tag used for logging to system logs

Definition at line 326 of file Logger.h.

◆ term_added_

bool madara::logger::Logger::term_added_
private

tracks whether terminal output has been added

Definition at line 320 of file Logger.h.

◆ timestamp_format_

std::string madara::logger::Logger::timestamp_format_
private

the timestamp format.

Definition at line 329 of file Logger.h.


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