Matrix Science header

Logging using ms_loggingmonitor
[Error handling and logging]

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:

LoggingClassDiagram.png

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());
    }
    void  onLogMessage(matrix_science::ms_loggingmonitor::Level_e eSeverity, 
                       matrix_science::ms_loggingmonitor::Source_e eSource, 
                       long lMsgId, const std::string &text, long lContextId = 0) {
        ofs << "0x" << std::hex << std::setw(4) << std::setfill('0') << lMsgId
            << " " << matrix_science::ms_loggingmonitor::severityAsText(eSeverity)
            << " " << matrix_science::ms_loggingmonitor::messageSourceAsText(eSource) 
            << " " << text << std::endl;
            
        // Or could call 
        matrix_science::ms_logger::formatMessage(eSeverity, eSource, lMsgId, text, lContextId);
    }
private:
    std::ofstream ofs;
};

int main()
{
    matrix_science::ms_loggingmonitor & loggingMonitor = matrix_science::ms_loggingmonitor::getDefaultMonitor();
    MyLogger logger("C:/tmp/log.txt",
                    matrix_science::ms_logger::COL_ASC_TIME | matrix_science::ms_logger::COL_MSG);
    loggingMonitor.addLogEventsHandler(logger);
}

Copyright © 2022 Matrix Science Ltd.  All Rights Reserved. Generated on Thu Mar 31 2022 01:12:30