Slice Tools
  • Home
  • SourceForge Page


  • libFoundation
  • Home
  • Class Hierarchy
  • Class List
  • Class Members
  • Examples


  • SourceForge.net Logo
     

    example-logging.cc

    Demonstrates logging capabilities.

    Log messages are written to the log from three different categories, and at various message levels using both insertion style and string messages.

    A separate log category should be used for different functional parts of the program. In this example, "MAIN" (implicitly), "A", and "B" are used.

    Different message levels should be used to note importance to the different messages, so that very important messages will be noticed, but lower priority messages can be filtered or ignored.

    // $Id: example-logging.cc,v 1.1 2005/07/29 02:55:16 mschatz Exp $
    // Demonstrates how Foundation logging can be used
    
    #include "Foundation.hh"
    
    // Some functional section of the program.
    // Note how both insertion style and string messages are logged.
    
    // The logManager is explicitly passed in so that this section
    // can ask for its own LogCategory
    void workA(Logger * logManager, int i)
    {
      LogCategory log = logManager->getCategory("A");
    
      log.setMessageLevel(i * 10);
      log << "Message from A, i=" << i << endl;
      log.debug("Message is at MESSAGE_LEVEL_DEBUG_HIGH");
    }
    
    
    
    
    // Some different functional section of the program
    void workB(Logger * logManager, int i)
    {
      LogCategory log = logManager->getCategory("B");
    
      log.log("This message is at the default message level");
    
      // Use the message level stack for making quick changes to the message level
      log.pushMessageLevel(MESSAGE_LEVEL_DEBUG_ERROR);
    
      // Messages are only written to the log when a '\n' newline character is
      // written
      log << "ERROR message from B, i=";
      log << i << endl;
      
      // Go back to the old message level
      log.popMessageLevel();
    }
    
    
    
    
    
    int main(int argc, char ** argv)
    {
      string helptext = "Foundation logging example";
      string version = "Foundation example-logging 1.0";
      string dependencies = "Foundation";
    
      try
      {
        // Instantiate Foundation
        Foundation *app = new Foundation (version, 
                                                    helptext, 
                                                    dependencies,
                                                    argc, 
                                                    argv);
        
        // Parse the command line for options (--filterlevel, --logfile, ...)
        app->handleStandardOptions();
    
        // Main Program
        // The logManager is created as soon as handleStandardOptions is called
        Logger * logManager = app->getLogger();
        LogCategory main = app->log();
    
        for (int i = 1; i < 8; i++)
        {
          main << "In Main Program i=" << i << endl;
    
          workA(logManager, i);
          workB(logManager, i);
        }
    
        // Delete Foundation
        delete app;
      }
      catch (ExitProgramNormally e)
      {
        // non fatal error (help, version, or dependencies were requested)
      }
      catch (Exception e)
      {
        // fatal error
        
        cerr << e << endl;
        exit (100);
      }
    
      return 0;
    }