LogFile.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /********************************************************************/
  2. /* */
  3. /* LogFile.CPP */
  4. /* */
  5. /* This source file implements log functionality */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Last modified: 14 march 2002 */
  9. /* Copyright LYFZ Software Solutions 2002 */
  10. /* */
  11. /********************************************************************/
  12. #include "stdafx.h"
  13. #include <strstream>
  14. #include "LogFile.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. CLogFile::CLogFile()
  21. {
  22. m_File = NULL;
  23. m_nLevel = 0;
  24. m_nCurrentLevel = 0;
  25. }
  26. CLogFile::~CLogFile()
  27. {
  28. Write("*** END OF APPLICATION ***");
  29. // close file, if still opened
  30. Close();
  31. }
  32. /****************************************************************/
  33. /* */
  34. /* Open File using date as part of the filename. */
  35. /* */
  36. /****************************************************************/
  37. void CLogFile::Open()
  38. {
  39. SYSTEMTIME sysTime;
  40. GetLocalTime(&sysTime);
  41. // save current date
  42. m_OldDate = sysTime.wDay;
  43. // create the log directory if it does not already exists
  44. CreateDirectory(m_szLogPath, NULL);
  45. // create file name
  46. wsprintf(m_FileName,"%s%04d%02d%02d.txt", m_szLogPath, sysTime.wYear, sysTime.wMonth, sysTime.wDay);
  47. Open(m_FileName);
  48. }
  49. /****************************************************************/
  50. /* */
  51. /* Open File with name from parameter. */
  52. /* */
  53. /****************************************************************/
  54. void CLogFile::Open(LPCTSTR lpszFileName)
  55. {
  56. // if already open close !
  57. if (m_File != NULL)
  58. Close();
  59. lstrcpy(m_FileName, lpszFileName);
  60. // create ofstream class for buffered disk file I/O
  61. m_File = new ofstream(lpszFileName, ios_base::out | ios::app);
  62. // successfull opened ?
  63. if (!m_File->is_open())
  64. {
  65. // failed to open file
  66. char strValue[256];
  67. wsprintf(strValue, "Cannot create or open file: '%s'", lpszFileName);
  68. MessageBox(NULL,(LPCTSTR)strValue,"Error in CLogFile::Open()",MB_OK | MB_ICONSTOP);
  69. delete m_File;
  70. m_File = NULL;
  71. }
  72. Write("*** START OF APPLICATION ***");
  73. }
  74. /****************************************************************/
  75. /* */
  76. /* Close current logfile. */
  77. /* */
  78. /****************************************************************/
  79. void CLogFile::Close()
  80. {
  81. // file not open...
  82. if (m_File == NULL)
  83. return;
  84. // close file
  85. m_File->close();
  86. // delete class
  87. delete m_File;
  88. m_File = NULL;
  89. }
  90. /****************************************************************/
  91. /* */
  92. /* Private Member function: Output data to file. */
  93. /* This is the only place where is actually written to the file.*/
  94. /* */
  95. /****************************************************************/
  96. void CLogFile::Output(const TCHAR *data)
  97. {
  98. // file not opened
  99. if (m_File == NULL)
  100. return;
  101. // do we need to log this data ?
  102. if (m_nCurrentLevel <= m_nLevel)
  103. {
  104. // write data to file
  105. m_File->write(data, _tcslen(data));
  106. m_File->flush();
  107. }
  108. }
  109. /****************************************************************/
  110. /* */
  111. /* Member function: Used for logging the time. */
  112. /* */
  113. /****************************************************************/
  114. void CLogFile::Time()
  115. {
  116. TCHAR szTime[15];
  117. SYSTEMTIME sysTime;
  118. GetLocalTime(&sysTime);
  119. wsprintf(szTime, "%02d:%02d:%02d.%03d ", sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
  120. Output(szTime);
  121. }
  122. void CLogFile::Date()
  123. {
  124. TCHAR szDate[12];
  125. SYSTEMTIME sysTime;
  126. GetLocalTime(&sysTime);
  127. wsprintf(szDate, "%02d/%02d/%02d ", sysTime.wDay, sysTime.wMonth, sysTime.wYear);
  128. Output(szDate);
  129. }
  130. /****************************************************************/
  131. /* */
  132. /* Operator: Used for logging 'unsigned int' types. */
  133. /* */
  134. /****************************************************************/
  135. CLogFile& CLogFile::operator <<(unsigned int unVal)
  136. {
  137. strstream tmp;
  138. tmp << unVal;
  139. tmp << '\0';
  140. TCHAR* output = tmp.str();
  141. Output(output);
  142. tmp.freeze(false);
  143. return *this;
  144. }
  145. /****************************************************************/
  146. /* */
  147. /* Operator: Used for logging 'long' types. */
  148. /* */
  149. /****************************************************************/
  150. CLogFile& CLogFile::operator <<(long lVal)
  151. {
  152. strstream tmp;
  153. tmp << lVal;
  154. tmp << '\0';
  155. TCHAR* output = tmp.str();
  156. Output(output);
  157. tmp.freeze(false);
  158. return *this;
  159. }
  160. /****************************************************************/
  161. /* */
  162. /* Operator: Used for logging 'char[]' types. */
  163. /* */
  164. /****************************************************************/
  165. CLogFile& CLogFile::operator <<(const TCHAR* str)
  166. {
  167. Output(str);
  168. return *this;
  169. }
  170. /****************************************************************/
  171. /* */
  172. /* Operator: Used for logging 'char' types. */
  173. /* */
  174. /****************************************************************/
  175. CLogFile& CLogFile::operator <<(TCHAR tch)
  176. {
  177. TCHAR szCh[2];
  178. szCh[0] = tch;
  179. szCh[1] = '\0';
  180. Output(szCh);
  181. return *this;
  182. }
  183. /****************************************************************/
  184. /* */
  185. /* Operator: Used for logging 'int' types. */
  186. /* */
  187. /****************************************************************/
  188. CLogFile& CLogFile::operator <<(int nVal)
  189. {
  190. strstream tmp;
  191. tmp << nVal;
  192. tmp << '\0';
  193. TCHAR* output = tmp.str();
  194. Output(output);
  195. tmp.freeze(false);
  196. return *this;
  197. }
  198. /****************************************************************/
  199. /* */
  200. /* Operator: Used for logging 'unsigned long' types. */
  201. /* */
  202. /****************************************************************/
  203. CLogFile& CLogFile::operator <<(unsigned long ulVal)
  204. {
  205. strstream tmp;
  206. tmp << ulVal;
  207. tmp << '\0';
  208. TCHAR* output = tmp.str();
  209. Output(output);
  210. tmp.freeze(false);
  211. return *this;
  212. }
  213. /****************************************************************/
  214. /* */
  215. /* Operator: Used for logging 'double' types. */
  216. /* */
  217. /****************************************************************/
  218. CLogFile& CLogFile::operator <<(double dVal)
  219. {
  220. strstream tmp;
  221. tmp << dVal;
  222. tmp << '\0';
  223. TCHAR* output = tmp.str();
  224. Output(output);
  225. tmp.freeze(false);
  226. return *this;
  227. }
  228. /****************************************************************/
  229. /* */
  230. /* Operator: Necessary for manipulators 'time' and 'endl' */
  231. /* */
  232. /****************************************************************/
  233. CLogFile& CLogFile::operator <<(CLogFile& (*_f)( CLogFile& ) )
  234. {
  235. (*_f)(*this);
  236. return *this;
  237. }
  238. /****************************************************************/
  239. /* */
  240. /* Write line to log file, supports printf formatting. */
  241. /* */
  242. /****************************************************************/
  243. void CLogFile::Write(TCHAR* formatString, ...)
  244. {
  245. m_nCurrentLevel = 1;
  246. // Insert the current date
  247. Date();
  248. // Insert the current time
  249. Time();
  250. // Parse the format string and write to the file
  251. if (formatString == NULL)
  252. {
  253. // No point in continuiing
  254. return;
  255. }
  256. va_list argList;
  257. // Set va_list to the beginning of optional arguments
  258. va_start(argList, formatString);
  259. TCHAR* ptr = formatString;
  260. while(*ptr != '\0')
  261. {
  262. TCHAR* str = NULL;
  263. int nInteger = 0;
  264. unsigned int unInt = 0;
  265. long lLong = 0;
  266. unsigned long ulLong= 0;
  267. double dDoub = 0;
  268. if(*ptr == '%')
  269. {
  270. switch(*(ptr+1))
  271. {
  272. case 's':
  273. str = va_arg(argList, TCHAR*);
  274. if( NULL == str)
  275. break;
  276. *this << str;
  277. ptr++;
  278. break;
  279. case 'd':
  280. nInteger = va_arg(argList, int);
  281. *this << nInteger;
  282. ptr++;
  283. break;
  284. case 'u':
  285. unInt = va_arg(argList, unsigned int);
  286. *this << unInt;
  287. ptr++;
  288. break;
  289. case 'l':
  290. ptr++;
  291. if(*(ptr+1) == 'd')
  292. {
  293. lLong = va_arg(argList, long);
  294. *this << lLong;
  295. ptr++;
  296. }
  297. else if(*(ptr+1) == 'u')
  298. {
  299. ulLong = va_arg(argList, unsigned long);
  300. *this << ulLong;
  301. ptr++;
  302. }
  303. break;
  304. case 'f':
  305. dDoub = va_arg(argList, double);
  306. *this << dDoub;
  307. ptr++;
  308. break;
  309. default:
  310. *this << *ptr;
  311. }
  312. } // if(*ptr == '%')
  313. else
  314. {
  315. *this << *ptr;
  316. }
  317. // Increment pointer..
  318. ptr++;
  319. }
  320. *this << '\n';
  321. }
  322. /****************************************************************/
  323. /* */
  324. /* Manipulator to insert the time into log file. */
  325. /* */
  326. /****************************************************************/
  327. CLogFile& time(CLogFile& os)
  328. {
  329. TCHAR szTime[15];
  330. SYSTEMTIME sysTime;
  331. GetLocalTime(&sysTime);
  332. wsprintf(szTime, "%02d:%02d:%02d.%03d ", sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
  333. return os << szTime;
  334. }
  335. /****************************************************************/
  336. /* */
  337. /* Manipulator to insert the date into log file. */
  338. /* */
  339. /****************************************************************/
  340. CLogFile& date(CLogFile& os)
  341. {
  342. TCHAR szDate[12];
  343. SYSTEMTIME sysTime;
  344. GetLocalTime(&sysTime);
  345. wsprintf(szDate, "%02d/%02d/%02d ", sysTime.wDay, sysTime.wMonth, sysTime.wYear);
  346. return os << szDate;
  347. }
  348. /****************************************************************/
  349. /* */
  350. /* Manipulator to insert end of line into log file. */
  351. /* */
  352. /****************************************************************/
  353. CLogFile& endl(CLogFile& os)
  354. {
  355. return os << '\n';
  356. }
  357. void CLogFile::SetLogLevel(int nLevel)
  358. {
  359. m_nLevel = nLevel;
  360. }
  361. /* Clear current logfile */
  362. void CLogFile::Clear()
  363. {
  364. // close file before deleting
  365. Close();
  366. // remove log file
  367. DeleteFile((LPCTSTR)m_FileName);
  368. // create new file
  369. Open(m_FileName);
  370. Write("*** CLEARED LOGFILE ***");
  371. }