logfile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "stdafx.h"
  2. #include <strstream>
  3. #include "logfile.h"
  4. CLogFile::CLogFile()
  5. {
  6. /*
  7. ** Open File
  8. */
  9. m_cOutFile = new ofstream("D:\\avirecordfile\\log.log", ios_base::out);
  10. if (!m_cOutFile->is_open())
  11. {
  12. MessageBox(NULL,"Unable to create log file","´íÎó", MB_OK|MB_ICONSTOP);
  13. delete m_cOutFile;
  14. m_cOutFile = NULL;
  15. }
  16. }
  17. CLogFile::CLogFile(char* tszFileName)
  18. {
  19. m_cOutFile = new ofstream(tszFileName, ios_base::out);
  20. if (!m_cOutFile->is_open())
  21. {
  22. MessageBox(NULL,"Unable to create log file","´íÎó", MB_OK|MB_ICONSTOP);
  23. delete m_cOutFile;
  24. m_cOutFile = NULL;
  25. }
  26. }
  27. CLogFile::~CLogFile()
  28. {
  29. /*
  30. ** Close the file and do clean up
  31. */
  32. m_cOutFile->close();
  33. delete m_cOutFile;
  34. m_cOutFile = NULL;
  35. }
  36. void CLogFile::Output(const TCHAR *data)
  37. {
  38. m_cOutFile->write(data,strlen(data));
  39. }
  40. void CLogFile::PrintCurTime()
  41. {
  42. TCHAR dateString[52];
  43. SYSTEMTIME cur;
  44. GetSystemTime(&cur);
  45. sprintf(dateString,"[%d-%d-%d,%d:%d:%d] ", cur.wYear, cur.wMonth,
  46. cur.wDay, cur.wHour, cur.wMinute, cur.wSecond);
  47. Output(dateString);
  48. }
  49. CLogFile& CLogFile::operator <<(unsigned int unVal)
  50. {
  51. strstream tmp;
  52. tmp << unVal;
  53. tmp << '\0';
  54. TCHAR* output = tmp.str();
  55. Output(output);
  56. tmp.freeze(false);
  57. return *this;
  58. }
  59. CLogFile& CLogFile::operator <<(long lVal)
  60. {
  61. strstream tmp;
  62. tmp << lVal;
  63. tmp << '\0';
  64. TCHAR* output = tmp.str();
  65. Output(output);
  66. tmp.freeze(false);
  67. return *this;
  68. }
  69. CLogFile& CLogFile::operator <<(const TCHAR* str)
  70. {
  71. Output(str);
  72. return *this;
  73. }
  74. CLogFile& CLogFile::operator <<(TCHAR tch)
  75. {
  76. TCHAR szCh[2];
  77. szCh[0] = tch;
  78. szCh[1] = '\0';
  79. Output(szCh);
  80. return *this;
  81. }
  82. CLogFile& CLogFile::operator <<(int nVal)
  83. {
  84. strstream tmp;
  85. tmp << nVal;
  86. tmp << '\0';
  87. TCHAR* output = tmp.str();
  88. Output(output);
  89. tmp.freeze(false);
  90. return *this;
  91. }
  92. CLogFile& CLogFile::operator <<(unsigned long ulVal)
  93. {
  94. strstream tmp;
  95. tmp << ulVal;
  96. tmp << '\0';
  97. TCHAR* output = tmp.str();
  98. Output(output);
  99. tmp.freeze(false);
  100. return *this;
  101. }
  102. CLogFile& CLogFile::operator <<(double dVal)
  103. {
  104. strstream tmp;
  105. tmp << dVal;
  106. tmp << '\0';
  107. TCHAR* output = tmp.str();
  108. Output(output);
  109. tmp.freeze(false);
  110. return *this;
  111. }
  112. CLogFile& CLogFile::operator <<(unsigned __int64 unllVal)
  113. {
  114. strstream tmp;
  115. tmp << unllVal;
  116. tmp << '\0';
  117. TCHAR* output = tmp.str();
  118. Output(output);
  119. tmp.freeze(false);
  120. return *this;
  121. }
  122. void CLogFile::LOGERROR(TCHAR* formatString, ...)
  123. {
  124. /*
  125. ** Insert the current time..
  126. */
  127. PrintCurTime();
  128. /*
  129. ** Parse the format string and write to the file
  130. */
  131. if ( formatString == NULL)
  132. {
  133. /*
  134. ** No point in continuiing
  135. */
  136. return;
  137. }
  138. va_list argList;
  139. /*
  140. ** Set va_list to the beginning of optional arguments
  141. */
  142. va_start(argList, formatString);
  143. TCHAR* ptr = formatString;
  144. while(*ptr != '\0')
  145. {
  146. TCHAR* str = NULL;
  147. int nInteger = 0;
  148. unsigned int unInt = 0;
  149. long lLong = 0;
  150. unsigned long ulLong= 0;
  151. double dDoub = 0;
  152. unsigned __int64 ullLong = 0;
  153. if(*ptr == '%')
  154. {
  155. switch(*(ptr+1))
  156. {
  157. case 's':
  158. str = va_arg(argList, TCHAR*);
  159. if( NULL == str)
  160. break;
  161. *this << str;
  162. ptr++;
  163. break;
  164. case 'd':
  165. nInteger = va_arg(argList, int);
  166. *this << nInteger;
  167. ptr++;
  168. break;
  169. case 'u':
  170. unInt = va_arg(argList, unsigned int);
  171. *this << unInt;
  172. ptr++;
  173. break;
  174. case 'l':
  175. ptr++;
  176. if(*(ptr+1) == 'd')
  177. {
  178. lLong = va_arg(argList, long);
  179. *this << lLong;
  180. ptr++;
  181. }
  182. else if(*(ptr+1) == 'u')
  183. {
  184. ulLong = va_arg(argList, unsigned long);
  185. *this << ulLong;
  186. ptr++;
  187. }
  188. else if(*(ptr+1) == 'q')
  189. {
  190. ullLong = va_arg(argList, unsigned __int64);
  191. *this << ullLong;
  192. ptr++;
  193. }
  194. break;
  195. case 'f':
  196. dDoub = va_arg(argList, double);
  197. *this << dDoub;
  198. ptr++;
  199. break;
  200. default:
  201. *this << *ptr;
  202. }
  203. } // if(*ptr == '%')
  204. else
  205. {
  206. *this << *ptr;
  207. }
  208. /*
  209. ** Increment pointer..
  210. */
  211. ptr++;
  212. }
  213. *this << '\n';
  214. }