elsa logging

Table of Contents

Logger

class elsa::Logger

Logging class for elsa purposes (wrapper around spdlog).

This class provides logging for the elsa library. It is a thin wrapper around the spdlog third party library, using the single-threaded versions of the sinks.

Author

Maximilian Hornung - initial version

Author

Tobias Lasser - rewrite

Logger::get(“name”) requests a logger (from spdlog) corresponding to “name”, which is used just as you would use spdlog directly, i.e. you can use the info(), debug(), warn() etc. routines. For more details on spdlog, please visit https://github.com/gabime/spdlog.

By default, Logger enables console logging. If requested, file logging can enabled in addition via addFileSink. Log levels are set via setLevel (again, they correspond to the spdlog log levels).

Public Types

enum LogLevel

available log levels (default: INFO), corresponding to those of spdlog

Values:

enumerator TRACE
enumerator DEBUG
enumerator INFO
enumerator WARN
enumerator ERR
enumerator CRITICAL
enumerator OFF

Public Static Functions

std::shared_ptr<spdlog::logger> get(std::string name)

return a logger corresponding to “name” (if not existing, it is created)

void setLevel(LogLevel level)

set the log level for all loggers

LogLevel getLevel()

get the log level for all loggers from current instance

void enableFileLogging(std::string filename)

enable file logging

void flush()

flush all loggers

void addSink(std::ostream &os)

add a sink that writes to std::ostream (useful for testing)

Private Members

LogLevel _level = {LogLevel::INFO}

the log level

std::string _fileName = {""}

the file name for file logging (if enabled)

std::unordered_map<std::string, std::shared_ptr<spdlog::logger>> _loggers

map storing the loggers identified by a string

Private Static Functions

Logger &getInstance()

returns the singleton instance of Logger

std::shared_ptr<spdlog::sinks::dist_sink_st> initSinks()

Static function initialising the sinks.

std::shared_ptr<spdlog::sinks::dist_sink_st> sinks()

Return the sinks stored for the loggers.

spdlog::level::level_enum convertLevelToSpdlog(LogLevel level)

convert elsa LogLevel to spdlog::level::level_enum

Friends

friend class LogGuard

LogGuard

class elsa::LogGuard

simple log guard class, logs a message each on creation and destruction.

This class serves as a guard for logging, simple instantiation will log a starting message, while going out of scope logs an ending message.

Author

Matthias Wieczorek - initial code

Author

Maximilian Hornung - modularization

Author

Tobias Lasser - minor modifications

Public Functions

LogGuard(std::string loggerName, std::string message, Logger::LogLevel level = Logger::LogLevel::INFO)

constructor taking the name of the logger to log to, the message and the log level of the message

~LogGuard()

destructor outputting the final log message

Private Members

std::string _message

the name of the logger to log to

Logger::LogLevel _level

the message to log

Timer

class elsa::Timer

Timer class to provide easy logging of timing.

This class provides logging of timing using the guard pattern. It measures the current time from creation, until destruction and outputs a log message with time elapsed on destruction.

Author

Matthias Wieczorek - initial code

Author

Maximilian Hornung - modularization

Author

Tobias Lasser - minor changes

Author

David Frank - use spdlog::stopwatch, renaming of variables for expressing intent better

Public Functions

Timer(std::string caller, std::string method)

start the timer, using loggerName as logger, outputting message at end

~Timer()

stop the timer and actually output the log message

Private Members

std::string _caller

stopwatch measuring the time

std::string _method

the name of what is running