123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- /********************************************************************/
- /* */
- /* LogFile.CPP */
- /* */
- /* This source file implements log functionality */
- /* */
- /* Programmed by LYFZ van der Meer */
- /* Last modified: 14 march 2002 */
- /* Copyright LYFZ Software Solutions 2002 */
- /* */
- /********************************************************************/
- #include "stdafx.h"
- #include <strstream>
- #include "LogFile.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CLogFile::CLogFile()
- {
- m_File = NULL;
- m_nLevel = 0;
- m_nCurrentLevel = 0;
- }
- CLogFile::~CLogFile()
- {
- Write("*** END OF APPLICATION ***");
- // close file, if still opened
- Close();
- }
- /****************************************************************/
- /* */
- /* Open File using date as part of the filename. */
- /* */
- /****************************************************************/
- void CLogFile::Open()
- {
- SYSTEMTIME sysTime;
- GetLocalTime(&sysTime);
- // save current date
- m_OldDate = sysTime.wDay;
-
- // create the log directory if it does not already exists
- CreateDirectory(m_szLogPath, NULL);
-
- // create file name
- wsprintf(m_FileName,"%s%04d%02d%02d.txt", m_szLogPath, sysTime.wYear, sysTime.wMonth, sysTime.wDay);
- Open(m_FileName);
- }
- /****************************************************************/
- /* */
- /* Open File with name from parameter. */
- /* */
- /****************************************************************/
- void CLogFile::Open(LPCTSTR lpszFileName)
- {
- // if already open close !
- if (m_File != NULL)
- Close();
- lstrcpy(m_FileName, lpszFileName);
- // create ofstream class for buffered disk file I/O
- m_File = new ofstream(lpszFileName, ios_base::out | ios::app);
- // successfull opened ?
- if (!m_File->is_open())
- {
- // failed to open file
- char strValue[256];
- wsprintf(strValue, "Cannot create or open file: '%s'", lpszFileName);
- MessageBox(NULL,(LPCTSTR)strValue,"Error in CLogFile::Open()",MB_OK | MB_ICONSTOP);
- delete m_File;
- m_File = NULL;
- }
- Write("*** START OF APPLICATION ***");
- }
- /****************************************************************/
- /* */
- /* Close current logfile. */
- /* */
- /****************************************************************/
- void CLogFile::Close()
- {
- // file not open...
- if (m_File == NULL)
- return;
-
- // close file
- m_File->close();
- // delete class
- delete m_File;
- m_File = NULL;
- }
- /****************************************************************/
- /* */
- /* Private Member function: Output data to file. */
- /* This is the only place where is actually written to the file.*/
- /* */
- /****************************************************************/
- void CLogFile::Output(const TCHAR *data)
- {
- // file not opened
- if (m_File == NULL)
- return;
- // do we need to log this data ?
- if (m_nCurrentLevel <= m_nLevel)
- {
- // write data to file
- m_File->write(data, _tcslen(data));
- m_File->flush();
- }
- }
- /****************************************************************/
- /* */
- /* Member function: Used for logging the time. */
- /* */
- /****************************************************************/
- void CLogFile::Time()
- {
- TCHAR szTime[15];
- SYSTEMTIME sysTime;
- GetLocalTime(&sysTime);
- wsprintf(szTime, "%02d:%02d:%02d.%03d ", sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
- Output(szTime);
- }
- void CLogFile::Date()
- {
- TCHAR szDate[12];
- SYSTEMTIME sysTime;
- GetLocalTime(&sysTime);
- wsprintf(szDate, "%02d/%02d/%02d ", sysTime.wDay, sysTime.wMonth, sysTime.wYear);
- Output(szDate);
- }
- /****************************************************************/
- /* */
- /* Operator: Used for logging 'unsigned int' types. */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(unsigned int unVal)
- {
- strstream tmp;
- tmp << unVal;
- tmp << '\0';
- TCHAR* output = tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Operator: Used for logging 'long' types. */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(long lVal)
- {
- strstream tmp;
- tmp << lVal;
- tmp << '\0';
- TCHAR* output = tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Operator: Used for logging 'char[]' types. */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(const TCHAR* str)
- {
- Output(str);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Operator: Used for logging 'char' types. */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(TCHAR tch)
- {
- TCHAR szCh[2];
- szCh[0] = tch;
- szCh[1] = '\0';
- Output(szCh);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Operator: Used for logging 'int' types. */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(int nVal)
- {
- strstream tmp;
- tmp << nVal;
- tmp << '\0';
- TCHAR* output = tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Operator: Used for logging 'unsigned long' types. */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(unsigned long ulVal)
- {
- strstream tmp;
- tmp << ulVal;
- tmp << '\0';
- TCHAR* output = tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Operator: Used for logging 'double' types. */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(double dVal)
- {
- strstream tmp;
- tmp << dVal;
- tmp << '\0';
- TCHAR* output = tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Operator: Necessary for manipulators 'time' and 'endl' */
- /* */
- /****************************************************************/
- CLogFile& CLogFile::operator <<(CLogFile& (*_f)( CLogFile& ) )
- {
- (*_f)(*this);
- return *this;
- }
- /****************************************************************/
- /* */
- /* Write line to log file, supports printf formatting. */
- /* */
- /****************************************************************/
- void CLogFile::Write(TCHAR* formatString, ...)
- {
- m_nCurrentLevel = 1;
- // Insert the current date
- Date();
- // Insert the current time
- Time();
- // Parse the format string and write to the file
- if (formatString == NULL)
- {
- // No point in continuiing
- return;
- }
-
- va_list argList;
- // Set va_list to the beginning of optional arguments
- va_start(argList, formatString);
- TCHAR* ptr = formatString;
- while(*ptr != '\0')
- {
- TCHAR* str = NULL;
- int nInteger = 0;
- unsigned int unInt = 0;
- long lLong = 0;
- unsigned long ulLong= 0;
- double dDoub = 0;
- if(*ptr == '%')
- {
- switch(*(ptr+1))
- {
- case 's':
- str = va_arg(argList, TCHAR*);
- if( NULL == str)
- break;
- *this << str;
- ptr++;
- break;
- case 'd':
- nInteger = va_arg(argList, int);
- *this << nInteger;
- ptr++;
- break;
- case 'u':
- unInt = va_arg(argList, unsigned int);
- *this << unInt;
- ptr++;
- break;
- case 'l':
- ptr++;
- if(*(ptr+1) == 'd')
- {
- lLong = va_arg(argList, long);
- *this << lLong;
- ptr++;
- }
- else if(*(ptr+1) == 'u')
- {
- ulLong = va_arg(argList, unsigned long);
- *this << ulLong;
- ptr++;
- }
- break;
- case 'f':
- dDoub = va_arg(argList, double);
- *this << dDoub;
- ptr++;
- break;
- default:
- *this << *ptr;
- }
- } // if(*ptr == '%')
- else
- {
- *this << *ptr;
- }
- // Increment pointer..
- ptr++;
- }
- *this << '\n';
- }
- /****************************************************************/
- /* */
- /* Manipulator to insert the time into log file. */
- /* */
- /****************************************************************/
- CLogFile& time(CLogFile& os)
- {
- TCHAR szTime[15];
- SYSTEMTIME sysTime;
-
- GetLocalTime(&sysTime);
- wsprintf(szTime, "%02d:%02d:%02d.%03d ", sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
- return os << szTime;
- }
- /****************************************************************/
- /* */
- /* Manipulator to insert the date into log file. */
- /* */
- /****************************************************************/
- CLogFile& date(CLogFile& os)
- {
- TCHAR szDate[12];
- SYSTEMTIME sysTime;
- GetLocalTime(&sysTime);
- wsprintf(szDate, "%02d/%02d/%02d ", sysTime.wDay, sysTime.wMonth, sysTime.wYear);
- return os << szDate;
- }
- /****************************************************************/
- /* */
- /* Manipulator to insert end of line into log file. */
- /* */
- /****************************************************************/
- CLogFile& endl(CLogFile& os)
- {
- return os << '\n';
- }
- void CLogFile::SetLogLevel(int nLevel)
- {
- m_nLevel = nLevel;
- }
- /* Clear current logfile */
- void CLogFile::Clear()
- {
- // close file before deleting
- Close();
- // remove log file
- DeleteFile((LPCTSTR)m_FileName);
- // create new file
- Open(m_FileName);
- Write("*** CLEARED LOGFILE ***");
- }
|