Leaked source code of windows server 2003
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.

307 lines
6.4 KiB

  1. // DbgLogr.cpp : This file contains the
  2. // Created: Dec '97
  3. // Author : a-rakeba
  4. // History:
  5. // Copyright (C) 1997 Microsoft Corporation
  6. // All rights reserved.
  7. // Microsoft Confidential
  8. #include "cmnhdr.h"
  9. #include <windows.h>
  10. #include <iostream.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <sys/timeb.h>
  14. #include <time.h>
  15. #include <assert.h>
  16. #include "DbgLogr.h"
  17. #include <tlntutils.h>
  18. using namespace _Utils;
  19. LPSTR CDebugLogger::s_lpszLogFileName = 0;
  20. HANDLE CDebugLogger::s_hMutex = 0;
  21. ostream* CDebugLogger::s_pOutput = 0;
  22. bool
  23. CDebugLogger::Init
  24. (
  25. DWORD dwDebugLvl,
  26. LPCSTR lpszLogFileName
  27. )
  28. {
  29. Synchronize();
  30. if (! TlntSynchronizeOn(s_hMutex))
  31. {
  32. return false;
  33. }
  34. CDebugLevel::TurnOffAll();
  35. CDebugLevel::TurnOn( dwDebugLvl );
  36. if( lpszLogFileName == NULL )
  37. {
  38. if( s_pOutput != &cerr )
  39. {
  40. delete s_pOutput;
  41. s_pOutput = &cerr;
  42. }
  43. ReleaseMutex( s_hMutex );
  44. return ( true );
  45. }
  46. LogToFile( lpszLogFileName );
  47. ReleaseMutex( s_hMutex );
  48. return ( true );
  49. }
  50. void CDebugLogger::Synchronize( void )
  51. {
  52. if( s_hMutex != NULL )
  53. return;
  54. s_hMutex = TnCreateMutex( NULL, FALSE, 0 );
  55. assert( s_hMutex != NULL );
  56. }
  57. void CDebugLogger::ShutDown( void )
  58. {
  59. // WaitForSingleObject( s_hMutex, INFINITE );
  60. CDebugLevel::TurnOffAll();
  61. if( s_pOutput != &cerr )
  62. {
  63. ( ( ofstream* ) s_pOutput )->close();
  64. delete (ofstream*)s_pOutput;
  65. }
  66. s_pOutput = NULL;
  67. delete [] s_lpszLogFileName;
  68. s_lpszLogFileName = NULL;
  69. TELNET_CLOSE_HANDLE( s_hMutex );
  70. }
  71. void
  72. CDebugLogger::OutMessage
  73. (
  74. DWORD dwDebugLvl,
  75. LPSTR lpszFmtStr,
  76. ...
  77. )
  78. {
  79. if (TlntSynchronizeOn(s_hMutex))
  80. {
  81. assert( s_pOutput != NULL );
  82. if( CDebugLevel::IsCurrLevel( dwDebugLvl ) )
  83. {
  84. CHAR *szBuf = new CHAR[ 2 * BUFF_SIZE ];
  85. if (szBuf)
  86. {
  87. va_list arg;
  88. va_start( arg, lpszFmtStr );
  89. vsprintf( szBuf, lpszFmtStr, arg );
  90. va_end( arg );
  91. *s_pOutput << "Thread ID : "<< GetCurrentThreadId() << "\n\t" << szBuf
  92. << endl;
  93. delete [] szBuf;
  94. }
  95. }
  96. ReleaseMutex( s_hMutex );
  97. }
  98. }
  99. void
  100. CDebugLogger::OutMessage
  101. (
  102. DWORD dwDebugLvl,
  103. LPTSTR lpszFmtStr,
  104. ...
  105. )
  106. {
  107. if (TlntSynchronizeOn(s_hMutex))
  108. {
  109. assert( s_pOutput != NULL );
  110. if( CDebugLevel::IsCurrLevel( dwDebugLvl ) )
  111. {
  112. WCHAR *szBuf = new WCHAR[ 2 * BUFF_SIZE ];
  113. if( szBuf )
  114. {
  115. va_list arg;
  116. va_start( arg, lpszFmtStr );
  117. vswprintf( szBuf, lpszFmtStr, arg );
  118. va_end( arg );
  119. DWORD dwSize = WideCharToMultiByte( GetConsoleCP(), 0, szBuf, -1,
  120. NULL, 0, NULL, NULL );
  121. CHAR *szStr = new CHAR[ dwSize ] ;
  122. if (szStr)
  123. {
  124. WideCharToMultiByte( GetConsoleCP(), 0, szBuf, -1, szStr, dwSize,
  125. NULL, NULL );
  126. *s_pOutput << "Thread ID : "<< GetCurrentThreadId() << "\n\t" << szStr
  127. << endl;
  128. delete[] szStr;
  129. }
  130. delete[] szBuf;
  131. }
  132. }
  133. ReleaseMutex( s_hMutex );
  134. }
  135. }
  136. void
  137. CDebugLogger::OutMessage
  138. (
  139. DWORD dwDebugLvl,
  140. DWORD dwLineNum,
  141. LPCSTR lpszFileName
  142. )
  143. {
  144. if (TlntSynchronizeOn(s_hMutex))
  145. {
  146. assert(s_pOutput != NULL);
  147. if( !CDebugLevel::IsCurrLevel( dwDebugLvl ) )
  148. {
  149. ReleaseMutex( s_hMutex );
  150. return;
  151. }
  152. *s_pOutput << "Thread ID : " << GetCurrentThreadId() << "\n\t"
  153. << " in File: " << lpszFileName << " at line: " << dwLineNum << endl;
  154. ReleaseMutex( s_hMutex );
  155. }
  156. }
  157. void
  158. CDebugLogger::OutMessage
  159. (
  160. LPCSTR lpszLineDesc,
  161. LPCSTR lpszFileName,
  162. DWORD dwLineNum,
  163. DWORD dwErrNum
  164. )
  165. {
  166. if (TlntSynchronizeOn(s_hMutex))
  167. {
  168. assert( s_pOutput != NULL );
  169. DWORD dwSize = 0;
  170. CHAR *szStr = NULL;
  171. LPTSTR lpBuffer;
  172. FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  173. NULL, dwErrNum, LANG_NEUTRAL, ( LPTSTR ) &lpBuffer, 0, NULL );
  174. if( !lpBuffer )
  175. {
  176. goto AbortOutMessage1;
  177. }
  178. *s_pOutput << "\nThe following call failed at line"
  179. << dwLineNum << " in " << lpszFileName << ":\n\n"
  180. << lpszLineDesc << "\n\nReason:";
  181. dwSize = WideCharToMultiByte( GetConsoleCP(), 0, lpBuffer, -1,
  182. NULL, 0, NULL, NULL );
  183. szStr = new CHAR[ dwSize ] ;
  184. if( !szStr )
  185. {
  186. goto AbortOutMessage2;
  187. }
  188. WideCharToMultiByte( GetConsoleCP(), 0, lpBuffer, -1, szStr, dwSize,
  189. NULL, NULL );
  190. *s_pOutput << szStr << "\n";
  191. delete[] szStr;
  192. AbortOutMessage2:
  193. LocalFree( lpBuffer );
  194. AbortOutMessage1:
  195. ReleaseMutex( s_hMutex );
  196. }
  197. }
  198. void CDebugLogger::PrintTime( void )
  199. {
  200. // print the current time
  201. assert( s_pOutput != NULL );
  202. struct _timeb timebuffer; char *timeline; _ftime( &timebuffer );
  203. timeline = ctime( & ( timebuffer.time ) );
  204. CHAR szBuff[256];
  205. sprintf( szBuff, "The time is %.19s.%hu %s", timeline, timebuffer.millitm,
  206. &timeline[20] );
  207. *s_pOutput << szBuff << endl;
  208. return;
  209. }
  210. void
  211. CDebugLogger::LogToFile
  212. (
  213. LPCSTR lpszFileName
  214. )
  215. {
  216. if( s_pOutput != &cerr )
  217. {
  218. delete s_pOutput;
  219. s_pOutput = NULL;
  220. }
  221. if( s_lpszLogFileName )
  222. {
  223. delete [] s_lpszLogFileName;
  224. s_lpszLogFileName = NULL;
  225. }
  226. s_lpszLogFileName = new CHAR[strlen(lpszFileName)+1];
  227. if( !s_lpszLogFileName )
  228. {
  229. return;
  230. }
  231. strcpy( s_lpszLogFileName, lpszFileName );
  232. s_pOutput = new ofstream( s_lpszLogFileName, ios::app );
  233. }