Click here to Skip to main content
12,813,675 members (33,459 online)
Click here to Skip to main content
Add your own
alternative version

Stats

34.2K views
1.1K downloads
20 bookmarked
Posted 30 Apr 2003

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

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

<pre lang=c++> #include "./classes/CJazProtocol.h"

Use the Namespace if needed:

<pre lang=c++> using namespace jaz;

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

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

<pre lang=c++> objProt.open("MyProtocol.log", true);

Write lines to log file:

<pre lang=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.

<pre lang=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.

<pre lang=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:

<pre lang=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).

<pre lang=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)

Share

About the Author

jazper
Software Developer
Switzerland Switzerland
No Biography provided

You may also be interested in...

Comments and Discussions

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