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.

372 lines
8.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1997.
  5. //
  6. // File: ntlog.cxx
  7. //
  8. // Contents: implemntation of CNtLog
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // Notes: loginf.h is where app specific setting should go
  15. //
  16. // History: 8-23-96 benl Created
  17. //
  18. //----------------------------------------------------------------------------
  19. #include <windows.h>
  20. #include <tchar.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <assert.h>
  24. //#include <iostream.h>
  25. #include <ntlog.h>
  26. #include "ntlog.hxx"
  27. // #include "loginf.h" //put any ntlog settings in here // not needed 4 now
  28. // instead
  29. #define LOG_OPTIONS 0
  30. //constants
  31. const int BUFLEN = 255;
  32. //+---------------------------------------------------------------------------
  33. //
  34. // Member: CNtLog::Init
  35. //
  36. // Synopsis: set up logging
  37. //
  38. // Arguments: [lpLogFile] -- log file name
  39. //
  40. // Returns: TRUE if successful
  41. //
  42. // History: 8-23-96 benl Created
  43. //
  44. // Notes:
  45. //
  46. //----------------------------------------------------------------------------
  47. BOOL CNtLog::Init(LPCTSTR lpLogFile)
  48. {
  49. WIN32_FIND_DATA wfd;
  50. HANDLE h;
  51. INT iNumExt = 0;
  52. if (lpLogFile)
  53. {
  54. _sntprintf(_lpName,MAX_PATH,_T("%s"),lpLogFile);
  55. //repeat until we have a name that doesn't exist
  56. do {
  57. h = ::FindFirstFile(_lpName,&wfd);
  58. if ( h != INVALID_HANDLE_VALUE )
  59. {
  60. _sntprintf(_lpName,MAX_PATH, _T("%s.%d"),lpLogFile,iNumExt++);
  61. ::FindClose(h);
  62. } else break;
  63. } while (1);
  64. //Now set up the log
  65. _hLog = ::tlCreateLog(_lpName, LOG_OPTIONS );
  66. } else
  67. {
  68. //set up log with no associated file
  69. _lpName[0] = _T('\0');
  70. _hLog = ::tlCreateLog(NULL, LOG_OPTIONS );
  71. }
  72. if ( !_hLog ) {
  73. _ftprintf(stderr, _T("CNtLog::Init: tlCreateLog %s failed\n"),
  74. lpLogFile);
  75. if (lpLogFile)
  76. ::DeleteFile(_lpName);
  77. return FALSE;
  78. }
  79. //Add main thread as a participant
  80. ::tlAddParticipant(_hLog, 0, 0 );
  81. _dwLevel = TLS_TEST;
  82. return TRUE;
  83. } //CNtLog::Init
  84. //+---------------------------------------------------------------------------
  85. //
  86. // Member: CNtLog::Error
  87. //
  88. // Synopsis:
  89. //
  90. // Arguments: [fmt] -- format string like any other printf func.
  91. //
  92. // Returns:
  93. //
  94. // History: 8-23-96 benl Created
  95. //
  96. // Notes:
  97. //
  98. //----------------------------------------------------------------------------
  99. VOID CNtLog::Error (LPCTSTR fmt, ...)
  100. {
  101. va_list vl;
  102. TCHAR lpBuffer[BUFLEN];
  103. va_start (vl, fmt);
  104. _vsntprintf(lpBuffer,BUFLEN,fmt,vl);
  105. tlLog(_hLog, TLS_BLOCK | _dwLevel, TEXT(__FILE__),(int)__LINE__, lpBuffer);
  106. _tprintf(_T("Error: %s\n"), lpBuffer);
  107. va_end (vl);
  108. } //CNtLog::Error //CNtLog::Error
  109. //+---------------------------------------------------------------------------
  110. //
  111. // Member: CNtLog::Warn
  112. //
  113. // Synopsis:
  114. //
  115. // Arguments: [fmt] --
  116. //
  117. // Returns:
  118. //
  119. // History: 8-23-96 benl Created
  120. //
  121. // Notes:
  122. //
  123. //----------------------------------------------------------------------------
  124. VOID CNtLog::Warn (LPCTSTR fmt, ...)
  125. {
  126. va_list vl;
  127. TCHAR lpBuffer[BUFLEN];
  128. va_start (vl, fmt);
  129. _vsntprintf(lpBuffer,BUFLEN,fmt,vl);
  130. tlLog(_hLog,TLS_WARN | _dwLevel, TEXT(__FILE__),(int)__LINE__, lpBuffer);
  131. _tprintf(_T("Warning: %s\n"), lpBuffer);
  132. va_end (vl);
  133. } //CNtLog::Warn
  134. //+---------------------------------------------------------------------------
  135. //
  136. // Member: CNtLog::Info
  137. //
  138. // Synopsis:
  139. //
  140. // Arguments: [fmt] --
  141. //
  142. // Returns:
  143. //
  144. // History: 8-23-96 benl Created
  145. //
  146. // Notes:
  147. //
  148. //----------------------------------------------------------------------------
  149. VOID CNtLog::Info (LPCTSTR fmt, ...)
  150. {
  151. va_list vl;
  152. TCHAR lpBuffer[BUFLEN];
  153. va_start (vl, fmt);
  154. _vsntprintf(lpBuffer,BUFLEN,fmt,vl);
  155. tlLog(_hLog,TLS_INFO | _dwLevel, TEXT(__FILE__),(int)__LINE__, lpBuffer);
  156. _tprintf(_T("%s\n"), lpBuffer);
  157. va_end (vl);
  158. } //CNtLog::Info
  159. //+---------------------------------------------------------------------------
  160. //
  161. // Member: CNtLog::Pass
  162. //
  163. // Synopsis:
  164. //
  165. // Arguments: [fmt] --
  166. //
  167. // Returns:
  168. //
  169. // History: 8-26-96 benl Created
  170. //
  171. // Notes:
  172. //
  173. //----------------------------------------------------------------------------
  174. VOID CNtLog::Pass( LPCTSTR fmt, ...)
  175. {
  176. va_list vl;
  177. TCHAR lpBuffer[BUFLEN];
  178. va_start (vl, fmt);
  179. _vsntprintf(lpBuffer,BUFLEN,fmt,vl);
  180. tlLog(_hLog,TLS_PASS | _dwLevel, TEXT(__FILE__),(int)__LINE__, lpBuffer);
  181. //_tprintf(_T("Pass: %s\n"), lpBuffer);
  182. va_end (vl);
  183. } //CNtLog::Pass
  184. //+---------------------------------------------------------------------------
  185. //
  186. // Member: CNtLog::Fail
  187. //
  188. // Synopsis:
  189. //
  190. // Arguments: [fmt] --
  191. //
  192. // Returns:
  193. //
  194. // History: 8-26-96 benl Created
  195. //
  196. // Notes:
  197. //
  198. //----------------------------------------------------------------------------
  199. VOID CNtLog::Fail( LPCTSTR fmt, ...)
  200. {
  201. va_list vl;
  202. TCHAR lpBuffer[BUFLEN];
  203. va_start (vl, fmt);
  204. _vsntprintf(lpBuffer,BUFLEN,fmt,vl);
  205. tlLog(_hLog,TLS_SEV2 | _dwLevel, TEXT(__FILE__),(int)__LINE__, lpBuffer);
  206. _tprintf(_T("Fail: %s\n"), lpBuffer);
  207. va_end (vl);
  208. } //CNtLog::Fail
  209. //+---------------------------------------------------------------------------
  210. //
  211. // Member: CNtLog::Close
  212. //
  213. // Synopsis:
  214. //
  215. // Arguments: [bDelete] --
  216. //
  217. // Returns:
  218. //
  219. // History: 8-23-96 benl Created
  220. //
  221. // Notes:
  222. //
  223. //----------------------------------------------------------------------------
  224. VOID CNtLog::Close(BOOL bDelete)
  225. {
  226. assert(_hLog != NULL);
  227. tlReportStats(_hLog);
  228. tlRemoveParticipant(_hLog);
  229. tlDestroyLog(_hLog);
  230. if (bDelete && _lpName[0] != _T('\0'))
  231. ::DeleteFile(_lpName);
  232. //cleanup variables
  233. _lpName[0] = _T('\0');
  234. _hLog = NULL;
  235. } //CNtLog::Close
  236. //+---------------------------------------------------------------------------
  237. //
  238. // Member: CNtLog::AttachThread
  239. //
  240. // Synopsis:
  241. //
  242. // Arguments: (none)
  243. //
  244. // Returns:
  245. //
  246. // History: 12-04-1996 benl Created
  247. //
  248. // Notes:
  249. //
  250. //----------------------------------------------------------------------------
  251. VOID CNtLog::AttachThread()
  252. {
  253. tlAddParticipant(_hLog, 0, 0);
  254. } //CNtLog::AttachThread
  255. //+---------------------------------------------------------------------------
  256. //
  257. // Member: CNtLog::DetachThread
  258. //
  259. // Synopsis:
  260. //
  261. // Arguments: (none)
  262. //
  263. // Returns:
  264. //
  265. // History: 12-04-1996 benl Created
  266. //
  267. // Notes:
  268. //
  269. //----------------------------------------------------------------------------
  270. VOID CNtLog::DetachThread()
  271. {
  272. tlRemoveParticipant(_hLog);
  273. } //CNtLog::DetachThread
  274. //+---------------------------------------------------------------------------
  275. //
  276. // Member: CNtLog::StartVariation
  277. //
  278. // Synopsis:
  279. //
  280. // Arguments: (none)
  281. //
  282. // Returns:
  283. //
  284. // History: 9-18-1997 benl Created
  285. //
  286. // Notes:
  287. //
  288. //----------------------------------------------------------------------------
  289. VOID CNtLog::StartVariation()
  290. {
  291. _dwLevel = TLS_VARIATION;
  292. tlStartVariation(_hLog);
  293. } // CNtLog::StartVariation
  294. //+---------------------------------------------------------------------------
  295. //
  296. // Member: CNtLog::EndVariation
  297. //
  298. // Synopsis:
  299. //
  300. // Arguments: (none)
  301. //
  302. // Returns:
  303. //
  304. // History: 9-18-1997 benl Created
  305. //
  306. // Notes:
  307. //
  308. //----------------------------------------------------------------------------
  309. VOID CNtLog::EndVariation()
  310. {
  311. DWORD dwResult;
  312. _dwLevel = TLS_TEST;
  313. dwResult = tlEndVariation(_hLog);
  314. tlLog(_hLog, dwResult | TL_TEST, _T("Variation result"));
  315. } // CNtLog::EndVariation