Click here to Skip to main content
15,894,017 members
Articles / Desktop Programming / MFC

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
/*.........................................................................
    Header:         CJazProtocol.h
    SourceCode:     CJazProtocol.cpp
    Description:    Providing a Logging (Protocol) Mechanism
    
    Copyright:      Juerg Zgraggen, Switzerland
    Website:        http://www.ejaz.net

    History
    2003.04.01  jz  Created
..........................................................................*/

#if !defined(_JAZ_PROTOCOL_INCLUDED_)
#define _JAZ_PROTOCOL_INCLUDED_

//--- Includes -------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <time.h>
#include "CJazException.h"

namespace jaz
{
    //--- Definitions ----------------------------------------------------------
    class CStatistic;
    class CStatisticIO;
    class CStatisticDB;

    //--- Constants ------------------------------------------------------------
    const int _DEFAULT_WIDTH_     = 75;
    const int _IGNORE_LINES_      = 0;
    const int _IGNORE_MAXWIDTH_   = 0;
    const int _DEFAULT_INCREMENT_ = 1;
    const int _DEFAULT_DECREMENT_ = 1;


    /***************************************************************************
        Class:            CProtocol
        Description:    Class for generating a Standard Protocol Output
    ***************************************************************************/
    class CProtocol
    {
    public:
        //--- CONSTRUCTORs / DESTRUCTOR -----------------------------
        CProtocol(std::string strPgmName,
            std::string strVersion,
            int iLines = _IGNORE_LINES_,
            int iMaxWidth = _DEFAULT_WIDTH_,
            std::string strAddText = "");
        ~CProtocol();

        //--- MEMBERVARIABLES ---------------------------------------
        enum enumState { eNormal, eInformation, eWarning, eError, eArrow };

        //--- SETs --------------------------------------------------
        // Set Name of the Program
        void setPgmName(std::string strValue);
        
        // Set Version of the Program
        void setVersion(std::string strValue);
        
        // Set Number of Lines to be written before a new Headerlines
        void setLines(int iValue);
        
        // Set Maximum Width of Protocol (_DEFAULT_WIDTH_ / _IGNORE_MAXWIDTH_)
        void setMaxWidth(int iValue);
        
        // Set Additional Text for Headerlines
        void setAddText(std::string strValue);

        //--- GETs --------------------------------------------------
        // Get Name of the Program
        std::string getPgmName();

        // Get Version of the Program
        std::string getVersion();
        
        // Get Number of Lines to be written before a new Headerlines
        int getLines();
        
        // Get Maximum Width of Protocol (_DEFAULT_WIDTH_ / _IGNORE_MAXWIDTH_)
        int getMaxWidth();
        
        // Get Additional Text for Headerlines
        std::string getAddText();

        //--- METHODs -----------------------------------------------
        // Open / Create Protocol
        void open(std::string strFile, bool bOverwrite = false);
        
        // Close Protocol
        void close();

        // Check if Protocol is opend
        bool is_open();
        
        // Print Line into Protocol
        void printLn(const std::string& strLine, enumState eState = enumState::eNormal, bool bTimeStamp = false );
        
        // Print Headerlines of Protocol
        void printHeader();
        
        // Add new IO Statistic
        CStatisticIO& addStatisticIO();

        // Add new IO Statistic
        CStatisticIO& addStatisticIO(const std::string& strFile, const std::string& strText = "");

        // Add new DB Statistic
        CStatisticDB& addStatisticDB();

        // Add new DB Statistic
        CStatisticDB& addStatisticDB(const std::string& strDatabase, const std::string& strTable = "", const std::string& strText = "");

    private:
        //--- METHODs -----------------------------------------------
        // Write Statistic into Protocol
        void writeStatistic();

        //--- MEMBERVARIABLES ---------------------------------------
        std::string    m_strPgmName;
        std::string    m_strVersion;
        time_t         m_tDateTimeStart;
        std::string    m_strAddText;   // Additional Text for Header
        int            m_iLines;       // Defined Maximum Lines each Page (0 = Infinit - no PageBreak)
        int            m_iMaxWidth;    // Maximal Width of Protocol
        int            m_iPrintLnCnt;  // Number of Printed Lines for Calculation of PageBreak
        int            m_iMaxWidthCnt; // Number of Printed Letters for Calculation of MaxWidth
        std::vector<CStatisticIO*> m_vecStatIOPtr;
        std::vector<CStatisticDB*> m_vecStatDBPtr;
        std::fstream m_objFile;
    };

    /***************************************************************************
        Class:            CStatistic
        Description:    Superclass for Statistics
    ***************************************************************************/
    class CStatistic
    {
    public:
        //--- SETs --------------------------------------------------
        // Set Maintext of Statistic
        void setText(std::string strValue);

        //--- GETs --------------------------------------------------
        // Set Maintext of Statistic
        std::string getText();

    protected:
        //--- CONSTRUCTORs / DESTRUCTOR -----------------------------
        CStatistic();   // Not to be able to generate Instances

        //--- MEMBERVARIABLES ---------------------------------------
        std::string    m_strText;
    };

    /***************************************************************************
        Class:            CStatisticIO
        Description:    Statistic-Counter for File Operations
    ***************************************************************************/
    class CStatisticIO : public CStatistic
    {
    public:
        //--- CONSTRUCTORs / DESTRUCTOR -----------------------------
        CStatisticIO();
        CStatisticIO(std::string strFilename);

        //--- OPERATORs ---------------------------------------------
        void operator++();       //Prefix
        void operator++(int);    //Postfix
        CStatisticIO& operator+=(CStatisticIO obj);

        //--- SETs --------------------------------------------------
        // Set Filename on which this Statistic referes to
        void setFilename(std::string strValue);

        // Set actual Counter to any Value
        void setCounter(int iValue);

        //--- GETs --------------------------------------------------
        // Get Filename on which this Statistic referes to
        std::string getFilename();

        // Get actual Counter
        int getCounter();

        //--- METHODs -----------------------------------------------
        // Increment Counter
        void Increment(int iValue = _DEFAULT_INCREMENT_);
        
        // Decrement Counter
        void Decrement(int iValue = _DEFAULT_DECREMENT_);

    private:
        //--- MEMBERVARIABLES ---------------------------------------
        std::string m_strFilename;
        int         m_iCounter;
    };

    /***************************************************************************
        Class:            CStatisticDB
        Description:    Statistic-Counter for DB Operations
    ***************************************************************************/
    class CStatisticDB : public CStatistic
    {
    public:
        //--- CONSTRUCTORs / DESTRUCTOR -----------------------------
        CStatisticDB();
        CStatisticDB(std::string strDBName, std::string strTableName);

        //--- OPERATORs ---------------------------------------------
        void operator+(const CStatisticDB& obj);
        void operator+=(const CStatisticDB& obj);

        //--- MEMBERVARIABLES ---------------------------------------
        enum enumItem{ eSelect, eUpdate, eInsert, eDelete };

        //--- SETs --------------------------------------------------
        // Set Databasename which this Statistic referes to
        void setDBName(std::string strValue);

        // Set Databasename which this Statistic referes to
        void setTableName(std::string strValue);

        // Set actual Select-Counter to any Value
        void setSelectCnt(int iValue);

        // Set actual Update-Counter to any Value
        void setUpdateCnt(int iValue);

        // Set actual Insert-Counter to any Value
        void setInsertCnt(int iValue);

        // Set actual Delete-Counter to any Value
        void setDeleteCnt(int iValue);

        //--- GETs --------------------------------------------------
        // Get Databasename which this Statistic referes to
        std::string getDBName();
        
        // Get Databasename which this Statistic referes to
        std::string getTableName();

        // Get actual Select-Counter to any Value
        int getSelectCnt();

        // Get actual Update-Counter to any Value
        int getUpdateCnt();

        // Get actual Insert-Counter to any Value
        int getInsertCnt();

        // Get actual Delete-Counter to any Value
        int getDeleteCnt();

        //--- METHODs -----------------------------------------------
        // Increment Counter
        void Increment(enumItem eItem, int iValue = _DEFAULT_INCREMENT_);

        // Decrement Counter
        void Decrement(enumItem eItem, int iValue = _DEFAULT_DECREMENT_);

    private:
        //--- MEMBERVARIABLES ---------------------------------------
        std::string  m_strDBName;
        std::string  m_strTableName;
        int  m_iSelectCnt;
        int  m_iUpdateCnt;
        int  m_iInsertCnt;
        int  m_iDeleteCnt;
    };
}

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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