Simple Protocol logging class
An article on Protocolling / Logging
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!