123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- #include "stdafx.h"
- #include <strstream>
- #include "logfile.h"
- CLogFile::CLogFile()
- : m_cOutFile(NULL)
- {
- strncpy_s(logDir, "", sizeof(logDir));
- }
- CLogFile::CLogFile(const char* tszFileName)
- : m_cOutFile(NULL)
- {
- reset(tszFileName);
- }
- void CLogFile::Init(const char *filePath)
- {
- m_cOutFile = new ofstream(filePath, ios_base::app);
- if (!m_cOutFile->is_open())
- {
- MessageBox(NULL, _T("Unable to create log file"), filePath, MB_OK | MB_ICONSTOP);
- delete m_cOutFile;
- m_cOutFile = NULL;
- }
- }
- void CLogFile::reset(const char *filePath)
- {
- UnInit();
- strncpy_s(logDir, filePath, strlen(filePath));
- }
- CLogFile::~CLogFile()
- {
- UnInit();
- }
- void CLogFile::UnInit()
- {
- if (NULL != m_cOutFile)
- {
- m_cOutFile->close();
- delete m_cOutFile;
- m_cOutFile = NULL;
- }
- }
- void CLogFile::Output(const char *data)
- {
- char logPath[300] = "";
- SYSTEMTIME cur;
- GetLocalTime(&cur);
- sprintf_s(logPath, "%s\\%4d%.2d%.2d.txt", logDir, cur.wYear, cur.wMonth, cur.wDay);
- Init(logPath);
- if (NULL == m_cOutFile)
- return;
- m_cOutFile->write((const char*)data,strlen((const char*)data));
- UnInit();
- }
- void CLogFile::PrintCurTime()
- {
- char dateString[52];
- SYSTEMTIME cur;
- GetLocalTime(&cur);
- sprintf(dateString, "[%4d-%.2d-%.2d,%.2d:%.2d:%.2d] ", cur.wYear, cur.wMonth,
- cur.wDay, cur.wHour, cur.wMinute, cur.wSecond);
- Output(dateString);
- }
- CLogFile& CLogFile::operator <<(unsigned int unVal)
- {
- strstream tmp;
- tmp << unVal;
- tmp << '\0';
- char* output = (char*)tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- CLogFile& CLogFile::operator <<(long lVal)
- {
- strstream tmp;
- tmp << lVal;
- tmp << '\0';
- char* output = (char*)tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- CLogFile& CLogFile::operator <<(const char* str)
- {
- Output(str);
- return *this;
- }
- CLogFile& CLogFile::operator <<(char tch)
- {
- char szCh[2];
- szCh[0] = tch;
- szCh[1] = '\0';
- Output(szCh);
- return *this;
- }
- CLogFile& CLogFile::operator <<(int nVal)
- {
- strstream tmp;
- tmp << nVal;
- tmp << '\0';
- char* output = (char*)tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- CLogFile& CLogFile::operator <<(unsigned long ulVal)
- {
- strstream tmp;
- tmp << ulVal;
- tmp << '\0';
- char* output = (char*)tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- CLogFile& CLogFile::operator <<(double dVal)
- {
- strstream tmp;
- tmp << dVal;
- tmp << '\0';
- char* output = (char*)tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- CLogFile& CLogFile::operator <<(unsigned __int64 unllVal)
- {
- strstream tmp;
- tmp << unllVal;
- tmp << '\0';
- char* output = tmp.str();
- Output(output);
- tmp.freeze(false);
- return *this;
- }
- void CLogFile::LOGERROR(char* formatString, ...)
- {
- /*
- ** Insert the current time..
- */
- PrintCurTime();
- /*
- ** 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);
- char* ptr = formatString;
- while(*ptr != '\0')
- {
- char* str = NULL;
- int nInteger = 0;
- unsigned int unInt = 0;
- long lLong = 0;
- unsigned long ulLong= 0;
- double dDoub = 0;
- unsigned __int64 ullLong = 0;
- if(*ptr == '%')
- {
- switch(*(ptr+1))
- {
- case 's':
- str = va_arg(argList, char*);
- 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++;
- }
- else if(*(ptr+1) == 'q')
- {
- ullLong = va_arg(argList, unsigned __int64);
- *this << ullLong;
- 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';
- }
|