Class COutputLogger
Defined in File COutputLogger.h
Nested Relationships
Nested Types
Inheritance Relationships
Derived Types
public mrpt::system::CControlledRateTimer(Class CControlledRateTimer)public mrpt::system::CTimeLogger(Class CTimeLogger)
Class Documentation
-
class COutputLogger
Versatile class for consistent logging and management of output messages.
COutputLogger is a versatile class for logging messages either to the terminal window or to an external file. Class instances can take messages in std::string using the logStr class methods. The following macros are also provided for usage within a class that inherits from COutputLogger:
// Plain strings: MRPT_LOG_DEBUG("This will be shown only if verbosity level is LVL_DEBUG."); MRPT_LOG_ERROR("This message will be always shown."); // printf-like versions: MRPT_LOG_ERROR_FMT("Out of range value: %i.", int_param); // stream-like versions: MRPT_LOG_ERROR_STREAM("Out of range value: " << int_param << " more vars: " << other_var); // Minimum period (in seconds) between messages: MRPT_LOG_THROTTLE_DEBUG_STREAM(5.0, "Var=" << value << " foo=" << foo_var); // Only once: MRPT_LOG_ONCE_WARN("Notice: blah blah");
From outside of a class inheriting from COutputLogger the following
MRPT_UNSCOPED_{START|END}macros are provided:MRPT_UNSCOPED_LOGGER_START; MRPT_LOG_WARN("blah"); MRPT_LOG_ERROR_STREAM("error: " << strval); MRPT_UNSCOPED_LOGGER_END;
Logger instance keeps the messages in an internal container so that upon request it can dump them either to the console or to an external file altogether.
The message, when printed in the terminal window, is colored according to the logger’s current verbosity/logging level (Logging level with which the underlying TMsg instance was instantiated). The available verbosity levels as well as their corresponding colors are listed below:
LVL_DEBUG => CONCOL_BLUE
LVL_INFO => CONCOL_NORMAL
LVL_WARN => CONCOL_GREEN
LVL_ERROR => CONCOL_RED
Logged messages are displayed in the screen if the current logger level is higher than m_min_verbosity_level (logger ignores those messages altogether). This can be used for filtering the output messages according to their importance (e.g. show only error messages by issuing setMinLoggingLevel(LVL_ERROR)).
Default logging level is LVL_INFO.
See also
setLoggingLevel, setMinLoggingLevel
User may receive callbacks whenever a message is displayed to console by using logRegisterCallback(). If for some reason the callbacks are not needed any more, use logDeregisterCallback() to stop receiving calls. This mechanism is useful in case of showing the messages to a GUI, transmiting them to a remote machine, etc.
Note that only those messages whose “importance level” will be printed and sent to user callbacks. However, all messages will be stored and saved to a file with writeLogToFile() despite the current filter, if
logging_enable_keep_recordis set totrue(Default=false).See also
TMsg
Note
By default every logged message is going to be dumped to the standard output as well (if VerbosityLevel > m_min_verbosity_level). Unset logging_enable_console_output class variable if that’s not the desired behavior
Subclassed by mrpt::system::CControlledRateTimer, mrpt::system::CTimeLogger
Logging methods
-
bool logging_enable_console_output = {true}
[Default=true] Set it to false in case you don’t want the logged messages to be dumped to the output automatically.
-
bool logging_enable_keep_record = {false}
[Default=false] Enables storing all messages into an internal list.
See also
-
inline COutputLogger(std::string_view name)
Construct a COutputLogger instance with the given name as the instance name.
Call to this constructor can be used instead of first initializing the object and then explicitly setting the name like in the following case:
COutputLogger a_logger; a_logger.setLoggerName("logger_name");
-
COutputLogger() = default
Default constructor
-
virtual ~COutputLogger()
virtual dtor (so we can derive classes from this one)
-
void logStr(const VerbosityLevel level, std::string_view msg_str) const
Main method to add the specified message string to the logger.
- void logFmt (const VerbosityLevel level, const char *fmt,...) const MRPT_printf_format_check(3
Alternative logging method, which mimics the printf behavior.
Handy for not having to first use mrpt::format to pass a std::string message to logStr
// instead of: logStr(mrpt::format("Today is the %d of %s, %d", 15, "July", 2016)); // one can use: logFmt("Today is the %d of %s, %d", 15, "July", 2016);
- void void logCond (const VerbosityLevel level, bool cond, const std::string &msg_str) const
Log the given message only if the condition is satisfied.
-
void setLoggerName(const std::string &name)
Set the name of the COutputLogger instance.
See also
-
std::string getLoggerName() const
Return the name of the COutputLogger instance.
See also
-
inline void setVerbosityLevel(const VerbosityLevel level)
Set the minimum logging level for which the incoming logs are going to be taken into account.
String messages with specified VerbosityLevel smaller than the min, will not be:
printed out to the terminal,
stored in the internal record,
sent out to user-provided callbacks.
Since MRPT 2.5.7, the minimum verbosity level for user callbacks can be independently changed afterwards via setVerbosityLevelForCallbacks()
-
void setVerbosityLevelForCallbacks(const VerbosityLevel level)
Overrides the minimum verbosity level for user callbacks.
See also
logRegisterCallback()
Note
(New in MRPT 2.5.7)
-
void setMinLoggingLevel(const VerbosityLevel level)
alias of setVerbosityLevel()
-
inline VerbosityLevel getMinLoggingLevel() const
See also
-
inline VerbosityLevel getMinLoggingLevelForCallbacks() const
See also
-
inline bool isLoggingLevelVisible(VerbosityLevel level) const
-
void getLogAsString(std::string &log_contents) const
Fill the provided string with the contents of the logger’s history in std::string representation
-
std::string getLogAsString() const
Get the history of COutputLogger instance in a string representation.
-
void writeLogToFile(const std::optional<std::string> &fname_in = std::nullopt) const
Write the contents of the COutputLogger instance to an external file.
Upon call to this method, COutputLogger dumps the contents of all the logged commands so far to the specified external file. By default the filename is set to ${LOGGERNAME}.log except if the fname parameter is provided
See also
dumpToConsole, getAsString
-
void dumpLogToConsole() const
Dump the current contents of the COutputLogger instance in the terminal window.
See also
writeToFile
-
std::string getLoggerLastMsg() const
Return the last Tmsg instance registered in the logger history
-
void getLoggerLastMsg(std::string &msg_str) const
Fill input string with the contents of the last message in history
-
void loggerReset()
Reset the contents of the logger instance. Called upon construction.
-
void logRegisterCallback(output_logger_callback_t userFunc)
-
bool logDeregisterCallback(output_logger_callback_t userFunc)
- Returns:
true if an entry was found and deleted.
Public Functions
-
inline COutputLogger(const COutputLogger&)
-
inline COutputLogger &operator=(const COutputLogger&)
-
inline COutputLogger(COutputLogger&&)
-
inline COutputLogger &operator=(COutputLogger&&)
Public Static Functions
-
static std::array<mrpt::system::ConsoleForegroundColor, NUMBER_OF_VERBOSITY_LEVELS> &logging_levels_to_colors()
-
static const std::array<const char*, NUMBER_OF_VERBOSITY_LEVELS> &logging_levels_to_names()
Map from VerbosityLevels to their corresponding names. Handy for printing the current message VerbosityLevel along with the actual content
Protected Attributes
-
VerbosityLevel m_min_verbosity_level = LVL_INFO
Provided messages with VerbosityLevel smaller than this value shall be ignored.
-
VerbosityLevel m_min_verbosity_level_callbacks = LVL_INFO
The verbosity level that applies to callback calls