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.

634 lines
21 KiB

  1. //+-------------------------------------------------------------------
  2. //
  3. // File: log.hxx
  4. //
  5. // Contents: Class definitions for modules that use the log server
  6. // logging utility.
  7. //
  8. // Classes: Log
  9. //
  10. // History: 11-Sep-90 DavidSt Created
  11. // 11-Mar-94 DaveY Minature Log.
  12. //
  13. //--------------------------------------------------------------------
  14. #ifndef _LOG_HXX_
  15. #define _LOG_HXX_
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <windows.h>
  19. #ifndef WIN16
  20. #include <syncwrap.hxx> // for mutex.
  21. #else
  22. #include <types16.h>
  23. #include <shellapi.h> // to get the error codes
  24. #endif //WIN16
  25. #define LOG_PASS 0 // test case passed
  26. #define LOG_FAIL 1 // test case failed
  27. #define LOG_WARN 2
  28. #define LOG_ABORT 3 // the test aborted eg setup may have
  29. // failed
  30. #define LOG_INFO 4 // some additional information
  31. #define LOG_START 5 // these 2 are not normally required as
  32. #define LOG_DONE 6 // TOM automatically logs these.
  33. #define LOG_UNICODE 100 // Returned by Log::LogCharType
  34. #define LOG_ANSI 101 // Returned by Log::LogCharType
  35. class Log {
  36. public:
  37. Log(int argc, char *argv[], DWORD dwCharType = LOG_ANSI);
  38. Log(PCHAR pszLogServer,
  39. PCHAR pszTestName = (PCHAR) NULL,
  40. PCHAR pszTester = (PCHAR) NULL,
  41. PCHAR pszPath = (PCHAR) NULL,
  42. PCHAR pszObjectName = (PCHAR) NULL);
  43. ~Log();
  44. ULONG ConfirmCreation(void) { return ulLastError; }
  45. ULONG LogCharType(void) { return LOG_ANSI; };
  46. ULONG WriteVar(PCHAR pszVariation,
  47. USHORT usStatus,
  48. PCHAR pszStrFmt = (PCHAR)NULL,
  49. ... );
  50. ULONG WriteData(PCHAR szStrFmt, ...);
  51. ULONG WriteData2(PCHAR psz);
  52. #ifndef WIN16
  53. ULONG WriteData2(PWCHAR pwsz);
  54. #endif
  55. public:
  56. ULONG ulLastError;
  57. private:
  58. VOID DelString(PCHAR *pszOrig);
  59. ULONG NewString(PCHAR *pszOrig, const char * pszNewStr);
  60. ULONG SetLoggingDir(const char * pszNewName)
  61. {
  62. return NewString(&pszLoggingDir, pszNewName);
  63. }
  64. ULONG SetMachineName(const char * pszNewName)
  65. {
  66. return NewString(&pszMachineName, pszNewName);
  67. }
  68. ULONG SetObjectName(const char * pszNewName)
  69. {
  70. return NewString(&pszObjectName, pszNewName);
  71. }
  72. ULONG SetTestName(const char * pszNewName)
  73. {
  74. return NewString(&pszTestName, pszNewName);
  75. }
  76. ULONG SetPath(const char * pszNewName)
  77. {
  78. return NewString(&pszPath, pszNewName);
  79. }
  80. ULONG SetStatus(const char * pszNewName)
  81. {
  82. return NewString(&pszStatus, pszNewName);
  83. }
  84. ULONG SetLogFileName(const char * pszNewName)
  85. {
  86. return NewString(&pszLogFileName, pszNewName);
  87. }
  88. ULONG SetShortLogFileName(const char * pszNewName)
  89. {
  90. return NewString(&pszShortLogFileName, pszNewName);
  91. }
  92. ULONG Info(int argc, char *argv[]);
  93. ULONG AddComponent( // Combine path parts
  94. PCHAR pszNewName, // String being modified
  95. PCHAR pszComponent); // Component to be appended
  96. VOID InitLogging(); // Initialize class's elements
  97. VOID FreeLogging(); // Re-Initialize class's elements
  98. ULONG LogOpen(); // Open local/remote logging file
  99. ULONG LogData(); // Log current data, local/remote
  100. ULONG OpenLogFile(); // Opens or re-opens log file
  101. VOID CloseLogFile(); // Log DONE then close log file
  102. virtual int LogPrintf( // Centralized output method
  103. FILE *pfLog, // Output file handle
  104. PCHAR pszFmt, // Format string for vsprintf
  105. ...); // Args as needed by pszFmt
  106. ULONG SetInfo(const char * pszSrvr,
  107. const char * pszTest,
  108. const char * pszSubPath,
  109. const char * pszObject);
  110. ULONG SetLogFile();
  111. void GetBaseFilename(LPSTR pszFileWithExtension, LPSTR pszBaseName);
  112. private:
  113. FILE *pfLog; // log file pointer.
  114. PCHAR pszLoggingDir; // Root directory for log files
  115. PCHAR pszMachineName; // Machine sending data to log
  116. PCHAR pszObjectName; // Object generating the data
  117. PCHAR pszTestName; // Name of the running test
  118. PCHAR pszPath; // User provided log file path
  119. PCHAR pszStatus; // Logged test status
  120. PCHAR pszVariation; // Test variation number
  121. PCHAR pszLogFileName; // Full log file name (hLogFile)
  122. PCHAR pszShortLogFileName; // base log file name
  123. BOOL fInfoSet; // TRUE if test info has been set
  124. #ifndef WIN16
  125. CMutex hMtxSerialize; // Handle to use for queue mutex
  126. #endif
  127. };
  128. #ifdef oldstuff // old log class for win32
  129. #ifndef MIDL_PASS
  130. extern "C"
  131. {
  132. #include <stdarg.h>
  133. }
  134. #endif
  135. extern "C"
  136. {
  137. #include <stdio.h>
  138. }
  139. #include <windows.h>
  140. #if defined(WIN32)
  141. #include <syncwrap.hxx>
  142. #endif
  143. // manifests passed as status parameter to WriteVar
  144. // Note these values should always start at 0 and increment by 1.
  145. // The internal code verifies that the parameter is less than LOG_MAX_STATUS
  146. // in order to determine that the status is legitimate.
  147. #define LOG_PASS 0 // test case passed
  148. #define LOG_FAIL 1 // test case failed
  149. // WARNING - check results carefully there was something unexpected
  150. // but test may still have been valid eg cleanup may have failed
  151. #define LOG_WARN 2
  152. #define LOG_ABORT 3 // the test aborted eg setup may have
  153. // failed
  154. #define LOG_INFO 4 // some additional information
  155. #define LOG_START 5 // these 2 are not normally required as
  156. #define LOG_DONE 6 // TOM automatically logs these.
  157. #define LOG_MAX_STATUS 6 // the highest value of a status parameter
  158. #define LOG_UNICODE 100 // Returned by Log::LogCharType
  159. #define LOG_ANSI 101 // Returned by Log::LogCharType
  160. // Some TOM error codes
  161. #define TOM_ERROR_BASE (15000)
  162. #define TOM_CORRUPT_LOG_DATA (TOM_ERROR_BASE + 24)
  163. //+--------------------------------------------------------------------------
  164. // The LogInfo stucture is used to maintain a running count of the number
  165. // and type of log server records written
  166. //---------------------------------------------------------------------------
  167. #ifndef LOGSTATS_DEFINED
  168. #define LOGSTATS_DEFINED
  169. typedef struct _LogStats
  170. {
  171. ULONG ulFail;
  172. ULONG ulDone;
  173. ULONG ulStart;
  174. ULONG ulInfo;
  175. ULONG ulPass;
  176. ULONG ulAbort;
  177. ULONG ulWarn;
  178. } LogStats;
  179. #endif
  180. #define CHK_UNICODE(bool) if((bool) != fIsUnicode) \
  181. fprintf(stderr, "ASSERT - Log: calling wrong WCHAR/CHAR method\n")
  182. #ifndef MIDL_PASS
  183. //+-------------------------------------------------------------------
  184. //
  185. // Class: Log
  186. //
  187. // Purpose: In order to perform logging, the user needs to create one
  188. // instance of this class.
  189. //
  190. // Interface:
  191. //
  192. // History: ??-???-?? ???????? Created
  193. // 10-Sep-92 SarahJ Changed length to ULONG in WriteBinItem
  194. //
  195. //--------------------------------------------------------------------
  196. class Log
  197. {
  198. private:
  199. // **** char version ****
  200. PCHAR pszLoggingDir; // Root directory for log files
  201. ULONG ulEventCount; // Events in current log file
  202. ULONG ulEventTime; // When the event was logged
  203. PCHAR pszMachineName; // Machine sending data to log
  204. PCHAR pszObjectName; // Object generating the data
  205. PCHAR pszTestName; // Name of the running test
  206. PCHAR pszPath; // User provided log file path
  207. PCHAR pszStatus; // Logged test status
  208. PCHAR pszVariation; // Test variation number
  209. USHORT usBinLen; // # bytes binary data to log
  210. PVOID pvBinData; // The binary data to be logged
  211. PCHAR pszStrData; // String text to be logged
  212. HANDLE hLogFile; // Currently open log file handle
  213. PCHAR pszLogFileName; // Full log file name (hLogFile)
  214. BOOL fIsComPort; // TRUE of writing to a COM port
  215. BOOL fFlushWrites; // Flush all writes when TRUE
  216. BOOL fInfoSet; // TRUE if test info has been set
  217. // Selects WCHAR vs CHAR implementation
  218. BOOL fIsUnicode;
  219. // **** wchar version ****
  220. LPWSTR wszLoggingDir; // Root directory for log files
  221. LPWSTR wszMachineName; // Machine sending data to log
  222. LPWSTR wszObjectName; // Object generating the data
  223. LPWSTR wszTestName; // Name of the running test
  224. LPWSTR wszPath; // User provided log file path
  225. LPWSTR wszStatus; // Logged test status
  226. LPWSTR wszVariation; // Test variation number
  227. LPWSTR wszStrData; // String text to be logged
  228. LPWSTR wszLogFileName; // Full log file name (hLogFile)
  229. LPWSTR MbNameToWcs(PCHAR pszName); // convert CHAR to WCHAR
  230. PCHAR wcNametombs(PWCHAR pwcName); // convert WCHAR to CHAR
  231. #ifdef WIN32
  232. CMutex hMtxSerialize; // Handle to use for queue mutex
  233. #endif
  234. ULONG WriteHeader(); // Write header in log file
  235. VOID LogEventCount(); // Log the # events in log file
  236. VOID wLogEventCount(); // Log the # events in log file
  237. ULONG WriteBinItem( // Write a binary datum in log
  238. char chMark, // Used as ID for data item
  239. PVOID pvItem, // Dataum to be written
  240. ULONG uItemLen); // # bytes in pszItem
  241. ULONG CheckDir( // Make each subdirectory in path
  242. char * pszFullPath); // Full path to file
  243. ULONG CheckDir( // Make each subdirectory in path
  244. LPWSTR wszFullPath); // Full path to file
  245. ULONG AddComponent( // Combine path parts
  246. PCHAR pszNewName, // String being modified
  247. PCHAR pszComponent); // Component to be appended
  248. ULONG AddComponent( // Combine path parts
  249. LPWSTR wszNewName, // String being modified
  250. LPWSTR wszComponent); // Component to be appended
  251. ULONG SetLogFile(); // Sets pszLogFileName
  252. ULONG wSetLogFile(); // Sets wszLogFileName
  253. BOOL SetIsComPort( // Chk if named file is a com port
  254. const char * pszFile); // Name to be checked
  255. BOOL SetIsComPort( // Chk if named file is a com port
  256. LPCWSTR wszFile); // Name to be checked
  257. ULONG SetInfo( // Set general logging info
  258. const char * pszSrvr, // Name of logging server
  259. const char * pszTest, // Name of the test being run
  260. const char * pszSubPath, // Log file path qualifier
  261. const char * pszObject); // Object logging the data
  262. ULONG SetInfo( // Set general logging info
  263. LPCWSTR pszSrvr, // Name of logging server
  264. LPCWSTR pszTest, // Name of the test being run
  265. LPCWSTR pszSubPath, // Log file path qualifier
  266. LPCWSTR pszObject); // Object logging the data
  267. ULONG SetStrData( // Set logging string data
  268. PCHAR pszFmt, // Format string for vsprintf
  269. va_list pArgs); // Ptr to data for pszFmt
  270. ULONG SetStrData( // Set logging string data
  271. LPWSTR pszFmt, // Format string for vsprintf
  272. va_list pArgs); // Ptr to data for pszFmt
  273. ULONG SetEventCount(); // Retrieve # of events in log
  274. ULONG NewString( // Replace an existing string
  275. PCHAR *pszExist, // Existing string
  276. const char * pszNew); // String to be copied
  277. ULONG NewString( // Replace an existing string
  278. LPWSTR *wszExist, // Existing string
  279. LPCWSTR wszNew); // String to be copied
  280. VOID InitLogging(); // Initialize class's elements
  281. VOID FreeLogging(); // Re-Initialize class's elements
  282. ULONG LogOpen(); // Open local/remote logging file
  283. ULONG LogData(); // Log current data, local/remote
  284. ULONG OpenLogFile(); // Opens or re-opens log file
  285. ULONG WriteToLogFile(); // Write logging data to log file
  286. ULONG wWriteToLogFile(); // Write logging data to log file
  287. VOID CloseLogFile(); // Log DONE then close log file
  288. ULONG SetBinData( // Set new pvBinData
  289. USHORT usBytes, // # bytes of binary data
  290. PVOID pvData); // the binary data
  291. ULONG FlushLogFile( // Flush log data if requested
  292. ULONG usErr); // Value to return
  293. virtual int LogPrintf( // Centralized output method
  294. HANDLE hHandle, // Output file handle
  295. PCHAR pszFmt, // Format string for vsprintf
  296. ...); // Args as needed by pszFmt
  297. virtual int LogPrintf( // Centralized output method
  298. HANDLE hHandle, // Output file handle
  299. LPWSTR pszFmt, // Format string for vsprintf
  300. ...); // Args as needed by pszFmt
  301. LogStats _stLogStats; // keeps counts of types of log records
  302. ULONG SetLoggingDir(const char * pszNewName);
  303. ULONG SetMachineName(const char * pszNewName);
  304. ULONG SetObjectName(const char * pszNewName);
  305. ULONG SetTestName(const char * pszNewName);
  306. ULONG SetPath(const char * pszNewName);
  307. ULONG SetStatus(const char * pszNewName);
  308. ULONG SetVariation(const char * pszNewName);
  309. ULONG SetLogFileName(const char * pszNewName);
  310. ULONG SetLoggingDir(LPCWSTR pszNewName);
  311. ULONG SetMachineName(LPCWSTR pszNewName);
  312. ULONG SetObjectName(LPCWSTR pszNewName);
  313. ULONG SetTestName(LPCWSTR pszNewName);
  314. ULONG SetPath(LPCWSTR pszNewName);
  315. ULONG SetStatus(LPCWSTR pszNewName);
  316. ULONG SetVariation(LPCWSTR pszNewName);
  317. ULONG SetLogFileName(LPCWSTR pszNewName);
  318. public:
  319. ULONG ulLastError; // Code returned by last call
  320. ULONG LogCharType(void) { return (TRUE == fIsUnicode) ?
  321. LOG_UNICODE : LOG_ANSI; }
  322. ULONG ConfirmCreation(void) { return ulLastError; }
  323. // The Log methods cause a logging file to be set up.
  324. // The file may be on the local machine or on the server
  325. // named in pszLogServer (see overloaded method Log).
  326. Log(DWORD dwCharType = LOG_ANSI);
  327. Log(int argc, char *argv[], DWORD dwCharType = LOG_ANSI);
  328. Log(PCHAR pszLogServer,
  329. PCHAR pszTestName = (PCHAR) NULL,
  330. PCHAR pszTester = (PCHAR) NULL,
  331. PCHAR pszPath = (PCHAR) NULL,
  332. PCHAR pszObjectName = (PCHAR) NULL);
  333. Log(LPWSTR pszLogServer,
  334. LPWSTR pszTestName = (LPWSTR) NULL,
  335. LPWSTR pszTester = (LPWSTR) NULL,
  336. LPWSTR pszPath = (LPWSTR) NULL,
  337. LPWSTR pszObjectName = (LPWSTR) NULL);
  338. ~Log();
  339. ULONG Info(int argc, char *argv[]);
  340. ULONG WriteVar(PCHAR pszVariation,
  341. USHORT usStatus,
  342. PCHAR pszStrFmt = (PCHAR)NULL,
  343. ... );
  344. ULONG WriteVar(LPWSTR pszVariation,
  345. USHORT usStatus,
  346. LPWSTR pszStrFmt = (LPWSTR)NULL,
  347. ... );
  348. ULONG WriteVarList(PCHAR pszVariation,
  349. USHORT usStatus,
  350. PCHAR pszStrFmt,
  351. va_list vaArgs);
  352. ULONG WriteVarList(LPWSTR pszVariation,
  353. USHORT usStatus,
  354. LPWSTR pszStrFmt,
  355. va_list vaArgs);
  356. ULONG WriteVar(PCHAR pszVariation,
  357. USHORT usStatus,
  358. USHORT usBinLen,
  359. PVOID pvBinData,
  360. PCHAR pszStrFmt = (PCHAR)NULL,
  361. ...);
  362. ULONG WriteVar(LPWSTR pszVariation,
  363. USHORT usStatus,
  364. USHORT usBinLen,
  365. PVOID pvBinData,
  366. LPWSTR pszStrFmt = (LPWSTR)NULL,
  367. ...);
  368. ULONG WriteVarList(PCHAR pszVariation,
  369. USHORT usStatus,
  370. USHORT usBinLen,
  371. PVOID pvBinData,
  372. PCHAR pszStrFmt,
  373. va_list vaArgs);
  374. ULONG WriteVarList(LPWSTR pszVariation,
  375. USHORT usStatus,
  376. USHORT usBinLen,
  377. PVOID pvBinData,
  378. LPWSTR pszStrFmt,
  379. va_list vaArgs);
  380. ULONG WriteData(PCHAR szStrFmt, ...);
  381. ULONG WriteData(LPWSTR szStrFmt, ...);
  382. ULONG WriteDataList(PCHAR szStrFmt, va_list vaArgs);
  383. ULONG WriteDataList(LPWSTR szStrFmt, va_list vaArgs);
  384. ULONG WriteData(USHORT usBinLen,
  385. PVOID pvBinData,
  386. PCHAR pszStrFmt = (PCHAR)NULL,
  387. ...);
  388. ULONG WriteData(USHORT usBinLen,
  389. PVOID pvBinData,
  390. LPWSTR pszStrFmt = (LPWSTR)NULL,
  391. ...);
  392. ULONG WriteDataList(USHORT usBinLen,
  393. PVOID pvBinData,
  394. PCHAR pszStrFmt,
  395. va_list vaArgs);
  396. ULONG WriteDataList(USHORT usBinLen,
  397. PVOID pvBinData,
  398. LPWSTR pszStrFmt,
  399. va_list vaArgs);
  400. VOID GetLogStats(LogStats& stStats);
  401. };
  402. //+-------------------------------------------------------------------
  403. //
  404. // Member: Log::Set<x>(const char * pszNewName)
  405. // Member: Log::Set<x>(lpwstr wszNewName)
  406. //
  407. // Synopsis: This method sets the relevant public data to the
  408. // value in pszNewName/WszNewName
  409. //
  410. // Arguments: const char * pszNewName - the new string OR
  411. // Arguments: LPWSTR wszNewName - the new string
  412. //
  413. // History: 17-Sep-92 SarahJ Created to replace macro definition
  414. //
  415. //--------------------------------------------------------------------
  416. // BUGBUG - need to error check WCHAR vs CHAR to insure proper call
  417. inline ULONG Log::SetLoggingDir(const char * pszNewName)
  418. {
  419. CHK_UNICODE(FALSE);
  420. return NewString(&pszLoggingDir, pszNewName);
  421. }
  422. inline ULONG Log::SetLoggingDir(LPCWSTR wszNewName)
  423. {
  424. CHK_UNICODE(TRUE);
  425. return NewString(&wszLoggingDir, wszNewName);
  426. }
  427. inline ULONG Log::SetMachineName(const char * pszNewName)
  428. {
  429. CHK_UNICODE(FALSE);
  430. return NewString(&pszMachineName, pszNewName);
  431. }
  432. inline ULONG Log::SetMachineName(LPCWSTR wszNewName)
  433. {
  434. CHK_UNICODE(TRUE);
  435. return NewString(&wszMachineName, wszNewName);
  436. }
  437. inline ULONG Log::SetObjectName(const char * pszNewName)
  438. {
  439. CHK_UNICODE(FALSE);
  440. return NewString(&pszObjectName, pszNewName);
  441. }
  442. inline ULONG Log::SetObjectName(LPCWSTR wszNewName)
  443. {
  444. CHK_UNICODE(TRUE);
  445. return NewString(&wszObjectName, wszNewName);
  446. }
  447. inline ULONG Log::SetTestName(const char * pszNewName)
  448. {
  449. CHK_UNICODE(FALSE);
  450. return NewString(&pszTestName, pszNewName);
  451. }
  452. inline ULONG Log::SetTestName(LPCWSTR wszNewName)
  453. {
  454. CHK_UNICODE(TRUE);
  455. return NewString(&wszTestName, wszNewName);
  456. }
  457. inline ULONG Log::SetPath(const char * pszNewName)
  458. {
  459. CHK_UNICODE(FALSE);
  460. return NewString(&pszPath, pszNewName);
  461. }
  462. inline ULONG Log::SetPath(LPCWSTR wszNewName)
  463. {
  464. CHK_UNICODE(TRUE);
  465. return NewString(&wszPath, wszNewName);
  466. }
  467. inline ULONG Log::SetStatus(const char * pszNewName)
  468. {
  469. CHK_UNICODE(FALSE);
  470. return NewString(&pszStatus, pszNewName);
  471. }
  472. inline ULONG Log::SetStatus(LPCWSTR wszNewName)
  473. {
  474. CHK_UNICODE(TRUE);
  475. return NewString(&wszStatus, wszNewName);
  476. }
  477. inline ULONG Log::SetVariation(const char * pszNewName)
  478. {
  479. CHK_UNICODE(FALSE);
  480. return NewString(&pszVariation, pszNewName);
  481. }
  482. inline ULONG Log::SetVariation(LPCWSTR wszNewName)
  483. {
  484. CHK_UNICODE(TRUE);
  485. return NewString(&wszVariation, wszNewName);
  486. }
  487. inline ULONG Log::SetLogFileName(const char * pszNewName)
  488. {
  489. CHK_UNICODE(FALSE);
  490. return NewString(&pszLogFileName, pszNewName);
  491. }
  492. inline ULONG Log::SetLogFileName(LPCWSTR wszNewName)
  493. {
  494. CHK_UNICODE(TRUE);
  495. return NewString(&wszLogFileName, wszNewName);
  496. }
  497. #endif /* ifndef MIDL_PASS */
  498. #endif // oldstuff
  499. #endif // _LOG_HXX_