logfile.cpp 4.3 KB

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