Monthly Archives: October 2012

CRT Rocks

Compared to these days, CRT programming could be think as deprecated but it is false. CRT is on the edge of C programming.

Advertisements

OK, now enter the logging libraries area

In 1997, I made a web server for a regional bank using Visual C++ and MFC ISAPI. A great great great experience. You think than now you have ASP.NET but the real hardcore stuff are below, and ISAPI bring this. MFC ISAPI is a wonderfull technology. You have to respect every aspect of the web server. You have to provide your own mechanism for every thing from log to session stuff.

enum Mode

{

debug,

error

};

#define LogError(X,Y) Log(X,Y,__FILE__,__LINE__, error)

#define LogDebug(X,Y) Log(X,Y,__FILE__,__LINE__, debug)

class CInternetTrace

{

public:

static Mode m_Mode;

static int m_iCptRef;

static CString m_PathDirectory;

public:

_CERAPI_ CInternetTrace(CString strServiceName);

_CERAPI_ ~CInternetTrace();

_CERAPI_ void Log(LPCSTR szMethod,LPCSTR szMsg, LPCSTR szFile, int iLine, Mode modeAEcrire);

_CERAPI_ void Log2(LPCSTR szMethod,LPCSTR szMsg, LPCSTR szFile, int iLine, Mode modeAEcrire);

_CERAPI_ void Init(CString strServiceName);

};

#define INT_TRACE(x) \

{ \

CInternetTrace theTrace("Internet"); \

theTrace.LogDebug(x,""); \

} \

int CInternetTrace::m_iCptRef=0;

CString CInternetTrace::m_PathDirectory;

Mode CInternetTrace::m_Mode=debug;

static CInternetTrace theTrace(TRACE_INTERNET);

//////////////////////////////////////////////////////////////////

// CInternetTrace

//////////////////////////////////////////////////////////////////

BOOL GetRegValue(char *szChemin, char *szValeur, UCHAR *szBuffer)

{

CerRegKey RegKey(HKEY_LOCAL_MACHINE);

DWORD dwCount;

dwCount=256;

if(RegKey.OpenSubKey(szChemin,&RegKey,KEY_READ) == FALSE)

return FALSE;

RegKey.GetValue(szValeur,(UCHAR *)szBuffer,&dwCount);

RegKey.Close();

return TRUE;

}

CInternetTrace::CInternetTrace(CString strServiceName)

{

if( m_iCptRef==0 )

{

csTrace.Unlock();

Init(strServiceName);

}

m_iCptRef++;

}

CInternetTrace::~CInternetTrace()

{

m_iCptRef--;

}

void CInternetTrace::Init(CString strServiceName)

{

char szBuf[256];

if( GetRegValue(REG_TRACE, "Path",(UCHAR *)szBuf) == FALSE)

m_PathDirectory = "d:\\srv\\trace";

else

m_PathDirectory.Format("%s\\%s",szBuf,"CI"); //strServiceName);

if( GetRegValue(REG_TRACE, (LPSTR)(LPCSTR)strServiceName,(UCHAR *)szBuf) == FALSE)

m_Mode = debug;

else

{

CString Level;

Level = szBuf;

Level.MakeUpper();

if( Level == "ERROR" )

m_Mode = error;

else

m_Mode = debug;

}

}

void CInternetTrace::Log(LPCSTR szMethod,LPCSTR szMsg, LPCSTR szFile, int iLine, Mode modeAEcrire)

{

try

{

if( (modeAEcrire == debug) && (m_Mode == error) )

return;

csTrace.Lock();

CTime t=CTime::GetCurrentTime();

CString str;

str.Format("%02d/%02d/%02d %02d:%02d:%02d, %s, %s, %s, %d\n", t.GetDay(), t.GetMonth(), t.GetYear()%100, t.GetHour(), t.GetMinute(), t.GetSecond(), szMethod, szMsg, szFile, iLine);

CString strFileName;

strFileName.Format("%s%02d%02d%02d.txt",m_PathDirectory,t.GetDay(), t.GetMonth(), t.GetYear()%100);

CStdioFile file;

HANDLE hFileExist;

WIN32_FIND_DATA data;

hFileExist=FindFirstFile( strFileName, &data );

if( hFileExist==INVALID_HANDLE_VALUE )

{

if( file.Open(strFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeText)==FALSE )

{

FindClose(hFileExist);

Log2(szMethod, szMsg, szFile, iLine, modeAEcrire);

csTrace.Unlock();

return;

}

}

else

{

if( file.Open(strFileName, CFile::modeReadWrite | CFile::typeText)==FALSE )

{

FindClose(hFileExist);

Log2(szMethod, szMsg, szFile, iLine, modeAEcrire);

csTrace.Unlock();

return;

}

}

//CStdioFile file;

//if( file.Open(strFileName, CFile::modeReadWrite | CFile::typeText)==FALSE )

// if( file.Open(strFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeText)==FALSE )

// return;

FindClose(hFileExist);

file.Seek(0, CFile::end);

file.WriteString(str);

file.Close();

csTrace.Unlock();

}

catch( ... )

{

csTrace.Unlock();

Log2("CInternetTrace::Log","echec","techint.cpp",0,debug);

}

}

void CInternetTrace::Log2(LPCSTR szMethod,LPCSTR szMsg, LPCSTR szFile, int iLine, Mode modeAEcrire)

{

try

{

CTime t=CTime::GetCurrentTime();

CString str;

str.Format("%02d/%02d/%02d %02d:%02d:%02d, %s, %s, %s, %d\n", t.GetDay(), t.GetMonth(), t.GetYear()%100, t.GetHour(), t.GetMinute(), t.GetSecond(), szMethod, szMsg, szFile, iLine);

CString strFileName;

strFileName.Format("%sLocked.txt",m_PathDirectory);

CStdioFile file;

HANDLE hFileExist;

WIN32_FIND_DATA data;

hFileExist=FindFirstFile( strFileName, &data );

if( hFileExist==INVALID_HANDLE_VALUE )

{

if( file.Open(strFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeText)==FALSE )

{

FindClose(hFileExist);

return;

}

}

else

{

if( file.Open(strFileName, CFile::modeReadWrite | CFile::typeText)==FALSE )

{

FindClose(hFileExist);

return;

}

}

//CStdioFile file;

//if( file.Open(strFileName, CFile::modeReadWrite | CFile::typeText)==FALSE )

// if( file.Open(strFileName, CFile::modeWrite | CFile::modeCreate | CFile::typeText)==FALSE )

// return;

FindClose(hFileExist);

file.Seek(0, CFile::end);

file.WriteString(str);

file.Close();

}

catch( ... )

{

}

}

When C is good. CRT rocks.

It is time to be short and speed. No framework. No libraries. Just log entries in a file. Thanks CRT.

#define TRACE_APP(sz) { FILE *file; file=fopen("c:\\services\\trace.txt","a"); fprintf(file,"%s\n",(const char *)sz); fclose(file); }