Click here to Skip to main content
Click here to Skip to main content

Simple Protocol logging class

, 30 Apr 2003
Rate this:
Please Sign up or sign in to vote.
An article on Protocolling / Logging
<!-- Article image -->

Sample Image - jaz_CProtocol.gif

<!-- Add the rest of your HTML here -->

Introduction

This is a general Protocol class.  Use this class to generate a log file with IO- and DB-Counters if needed.  This is just a very simple way of generating a logfile.  I wrote that class because of its simplicity.  Better ideas and any errors, please let me know!

Using the code

Include the Header

   #include "./classes/CJazProtocol.h"

Use the Namespace if needed:

   using namespace jaz;

Now you can work with the Class and instance an Object of CProtocol.

   CProtocol objProt("MyName", "1.0.0", _IGNORE_LINES_, _IGNORE_MAXWIDTH_);
Set the Options.  All these options can be set by the constructor!
   objProt.setAddText("Additional Text");     // Additional Text in Header
   objProt.setLines(jaz::_IGNORE_LINES_);     // Number of Lines befor inserting a new Header
   objProt.setMaxWidth(jaz::_DEFAULT_WIDTH_); // Number of Characters on one Line before Break
   objProt.setPgmName("MyName");              // Programm Name in Header
   objProt.setVersion("1.0.0");               // Version Number in Header

Open log file:

   objProt.open("MyProtocol.log", true);

Write lines to log file:

   objProt.printLn("Start LogFile");
   objProt.printLn("This is a normal Logtext");
   objProt.printLn("This is a normal Logtext with Arrow", CProtocol::eArrow);
   objProt.printLn("This is an Information!" ,CProtocol::eInformation);
   objProt.printLn("This is a Warning!" ,CProtocol::eWarning);
   objProt.printLn("This is an Error!" ,CProtocol::eError);

Use the member function addStatisticIO() to get a reference of CStatisticIO. Now you can use that reference for counting input/output from/to a file. Just use ++ to increment it one time. Use the function Increment() to increment it with any number.

   // ------------------------------------------------------------------
   //  IO-Counter Statistics
   // ------------------------------------------------------------------
   // Add Input/Output-Statistic to Protocol
   CStatisticIO& cntAllIO = objProt.addStatisticIO("File1.txt", "All Records");
   CStatisticIO& cntProcessedIO = objProt.addStatisticIO("File1.txt", "Processed Records");
   CStatisticIO& cntDiscardedIO = objProt.addStatisticIO("File1.txt", "Discarded Records");

   // Increment / Decrement IO-Counter
   cntProcessedIO++;
   cntProcessedIO.Increment(8);
   cntProcessedIO.Decrement(3);

   for(int i=0; i < 10; i++)
      cntDiscardedIO++;

   // Add 2 Statistics
   cntAllIO += cntProcessedIO;
   cntAllIO += cntDiscardedIO;

Use the member function addStatisticDB() to get a reference of CStatisticDB. Now you can use that reference for counting Select/Updage/Insert/Delete statements. Use the function Increment() to increment it with any number.

   // ------------------------------------------------------------------
   //  DB-Counter Statistics
   // ------------------------------------------------------------------
   // Add Database-Statistic to Protocol
   CStatisticDB& cntSqlDB = objProt.addStatisticDB("MyDB", "MyTable", "Processed");

   cntSqlDB.Increment(CStatisticDB::eSelect, 20);
   cntSqlDB.Increment(CStatisticDB::eUpdate, 5);
   cntSqlDB.Increment(CStatisticDB::eInsert, 2);
   cntSqlDB.Increment(CStatisticDB::eDelete);

Use the try, catch for detecting exceptions like jaz::CEx_FileNotOpen:

   try
   {
        ...
   }
    catch(jaz::CEx_FileNotOpen)
    {
        cerr << "Could not Open File!" << endl;
        iret = 1;
    }
    catch(...)
    {
        cerr << "Undefined Error. Mucho Bad!" << endl;
        iret = 1;
    }

Close log file if it still open. You should use the is_open() because on a close() the Object tries to write the End-Statistic and when the Object is already closed the End-Statistic will throw an Exception (jaz::CEx_FileNotOpen).

    if( objProt.is_open() )
        objProt.close();

History

This is the 1st Version!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jazper
Software Developer
Switzerland Switzerland
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 1 May 2003
Article Copyright 2003 by jazper
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid