Matrix Science Mascot Parser toolkit
 
Loading...
Searching...
No Matches
Logging using ms_loggingmonitor

Logging using ms_loggingmonitor

Using setLoggingFile in Parser 2.5 and earlier (deprecated)

Mascot Parser 2.5 and earlier provided a function: ms_errs::setLoggingFile which saved error and logging messages to the specified file. The problem with this approach is that it's necessary to call this for every Mascot Parser object that is created because there is no global handler. Also, any logging that occurs in the constructor would not go into the log file.

ms_loggingmonitor was introduced in Mascot Parser 2.6. This can be used when an application wants to log its own messages and Parser messages to a single file, rather then to multiple log files.

Class diagram for logging classes:

Simple logging with Parser 2.6 and later

The following is an example of how to redirect all logging from msparser to a specified file and how to specify the logging level.

  1. In the program's main function, create a file logger:
    ms_file_logger fileLogger("C:/tmp/log.txt", ms_logger::COL_LOGGER_DEFAULTS );
    specifying whatever columns you want from the list in ms_logger::LogOutputColumns_e
  2. Add the file logger handler to the default monitor:
    ms_loggingmonitor::getDefaultMonitor() . addLogEventsHandler (fileLogger)
  3. Set the logging level, either to be the same for all event sources or different for each one, by calling, for example:
    ms_loggingmonitor::Level_e logLevel = ms_loggingmonitor::LVL_ERROR | ms_loggingmonitor::LVL_WARNING;
    ms_loggingmonitor::getDefaultMonitor().setLogMask(logLevel, ms_loggingmonitor::SRC_ALL);
  4. msparser messages will now all be logged to the specified log file
  5. Call ms_loggingmonitor::logMessage to log messages from your own application

Custom logging

For more complex cases, a custom logger can also be created:

class MyLogger : public matrix_science::ms_logger
{
public:
MyLogger(const std::string & logFile, const unsigned int colsToOutput) : ms_logger(colsToOutput)
{
ofs.open(logFile.c_str());
}
long lMsgId, const std::string &text, long lContextId = 0) {
ofs << "0x" << std::hex << std::setw(4) << std::setfill('0') << lMsgId
<< " " << text << std::endl;
// Or could call
matrix_science::ms_logger::formatMessage(eSeverity, eSource, lMsgId, text, lContextId);
}
private:
std::ofstream ofs;
};
int main()
{
MyLogger logger("C:/tmp/log.txt",
loggingMonitor.addLogEventsHandler(logger);
}
Abstract class which provides the interface used by ms_logging monitor to delegate logging events.
Definition: ms_logging.hpp:137
std::string formatMessage(ms_loggingmonitor::Level_e eSeverity, ms_loggingmonitor::Source_e eSource, long lMsgId, const std::string &text, const char *srcFileName, const int srcFileLineNum, long lContextId) const
Returns a string with formatting based on the columns requested to be output.
Definition: ms_logging.cpp:281
@ COL_ASC_TIME
[ ] Output the time in the format: "Sat Jan 6 11:35:22 2018"
Definition: ms_logging.hpp:152
@ COL_MSG
[*] Output the formatted message text.
Definition: ms_logging.hpp:163
virtual void onLogMessage(ms_loggingmonitor::Level_e eSeverity, ms_loggingmonitor::Source_e eSource, long lMsgId, const std::string &text, const char *srcFileName, const int srcFileLineNum, long lContextId=0)=0
Processes and handles the passed logging message.
Monitor class to delegate logging events generated from multiple sources.
Definition: ms_logging.hpp:44
static std::string severityAsText(Level_e eSeverity)
Returns a severity enum value as a text string.
Definition: ms_logging.cpp:212
static matrix_science::ms_loggingmonitor & getDefaultMonitor()
Returns the global ms_loggingmonitor used by msparser.
Definition: ms_logging.cpp:156
virtual int addLogEventsHandler(ms_logger &logger)
Registers the passed ms_logger with the ms_loggingmonitor.
Definition: ms_logging.cpp:56
Level_e
Logging severity levels.
Definition: ms_logging.hpp:51
Source_e
Logging event source.
Definition: ms_logging.hpp:65
static std::string messageSourceAsText(Source_e eSource)
Returns a Source_e enum value as a text string.
Definition: ms_logging.cpp:179