class mrpt::system::COutputLogger
Overview
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.
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_record is set to true (Default= false).
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
See also:
setLoggingLevel, setMinLoggingLevel
#include <mrpt/system/COutputLogger.h> class COutputLogger { public: // structs struct TMsg; // fields bool logging_enable_console_output {true}; bool logging_enable_keep_record {false}; // construction COutputLogger(std::string_view name); COutputLogger(); COutputLogger(const COutputLogger&); COutputLogger(COutputLogger&&); // methods void logStr(const VerbosityLevel level, std::string_view msg_str) const; void logFmt(const VerbosityLevel level, const char* fmt, ...) const; void void logCond(const VerbosityLevel level, bool cond, const std::string& msg_str) const; void setLoggerName(const std::string& name); std::string getLoggerName() const; void setVerbosityLevel(const VerbosityLevel level); void setVerbosityLevelForCallbacks(const VerbosityLevel level); void setMinLoggingLevel(const VerbosityLevel level); VerbosityLevel getMinLoggingLevel() const; VerbosityLevel getMinLoggingLevelForCallbacks() const; bool isLoggingLevelVisible(VerbosityLevel level) const; void getLogAsString(std::string& log_contents) const; std::string getLogAsString() const; void writeLogToFile(const std::optional<std::string>& fname_in = std::nullopt) const; void dumpLogToConsole() const; std::string getLoggerLastMsg() const; void getLoggerLastMsg(std::string& msg_str) const; void loggerReset(); void logRegisterCallback(output_logger_callback_t userFunc); bool logDeregisterCallback(output_logger_callback_t userFunc); COutputLogger& operator = (const COutputLogger&); COutputLogger& operator = (COutputLogger&&); 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(); }; // direct descendants template < size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE = double > class CKalmanFilterCapable; template <class GRAPH_T = typename mrpt::graphs::CNetworkOfPoses2DInf> class CGraphSlamEngine; template <class GRAPH_T = typename mrpt::graphs::CNetworkOfPoses2DInf> class CRegistrationDeciderOrOptimizer; template < class PARTICLE_TYPE, class MYSELF, mrpt::bayes::particle_storage_mode STORAGE > class PF_implementation; class DataSourceRawlog; class ICP_SLAM_App_Base; class KFSLAMApp; class MonteCarloLocalization_Base; class RBPF_SLAM_App_Base; class RawlogGrabberApp; class CParticleFilter; class CServerTCPSocket; class ScalarFactorGraph; class CWindowManager; class C2DRangeFinderAbstract; class CCANBusReader; class CCameraSensor; class CGPSInterface; class CNationalInstrumentsDAQ; class CPhidgetInterfaceKitProximitySensors; class CRandomFieldGridMap2D; class CRandomFieldGridMap3D; template <typename VECTORTYPE = CVectorDouble, class USERPARAM = VECTORTYPE> class CLevenbergMarquardtTempl; template < typename NUMTYPE = double, typename DATASET = CMatrixDynamic<NUMTYPE>, typename MODEL = CMatrixDynamic<NUMTYPE> > class RANSAC_Template; class CAbstractNavigator; class CRobot2NavInterface; class CIncrementalMapPartitioner; class CMetricMapBuilder; class CMetricMapsAlignmentAlgorithm; class CControlledRateTimer; class CTimeLogger;
Fields
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:
Construction
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 constructor.
Methods
void logStr(const VerbosityLevel level, std::string_view msg_str) const
Main method to add the specified message string to the logger.
See also:
void logFmt(const VerbosityLevel level, const char* fmt, ...) const
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);
See also:
void void logCond(const VerbosityLevel level, bool cond, const std::string& msg_str) const
Log the given message only if the condition is satisfied.
See also:
log, logFmt
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:
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()
See also:
setVerbosityLevelForCallbacks(), setVerbosityLevel()
void setVerbosityLevelForCallbacks(const VerbosityLevel level)
Overrides the minimum verbosity level for user callbacks.
(New in MRPT 2.5.7)
See also:
void setMinLoggingLevel(const VerbosityLevel level)
alias of setVerbosityLevel()
VerbosityLevel getMinLoggingLevel() const
See also:
VerbosityLevel getMinLoggingLevelForCallbacks() const
See also:
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 logger instance: clears the history and the callbacks, and restores the default name, verbosity levels and options.
bool logDeregisterCallback(output_logger_callback_t userFunc)
Returns:
true if an entry was found and deleted.
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