Click here to Skip to main content
15,884,472 members
Articles / Desktop Programming / MFC

A Great Protocol Analyser

Rate me:
Please Sign up or sign in to vote.
3.39/5 (12 votes)
16 Aug 2007CPOL2 min read 39.8K   2.9K   32  
A programmable, easy-to-use protocol decoder for parsing and displaying binary package
#include "StdAfx.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#include "LogFile.h"

//////////////////////////////////////////////////////
COneLogFile::COneLogFile(char* pFileName, bool bOverWrite)
{
    m_bOverWrite=bOverWrite;

    m_pSavbuf[0]=0;
    m_Savbuflen =0;

	strcpy(m_logfilename,pFileName);

	NewLogFile(bOverWrite);
}

COneLogFile::~COneLogFile()
{
	CloseLogFile();
}

void COneLogFile::NewLogFile(bool bOverWrite)
{
    if (!bOverWrite) return;

	time_t Timestamp;
    struct tm *pTime;
    time(&Timestamp);
    pTime=localtime(&Timestamp);

	sprintf(m_pSavbuf,"LOG BEGIN :  %02d-%02d-%02d %02d:%02d:%02d\n\n",
		     pTime->tm_year % 100,pTime->tm_mon+1,pTime->tm_mday,
			 pTime->tm_hour,pTime->tm_min,pTime->tm_sec);

    SaveDataToFile(m_pSavbuf,bOverWrite);
}

void COneLogFile::Appends(const char* pszFormat, ...)
{
    OTSTR   str;
    va_list arglist;
    
    va_start(arglist, pszFormat);
    str.Format_(pszFormat, arglist);
    va_end(arglist);
    
    Append(str.GetBuffer(0));
}
    
void COneLogFile::Append(LPCTSTR lpstrLog)
{
    int n=strlen(lpstrLog);
    
    if ( m_Savbuflen+n > ONELOG_SAVBUFSIZE )
    {
        SaveDataToFile(m_pSavbuf);
        
        strcpy(m_pSavbuf,lpstrLog);
        m_Savbuflen=n;
    }
    else
    {
        strcat(m_pSavbuf,lpstrLog);
        m_Savbuflen+=n;
    }
}

void COneLogFile::Flush()
{
    if (m_Savbuflen>0) SaveDataToFile(m_pSavbuf);
}

void COneLogFile::CloseLogFile()
{
    if (m_bOverWrite)
    {
        time_t Timestamp;
        struct tm *pTime;
        time(&Timestamp);
        pTime=localtime(&Timestamp);
        sprintf(m_pSavbuf+m_Savbuflen,"\nLOG END   :  %02d-%02d-%02d %02d:%02d:%02d\n",
            pTime->tm_year % 100,pTime->tm_mon+1,pTime->tm_mday,
            pTime->tm_hour,pTime->tm_min,pTime->tm_sec);
        
    }
    SaveDataToFile(m_pSavbuf);
}

void COneLogFile::SaveDataToFile(LPCTSTR pbuf,bool bOverWrite)
{
	FILE*   fp;
    if (pbuf != NULL)
    {
	    fp=fopen(m_logfilename,(bOverWrite)? "w":"a");  //
		if (fp!=NULL)
		{
			fwrite(pbuf,strlen(pbuf),1,fp);
			fclose(fp);  //
		}
    }
    m_pSavbuf[0]=0;
  	m_Savbuflen =0; 
}


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
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions