Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
6.3 KiB

  1. // GenLog.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "GenLog.h"
  5. CGenLog::CGenLog()
  6. : m_pFileHandle( NULL ), m_pWriteBuffer( NULL ), m_lWriteBufferLen( 0L )
  7. {
  8. }
  9. CGenLog::CGenLog
  10. (
  11. IN const char *pszFilePath
  12. )
  13. : m_pFileHandle( NULL ), m_pWriteBuffer( NULL ), m_lWriteBufferLen( 0L )
  14. {
  15. // _ASSERTE( pszFilePath != NULL );
  16. InitLog( pszFilePath, "w+" );
  17. }
  18. CGenLog::~CGenLog()
  19. {
  20. ResetGenLog();
  21. }
  22. long
  23. CGenLog::InitLog
  24. (
  25. IN const char *pszFilePath, /* = NULL */
  26. IN const char *pszMode, /* = NULL */
  27. IN const unsigned long lWriteBuffer /* = 256 */
  28. )
  29. {
  30. // _ASSERTE( pszFilePath != NULL );
  31. // _ASSERTE( pszMode != NULL );
  32. // _ASSERTE( lWriteBuffer > 0L );
  33. long lLastRet = GENLOG_ERROR_UNEXPECTED;
  34. FILE *pTempFile = NULL;
  35. char *pWriteBuff = NULL;
  36. __try {
  37. if( pszFilePath == NULL ||
  38. pszMode == NULL ||
  39. lWriteBuffer == 0L ) {
  40. lLastRet = GENLOG_ERROR_INVALIDARG;
  41. goto qInitLog;
  42. }
  43. ResetGenLog();
  44. pTempFile = fopen( pszFilePath, pszMode );
  45. if( !pTempFile ) {
  46. lLastRet = GENLOG_ERROR_FILEOPERATIONFAILED;
  47. goto qInitLog;
  48. }
  49. //
  50. // we allocate a byte more than what is required, so that we can
  51. // take care of av issues..
  52. //
  53. pWriteBuff = (char *) calloc( lWriteBuffer + 1, sizeof( char ) );
  54. // _ASSERTE( pWriteBuff != NULL );
  55. if( !pWriteBuff ) {
  56. lLastRet = GENLOG_ERROR_MEMORY;
  57. goto qInitLog;
  58. }
  59. lLastRet = GENLOG_SUCCESS;
  60. qInitLog:
  61. if( lLastRet == GENLOG_SUCCESS ) {
  62. m_pFileHandle = pTempFile;
  63. m_pWriteBuffer = pWriteBuff;
  64. m_lWriteBufferLen = lWriteBuffer;
  65. }
  66. else {
  67. // do the cleanup stuff here..
  68. if( pTempFile ) { fclose ( pTempFile ); }
  69. if( pWriteBuff ) { free( pWriteBuff ); }
  70. }
  71. }
  72. __except ( -1/*EXCEPTION_EXECUTE_HANDLER*/, 1 ) {
  73. lLastRet = GENLOG_ERROR_UNEXPECTED;
  74. if( pTempFile ) { fclose ( pTempFile ); }
  75. if( pWriteBuff ) { free( pWriteBuff ); }
  76. // _ASSERTE( false );
  77. }
  78. return lLastRet;
  79. }
  80. void
  81. CGenLog::Debug
  82. (
  83. IN const char *pDebugString,
  84. IN ...
  85. )
  86. {
  87. // _ASSERTE( pDebugString != NULL );
  88. // _ASSERTE( m_pFileHandle != NULL );
  89. // _ASSERTE( m_pWriteBuffer != NULL );
  90. __try {
  91. if( !pDebugString || !m_pFileHandle || !m_pWriteBuffer ) { return; }
  92. va_list argList;
  93. va_start(argList, pDebugString);
  94. _vsnprintf(m_pWriteBuffer, m_lWriteBufferLen, pDebugString, argList);
  95. Write();
  96. va_end(argList);
  97. }
  98. __except( -1/*EXCEPTION_EXECUTE_HANDLER*/, 1 ) {
  99. // _ASSERTE( false );
  100. }
  101. }
  102. void
  103. CGenLog::Error
  104. (
  105. IN const char *pErrorString,
  106. IN ...
  107. )
  108. {
  109. // _ASSERTE( pErrorString != NULL );
  110. // _ASSERTE( m_pFileHandle != NULL );
  111. // _ASSERTE( m_pWriteBuffer != NULL );
  112. __try {
  113. if( !pErrorString || !m_pFileHandle || !m_pWriteBuffer ) { return; }
  114. va_list argList;
  115. va_start(argList, pErrorString);
  116. _vsnprintf(m_pWriteBuffer, m_lWriteBufferLen, pErrorString, argList);
  117. Write();
  118. va_end(argList);
  119. }
  120. __except( -1/*EXCEPTION_EXECUTE_HANDLER*/, 1 ) {
  121. // _ASSERTE( false );
  122. }
  123. }
  124. void
  125. CGenLog::Log
  126. (
  127. IN const char *pLogString,
  128. IN ...
  129. )
  130. {
  131. // _ASSERTE( pLogString != NULL );
  132. // _ASSERTE( m_pFileHandle != NULL );
  133. // _ASSERTE( m_pWriteBuffer != NULL );
  134. __try {
  135. if( !pLogString || !m_pFileHandle || !m_pWriteBuffer ) { return; }
  136. va_list argList;
  137. va_start(argList, pLogString);
  138. _vsnprintf(m_pWriteBuffer, m_lWriteBufferLen, pLogString, argList);
  139. Write();
  140. va_end(argList);
  141. }
  142. __except( -1/*EXCEPTION_EXECUTE_HANDLER*/, 1 ) {
  143. // _ASSERTE( false );
  144. }
  145. }
  146. long
  147. CGenLog::Write()
  148. {
  149. // _ASSERTE( m_pFileHandle != NULL );
  150. // _ASSERTE( m_pWriteBuffer != NULL );
  151. long lLastRet = GENLOG_ERROR_UNEXPECTED;
  152. unsigned long lWrittenDataLen = 0L;
  153. __try {
  154. if( !m_pWriteBuffer || !m_pFileHandle ) {
  155. lLastRet = GENLOG_ERROR_INVALIDARG;
  156. goto qWrite;
  157. }
  158. if( !m_pFileHandle ) {
  159. lLastRet = GENLOG_ERROR_UNINITIALIZED;
  160. goto qWrite;
  161. }
  162. lWrittenDataLen = fwrite(
  163. (void *)m_pWriteBuffer,
  164. sizeof( char ),
  165. strlen( m_pWriteBuffer),
  166. m_pFileHandle
  167. );
  168. if( lWrittenDataLen != strlen( m_pWriteBuffer) ) {
  169. lLastRet = GENLOG_ERROR_FILEOPERATIONFAILED;
  170. goto qWrite;
  171. }
  172. lLastRet = GENLOG_SUCCESS;
  173. qWrite:
  174. if( lLastRet == GENLOG_SUCCESS ) {
  175. // great!!
  176. }
  177. else {
  178. // cleanup..
  179. }
  180. }
  181. __except ( -1 /*EXCEPTION_EXECUTE_HANDLER*/, 1 ) {
  182. lLastRet = GENLOG_ERROR_UNEXPECTED;
  183. // _ASSERTE( false );
  184. }
  185. return lLastRet;
  186. }
  187. void
  188. CGenLog::ResetGenLog()
  189. {
  190. if( m_pFileHandle ) { fclose( m_pFileHandle ); m_pFileHandle = NULL; }
  191. if( m_pWriteBuffer ) { free( (void *) m_pWriteBuffer ); m_pWriteBuffer = NULL; }
  192. m_lWriteBufferLen = GENLOG_DEFAULT_WRITEBUFSIZE;
  193. }
  194. void
  195. CGenLog::Header
  196. (
  197. IN const char *pszHeaderString
  198. )
  199. {
  200. const char *pszHead = "---------------------------------------------------\n";
  201. __try {
  202. if( !pszHeaderString ) {
  203. strcpy( m_pWriteBuffer, pszHead );
  204. Write();
  205. Now();
  206. strcpy( m_pWriteBuffer, pszHead );
  207. Write();
  208. }
  209. else {
  210. strncpy( m_pWriteBuffer, pszHeaderString, m_lWriteBufferLen );
  211. Write();
  212. }
  213. }
  214. __except ( -1 /*EXCEPTION_EXECUTE_HANDLER*/, 1 ) {
  215. // _ASSERTE( false );
  216. }
  217. }
  218. void
  219. CGenLog::Now()
  220. {
  221. time_t timeNow = ::time(NULL);
  222. struct tm *ptmTemp = localtime(&timeNow);
  223. const char *pFormat = "%H : %M : %S - %A, %B %d, %Y\n";
  224. __try {
  225. if (ptmTemp == NULL ||
  226. !strftime(m_pWriteBuffer, m_lWriteBufferLen, pFormat, ptmTemp))
  227. m_pWriteBuffer[0] = '\0';
  228. Write();
  229. }
  230. __except ( -1 /*EXCEPTION_EXECUTE_HANDLER*/, 1 ) {
  231. // _ASSERTE( false );
  232. }
  233. }