Click here to Skip to main content
12,447,625 members (57,882 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked

Simple Protocol logging class

, 30 Apr 2003 CPOL
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 -->


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:"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

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

// 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);

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

     cerr << "Could not Open File!" << endl;
     iret = 1;
     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() )


This is the 1st Version!


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


About the Author

Software Developer
Switzerland Switzerland
No Biography provided

You may also be interested in...


Comments and Discussions

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