Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / MFC
Article

Simple Protocol logging class

Rate me:
Please Sign up or sign in to vote.
3.20/5 (4 votes)
30 Apr 2003CPOL1 min read 37.9K   1.2K   20  
An article on Protocolling / Logging

Sample Image - jaz_CProtocol.gif

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

C++
#include "./classes/CJazProtocol.h"

Use the Namespace if needed:

C++
using namespace jaz;

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

C++
CProtocol objProt("MyName", "1.0.0", _IGNORE_LINES_, _IGNORE_MAXWIDTH_);
Set the Options.  All these options can be set by the constructor!
C++
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:

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

Write lines to log file:

C++
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.

C++
// ------------------------------------------------------------------
//  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.

C++
// ------------------------------------------------------------------
//  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:

C++
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).

C++
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)


Written By
Software Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --