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.

382 lines
12 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. tracerpt.c
  5. Abstract:
  6. Event Trace Reporting Tool
  7. Author:
  8. 08-Apr-1998 Melur Raghuraman
  9. Revision History:
  10. --*/
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <tchar.h>
  14. #include "tracectr.h"
  15. #include "resource.h"
  16. #include "varg.c"
  17. #define GROUP_SRC 0x01
  18. #define GROUP_REALTIME 0x02
  19. VARG_DECLARE_COMMANDS
  20. VARG_DEBUG ( VARG_FLAG_OPTIONAL|VARG_FLAG_HIDDEN )
  21. VARG_HELP ( VARG_FLAG_OPTIONAL )
  22. VARG_MSZ ( IDS_PARAM_LOGFILE, VARG_FLAG_NOFLAG|VARG_FLAG_EXPANDFILES|VARG_FLAG_ARG_FILENAME, NULL )
  23. VARG_STR ( IDS_PARAM_DUMPFILE, VARG_FLAG_OPTIONAL|VARG_FLAG_ARG_FILENAME|VARG_FLAG_DEFAULTABLE|VARG_FLAG_RCDEFAULT, IDS_DEFAULT_DUMP )
  24. VARG_STR ( IDS_PARAM_SUMMARY, VARG_FLAG_OPTIONAL|VARG_FLAG_ARG_FILENAME|VARG_FLAG_DEFAULTABLE|VARG_FLAG_RCDEFAULT, IDS_DEFAULT_SUMMARY )
  25. VARG_STR ( IDS_PARAM_MOFFILE, VARG_FLAG_OPTIONAL|VARG_FLAG_ARG_FILENAME|VARG_FLAG_HIDDEN|VARG_FLAG_DEFAULTABLE, NULL )
  26. VARG_STR ( IDS_PARAM_REPORTFILE, VARG_FLAG_OPTIONAL|VARG_FLAG_ARG_FILENAME|VARG_FLAG_DEFAULTABLE|VARG_FLAG_RCDEFAULT, IDS_DEFAULT_REPORT )
  27. VARG_MSZ ( IDS_PARAM_LOGGERNAME, 0, NULL )
  28. VARG_INI ( IDS_PARAM_SETTINGS, VARG_FLAG_OPTIONAL, NULL )
  29. VARG_BOOL ( IDS_PARAM_EXFMT, VARG_FLAG_OPTIONAL|VARG_FLAG_HIDDEN, FALSE )
  30. VARG_STR ( IDS_PARAM_MERGE, VARG_FLAG_OPTIONAL|VARG_FLAG_ARG_FILENAME|VARG_FLAG_DEFAULTABLE|VARG_FLAG_RCDEFAULT|VARG_FLAG_HIDDEN, IDS_DEFAULT_MERGED )
  31. VARG_STR ( IDS_PARAM_COMP, VARG_FLAG_OPTIONAL|VARG_FLAG_ARG_FILENAME|VARG_FLAG_HIDDEN|VARG_FLAG_RCDEFAULT|VARG_FLAG_DEFAULTABLE, IDS_DEFAULT_COMP )
  32. VARG_BOOL ( IDS_PARAM_YES, VARG_FLAG_OPTIONAL, FALSE )
  33. VARG_STR ( IDS_PARAM_FORMAT, VARG_FLAG_OPTIONAL|VARG_FLAG_LITERAL, _T("TXT") )
  34. VARG_DECLARE_NAMES
  35. eDebug,
  36. eHelp,
  37. eLogFile,
  38. eDump,
  39. eSummary,
  40. eMofFile,
  41. eReport,
  42. eRealTime,
  43. eConfig,
  44. eExFormat,
  45. eMerge,
  46. eInterpret,
  47. eYes,
  48. eFormat,
  49. VARG_DECLARE_FORMAT
  50. VARG_GROUP( eLogFile, VARG_EXCL(GROUP_SRC)|VARG_COND(GROUP_SRC) )
  51. VARG_GROUP( eRealTime, VARG_EXCL(GROUP_SRC)|VARG_COND(GROUP_SRC) )
  52. VARG_GROUP( eRealTime, VARG_EXCL(GROUP_REALTIME) )
  53. VARG_GROUP( eReport, VARG_EXCL(GROUP_REALTIME) )
  54. VARG_EXHELP( eDump, IDS_EXAMPLE_DUMPFILE)
  55. VARG_EXHELP( eSummary, IDS_EXAMPLE_SUMMARY )
  56. VARG_EXHELP( eRealTime, IDS_EXAMPLE_REALTIME)
  57. VARG_DECLARE_END
  58. #define MAXLOGFILES 32
  59. #define MAX_BUFFER_SIZE 1048576
  60. #define CHECK_HR( hr ) if( ERROR_SUCCESS != hr ){ goto cleanup; }
  61. DWORD ExtractResourceFile( LPWSTR szResourceName, LPWSTR szFileName );
  62. void ReportStatus( int Status, double Progress );
  63. int __cdecl _tmain (int argc, LPTSTR* argv)
  64. {
  65. LPTSTR* EvmFile = NULL; // List Of LogFiles To Process
  66. LPTSTR* Loggers = NULL; // List of Loggers to process
  67. ULONG LogFileCount = 0;
  68. ULONG LoggerCount = 0;
  69. TCHAR strEventDef[MAXSTR];
  70. TCHAR strDefinitionFile[MAXSTR] = _T("");
  71. DWORD dwCheckFileFlag = 0;
  72. HRESULT hr = ERROR_SUCCESS;
  73. TCHAR strXSLFile[MAXSTR] = _T("");
  74. TRACE_BASIC_INFO TraceBasicInfo;
  75. //
  76. // Parse the Command line arguments
  77. //
  78. ParseCmd( argc, argv );
  79. EvmFile = (LPTSTR*)VARG_ALLOC( GetMaxLoggers() * sizeof(LPTSTR) );
  80. if( NULL == EvmFile ){
  81. hr = ERROR_OUTOFMEMORY;
  82. goto cleanup;
  83. }
  84. Loggers = (LPTSTR*)VARG_ALLOC( GetMaxLoggers() * sizeof(LPTSTR) );
  85. if( NULL == Loggers ){
  86. hr = ERROR_OUTOFMEMORY;
  87. goto cleanup;
  88. }
  89. memset(&TraceBasicInfo, 0, sizeof(TRACE_BASIC_INFO));
  90. PrintMessage( g_normal, IDS_MESSAGE_INPUT );
  91. if( Commands[eRealTime].bDefined && Commands[eRealTime].strValue != NULL ) {
  92. LPTSTR strLogger = Commands[eRealTime].strValue;
  93. PrintMessage( g_normal, IDS_MESSAGE_LOGGERS );
  94. while( *strLogger != _T('\0') ){
  95. Loggers[LoggerCount] = strLogger;
  96. LoggerCount++;
  97. varg_printf( g_normal, _T(" %1!s!\n"), strLogger );
  98. strLogger += (_tcslen(strLogger)+1);
  99. }
  100. varg_printf( g_normal, _T("\n") );
  101. }
  102. if( Commands[eLogFile].bDefined && Commands[eLogFile].strValue != NULL ) {
  103. LPTSTR strLogger = Commands[eLogFile].strValue;
  104. PrintMessage( g_normal, IDS_MESSAGE_FILES );
  105. while( *strLogger != '\0' ){
  106. EvmFile[LogFileCount] = strLogger;
  107. LogFileCount++;
  108. hr = CheckFile(strLogger, VARG_CF_EXISTS);
  109. CHECK_HR( hr );
  110. varg_printf( g_normal, _T(" %1!s!\n"), strLogger );
  111. strLogger += (_tcslen(strLogger)+1);
  112. }
  113. varg_printf( g_normal, _T("\n") );
  114. }
  115. if( Commands[eMofFile].bDefined ) {
  116. hr = GetTempName( strDefinitionFile, MAXSTR );
  117. CHECK_HR( hr );
  118. LoadString( NULL, IDS_MESSAGE_DFLTSRC, strEventDef, MAXSTR );
  119. TraceBasicInfo.DefMofFileName = strDefinitionFile;
  120. hr = ExtractResourceFile( L"MOFFILE", strDefinitionFile );
  121. CHECK_HR( hr );
  122. if ( Commands[eMofFile].strValue != NULL ) {
  123. TraceBasicInfo.MofFileName = Commands[eMofFile].strValue;
  124. hr = StringCchCopy( strEventDef, MAXSTR, Commands[eMofFile].strValue );
  125. }
  126. }else{
  127. TraceBasicInfo.Flags |= TRACE_USE_WBEM;
  128. *strEventDef = _T('\0');
  129. }
  130. dwCheckFileFlag = Commands[eYes].bValue ? VARG_CF_OVERWRITE : (VARG_CF_PROMPT|VARG_CF_OVERWRITE);
  131. if( Commands[eMerge].bDefined ){
  132. TraceBasicInfo.Flags |= TRACE_MERGE_ETL;
  133. TraceBasicInfo.MergeFileName = Commands[eMerge].strValue;
  134. }else if ( _tcslen( strEventDef ) ){
  135. PrintMessage( g_normal, IDS_MESSAGE_DEFINE, strEventDef );
  136. }
  137. if( Commands[eInterpret].bDefined ) {
  138. TraceBasicInfo.Flags |= TRACE_INTERPRET;
  139. TraceBasicInfo.CompFileName = Commands[eInterpret].strValue;
  140. hr = CheckFile( Commands[eInterpret].strValue, dwCheckFileFlag );
  141. CHECK_HR(hr);
  142. }
  143. if( Commands[eReport].bDefined ) {
  144. TraceBasicInfo.Flags |= TRACE_REDUCE;
  145. TraceBasicInfo.ProcFileName = Commands[eReport].strValue;
  146. hr = CheckFile( Commands[eReport].strValue, dwCheckFileFlag );
  147. CHECK_HR(hr);
  148. DeleteFile( Commands[eReport].strValue );
  149. if( _wcsicmp( Commands[eFormat].strValue, L"XML" ) == 0 ){
  150. LPWSTR szScan;
  151. hr = StringCchCopy( strXSLFile, MAXSTR, Commands[eReport].strValue );
  152. szScan = strXSLFile + wcslen(strXSLFile) - 1;
  153. while( szScan > strXSLFile ){
  154. if( *szScan == L'\\' ){
  155. szScan++;
  156. *szScan = L'\0';
  157. hr = StringCchCat( strXSLFile, MAXSTR, L"report.xsl" );
  158. break;
  159. }
  160. szScan--;
  161. }
  162. if( szScan == strXSLFile ){
  163. hr = StringCchCopy( strXSLFile, MAXSTR, L"report.xsl" );
  164. }
  165. ExtractResourceFile( L"XREPORT.XSL", strXSLFile );
  166. TraceBasicInfo.XSLDocName = L"report.xsl";
  167. }else if( _wcsicmp( Commands[eFormat].strValue, L"TXT" ) == 0 ){
  168. GetTempName( strXSLFile, MAXSTR );
  169. ExtractResourceFile( L"XTEXT.XSL", strXSLFile );
  170. TraceBasicInfo.Flags |= TRACE_TRANSFORM_XML;
  171. TraceBasicInfo.XSLDocName = strXSLFile;
  172. }else if( _wcsicmp( Commands[eFormat].strValue, L"HTML" ) == 0 ){
  173. GetTempName( strXSLFile, MAXSTR );
  174. ExtractResourceFile( L"XREPORT.XSL", strXSLFile );
  175. TraceBasicInfo.Flags |= TRACE_TRANSFORM_XML;
  176. TraceBasicInfo.XSLDocName = strXSLFile;
  177. }
  178. }
  179. if( Commands[eExFormat].bValue ){
  180. TraceBasicInfo.Flags |= TRACE_EXTENDED_FMT;
  181. }
  182. if( Commands[eDump].bDefined ){
  183. TraceBasicInfo.Flags |= TRACE_DUMP;
  184. TraceBasicInfo.DumpFileName = Commands[eDump].strValue;
  185. hr = CheckFile( Commands[eDump].strValue, dwCheckFileFlag );
  186. CHECK_HR(hr);
  187. }
  188. if( Commands[eSummary].bDefined ) {
  189. TraceBasicInfo.Flags |= TRACE_SUMMARY;
  190. TraceBasicInfo.SummaryFileName = Commands[eSummary].strValue;
  191. hr = CheckFile( Commands[eSummary].strValue, dwCheckFileFlag );
  192. CHECK_HR(hr);
  193. }
  194. //
  195. // Make dump & summary the default
  196. //
  197. if( !(TraceBasicInfo.Flags & (TRACE_DUMP|TRACE_REDUCE|TRACE_MERGE_ETL|TRACE_SUMMARY) ) ) {
  198. TraceBasicInfo.Flags |= (TRACE_DUMP|TRACE_SUMMARY);
  199. TraceBasicInfo.DumpFileName = Commands[eDump].strValue;
  200. TraceBasicInfo.SummaryFileName = Commands[eSummary].strValue;
  201. hr = CheckFile( Commands[eDump].strValue, dwCheckFileFlag );
  202. CHECK_HR(hr);
  203. hr = CheckFile( Commands[eSummary].strValue, dwCheckFileFlag );
  204. CHECK_HR(hr);
  205. }
  206. TraceBasicInfo.LogFileName = EvmFile;
  207. TraceBasicInfo.LogFileCount = LogFileCount;
  208. TraceBasicInfo.LoggerName = Loggers;
  209. TraceBasicInfo.LoggerCount = LoggerCount;
  210. if( ! Commands[eRealTime].bDefined ){
  211. TraceBasicInfo.StatusFunction = ReportStatus;
  212. varg_printf( g_normal, _T("\n") );
  213. ReportStatus( 0, 0.0 );
  214. }
  215. hr = InitTraceContext(&TraceBasicInfo, NULL);
  216. CHECK_HR( hr );
  217. if( ! Commands[eRealTime].bDefined ){
  218. ReportStatus( 0, 1.0 );
  219. varg_printf( g_normal, _T("\n") );
  220. }
  221. PrintMessage( g_normal, IDS_MESSAGE_OUTPUT );
  222. if( TraceBasicInfo.Flags & TRACE_DUMP ){
  223. PrintMessage( g_normal, IDS_MESSAGE_CSVFILE, TraceBasicInfo.DumpFileName );
  224. }
  225. if( TraceBasicInfo.Flags & TRACE_SUMMARY ){
  226. PrintMessage( g_normal, IDS_MESSAGE_SUMMARY, TraceBasicInfo.SummaryFileName );
  227. }
  228. if( TraceBasicInfo.Flags & TRACE_REDUCE ){
  229. PrintMessage( g_normal, IDS_MESSAGE_REPORT, TraceBasicInfo.ProcFileName );
  230. }
  231. if( TraceBasicInfo.Flags & TRACE_MERGE_ETL ){
  232. PrintMessage( g_normal, IDS_MESSAGE_MERGED, TraceBasicInfo.MergeFileName );
  233. }
  234. if( TraceBasicInfo.Flags & TRACE_INTERPRET ){
  235. PrintMessage( g_normal, IDS_MESSAGE_COMP, TraceBasicInfo.CompFileName );
  236. }
  237. hr = DeinitTraceContext(&TraceBasicInfo);
  238. CHECK_HR( hr );
  239. cleanup:
  240. VARG_FREE( Loggers );
  241. VARG_FREE( EvmFile );
  242. if( ! IsEmpty( strDefinitionFile ) ){
  243. DeleteFile( strDefinitionFile );
  244. }
  245. if( TraceBasicInfo.Flags & TRACE_TRANSFORM_XML ){
  246. DeleteFile( strXSLFile );
  247. }
  248. varg_printf( g_normal, _T("\n") );
  249. switch( hr ){
  250. case ERROR_SUCCESS:
  251. PrintMessage( g_normal, IDS_MESSAGE_SUCCESS );
  252. break;
  253. case ERROR_INVALID_HANDLE:
  254. PrintMessage( g_debug, IDS_MESSAGE_BADFILE );
  255. break;
  256. default:
  257. PrintError( hr );
  258. }
  259. return hr;
  260. }
  261. void ReportStatus( int Status, double Progress )
  262. {
  263. HRESULT hr;
  264. TCHAR buffer[16];
  265. hr = StringCchPrintf( buffer, 16, _T("%1.2f%%"), Progress*100 );
  266. _tprintf( _T("\r") );
  267. varg_printf( g_normal, _T("%1!s!"), buffer );
  268. }
  269. DWORD
  270. ExtractResourceFile( LPWSTR szResourceName, LPWSTR szFileName )
  271. {
  272. DWORD dwStatus = ERROR_SUCCESS;
  273. HANDLE f;
  274. f = CreateFile(
  275. szFileName,
  276. GENERIC_WRITE,
  277. 0,
  278. NULL,
  279. CREATE_ALWAYS,
  280. FILE_ATTRIBUTE_TEMPORARY,
  281. NULL );
  282. if( f ){
  283. HRSRC hRes;
  284. HGLOBAL hResData;
  285. LPSTR buffer = NULL;
  286. hRes = FindResource( NULL, szResourceName, RT_HTML );
  287. if( hRes != NULL ){
  288. DWORD dwSize = SizeofResource( NULL, hRes );
  289. hResData = LoadResource( NULL, hRes );
  290. buffer = (LPSTR)LockResource(hResData);
  291. if( buffer != NULL ){
  292. DWORD dwSizeWritten = 0;
  293. BOOL bResult = WriteFile( f, buffer, dwSize, &dwSizeWritten, NULL );
  294. if( !bResult ){
  295. dwStatus = GetLastError();
  296. }
  297. }
  298. }
  299. CloseHandle(f);
  300. }else{
  301. dwStatus = GetLastError();
  302. }
  303. return dwStatus;
  304. }