API  4.5.1
For C++ developers
OpenSim::Logger Class Reference

This is a singleton class (single instance) for logging messages and controlling how those messages are presented to the user. More...

Public Member Functions

 Logger ()=delete
 This is a static singleton class: there is no way of constructing it. More...
 

Public Types

enum  Level {
  Level::Off = 6, Level::Critical = 5, Level::Error = 4, Level::Warn = 3,
  Level::Info = 2, Level::Debug = 1, Level::Trace = 0
}
 This enum lists the types of messages that should be logged. More...
 

Static Public Member Functions

static void setLevel (Level level)
 Log messages of importance level and greater. More...
 
static Level getLevel ()
 
static void setLevelString (std::string level)
 Set the logging level using one of the following strings (case-insensitive): More...
 
static std::string getLevelString ()
 
static bool shouldLog (Level level)
 Returns true if messages at the provided level should be logged, based on the set logging level. More...
 
static void addFileSink (const std::string &filepath="opensim.log")
 Log messages to a file at the level getLevel(). More...
 
static void removeFileSink ()
 Remove the filesink if it exists. More...
 
static void addSink (const std::shared_ptr< LogSink > sink)
 Start reporting messages to the provided sink. More...
 
static void removeSink (const std::shared_ptr< LogSink > sink)
 Remove a sink. More...
 
Commands to log messages

Use these functions instead of using spdlog directly.

template<typename... Args>
static void critical (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
static void error (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
static void warn (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
static void info (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
static void debug (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
static void trace (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
static void cout (spdlog::string_view_t fmt, const Args &... args)
 Use this function to log messages that would normally be sent to std::cout. More...
 

Related Functions

(Note that these are not member functions.)

Logging functions
template<typename... Args>
void log_critical (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
void log_error (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
void log_warn (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
void log_info (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
void log_debug (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
void log_trace (spdlog::string_view_t fmt, const Args &... args)
 
template<typename... Args>
void log_cout (spdlog::string_view_t fmt, const Args &... args)
 Use this function to log messages that would normally be sent to std::cout. More...
 

Detailed Description

This is a singleton class (single instance) for logging messages and controlling how those messages are presented to the user.

Member Enumeration Documentation

◆ Level

This enum lists the types of messages that should be logged.

These levels match those of the spdlog logging library that OpenSim uses for logging.

Enumerator
Off 

Do not log any messages.

Useful when running an optimization or automated pipeline.

Critical 

Only log critical errors.

Error 

Log all messages that require user intervention.

Warn 

Log warnings.

Warnings are generated when the software will proceed but the user should check their input.

Info 

Default.

Debug 

Log information that may be useful when debugging the operation of the software to investigate unexpected results.

Trace 

Log as much as possible, including messages that describe the software's behavior step by step.

Note: OpenSim has very few Trace-level messages.

Constructor & Destructor Documentation

◆ Logger()

OpenSim::Logger::Logger ( )
delete

This is a static singleton class: there is no way of constructing it.

Member Function Documentation

◆ addFileSink()

static void OpenSim::Logger::addFileSink ( const std::string &  filepath = "opensim.log")
static

Log messages to a file at the level getLevel().

OpenSim logs messages to the file opensim.log by default. If we are already logging messages to a file, then this function issues a warning and returns; invoke removeFileSink() first.

Note
This function is not thread-safe. Do not invoke this function concurrently, or concurrently with addSink() or removeSink().
If filepath can't be opened, no log file is created.

◆ addSink()

static void OpenSim::Logger::addSink ( const std::shared_ptr< LogSink sink)
static

Start reporting messages to the provided sink.

Note
This function is not thread-safe. Do not invoke this function concurrently, or concurrently with addLogFile() or removeSink().

◆ cout()

template<typename... Args>
static void OpenSim::Logger::cout ( spdlog::string_view_t  fmt,
const Args &...  args 
)
inlinestatic

Use this function to log messages that would normally be sent to std::cout.

These messages always appear, and are also logged to the filesink (addFileSink()) and any sinks added via addSink(). The main use case for this function is inside of functions whose intent is to print information (e.g., Component::printSubcomponentInfo()). Besides such use cases, this function should be used sparingly to give users control over what gets logged.

Referenced by log_cout().

◆ critical()

template<typename... Args>
static void OpenSim::Logger::critical ( spdlog::string_view_t  fmt,
const Args &...  args 
)
inlinestatic

Referenced by log_critical().

◆ debug()

template<typename... Args>
static void OpenSim::Logger::debug ( spdlog::string_view_t  fmt,
const Args &...  args 
)
inlinestatic

Referenced by log_debug().

◆ error()

template<typename... Args>
static void OpenSim::Logger::error ( spdlog::string_view_t  fmt,
const Args &...  args 
)
inlinestatic

Referenced by log_error().

◆ getLevel()

static Level OpenSim::Logger::getLevel ( )
static

◆ getLevelString()

static std::string OpenSim::Logger::getLevelString ( )
static

◆ info()

template<typename... Args>
static void OpenSim::Logger::info ( spdlog::string_view_t  fmt,
const Args &...  args 
)
inlinestatic

Referenced by log_info().

◆ removeFileSink()

static void OpenSim::Logger::removeFileSink ( )
static

Remove the filesink if it exists.

If the filesink was already removed, then this does nothing.

Note
This function is not thread-safe. Do not invoke this function concurrently, or concurrently with addSink() or removeSink().

◆ removeSink()

static void OpenSim::Logger::removeSink ( const std::shared_ptr< LogSink sink)
static

Remove a sink.

If it doesn't exist, do nothing.

Note
This function is not thread-safe. Do not invoke this function concurrently, or concurrently with addLogFile() or addSink().

◆ setLevel()

static void OpenSim::Logger::setLevel ( Level  level)
static

Log messages of importance level and greater.

For example, if the level is set to Info, then Critical, Error, Warn, and Info messages are logged, while Debug and Trace messages are not logged.

◆ setLevelString()

static void OpenSim::Logger::setLevelString ( std::string  level)
static

Set the logging level using one of the following strings (case-insensitive):

  • Off
  • Critical
  • Error
  • Warn
  • Info
  • Debug
  • Trace This variant of setLevel() is for use in Matlab.
    See also
    Level.

◆ shouldLog()

static bool OpenSim::Logger::shouldLog ( Level  level)
static

Returns true if messages at the provided level should be logged, based on the set logging level.

The following code will produce output:

Log::setLevel(Log::Level::Warn);
if (shouldLog(Log::Level::Error)) {
std::cout << "Error encountered." << std::endl;
}

◆ trace()

template<typename... Args>
static void OpenSim::Logger::trace ( spdlog::string_view_t  fmt,
const Args &...  args 
)
inlinestatic

Referenced by log_trace().

◆ warn()

template<typename... Args>
static void OpenSim::Logger::warn ( spdlog::string_view_t  fmt,
const Args &...  args 
)
inlinestatic

Referenced by log_warn().

Friends And Related Function Documentation

◆ log_cout()

template<typename... Args>
void log_cout ( spdlog::string_view_t  fmt,
const Args &...  args 
)
related

Use this function to log messages that would normally be sent to std::cout.

These messages always appear, and are also logged to the filesink (addFileSink()) and any sinks added via addSink(). The main use case for this function is inside of functions whose intent is to print information (e.g., Component::printSubcomponentInfo()). Besides such use cases, this function should be used sparingly to give users control over what gets logged.

References cout().

◆ log_critical()

template<typename... Args>
void log_critical ( spdlog::string_view_t  fmt,
const Args &...  args 
)
related

References critical().

◆ log_debug()

template<typename... Args>
void log_debug ( spdlog::string_view_t  fmt,
const Args &...  args 
)
related

References debug().

◆ log_error()

template<typename... Args>
void log_error ( spdlog::string_view_t  fmt,
const Args &...  args 
)
related

References error().

◆ log_info()

template<typename... Args>
void log_info ( spdlog::string_view_t  fmt,
const Args &...  args 
)
related

References info().

◆ log_trace()

template<typename... Args>
void log_trace ( spdlog::string_view_t  fmt,
const Args &...  args 
)
related

References trace().

◆ log_warn()

template<typename... Args>
void log_warn ( spdlog::string_view_t  fmt,
const Args &...  args 
)
related

References warn().


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