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.

250 lines
5.0 KiB

  1. // Copyright (c) Microsoft. All rights reserved.
  2. //
  3. // This is unpublished source code of Microsoft.
  4. // The copyright notice above does not evidence any
  5. // actual or intended publication of such source code.
  6. // OneLiner : Implementation of MWmiInstance
  7. // DevUnit : wlbstest
  8. // Author : Murtaza Hakim
  9. // include files
  10. #include "MTrace.h"
  11. #include <time.h>
  12. // initialize static variables.
  13. //
  14. MTrace* MTrace::_instance = 0;
  15. // constructor
  16. //
  17. MTrace::MTrace()
  18. : _traceFile( "tracedata.txt"),
  19. _level( WARNING )
  20. {}
  21. // Instance
  22. //
  23. MTrace*
  24. MTrace::Instance()
  25. {
  26. if( _instance == 0 )
  27. {
  28. _instance = new MTrace;
  29. }
  30. return _instance;
  31. }
  32. // Initialize
  33. // traceFile specified.
  34. void
  35. MTrace::Initialize( TraceLevel level,
  36. string traceFile )
  37. {
  38. char temp[MAX_MESSAGE_SIZE];
  39. _traceFile = traceFile;
  40. FILE* stream = fopen( _traceFile.c_str(), "w");
  41. if( stream == 0 )
  42. {
  43. cout << "not able to initialize trace file " << _traceFile.c_str() << endl;
  44. }
  45. else
  46. {
  47. fclose( stream );
  48. }
  49. _level = level;
  50. }
  51. // Initialize
  52. // no trace file specified. Data written to tracedata.txt
  53. void
  54. MTrace::Initialize( TraceLevel level )
  55. {
  56. FILE* stream = fopen( _traceFile.c_str(), "w");
  57. if( stream == 0 )
  58. {
  59. cout << "not able to initialize trace file " << _traceFile.c_str() << endl;
  60. }
  61. else
  62. {
  63. fclose( stream );
  64. }
  65. _level = level;
  66. }
  67. // SendTraceOutput
  68. // wchar interface.
  69. void
  70. MTrace::SendTraceOutput( TraceLevel level,
  71. wstring traceMessage )
  72. {
  73. FILE* stream;
  74. if( level >= _level )
  75. {
  76. char temp[ MAX_MESSAGE_SIZE ];
  77. WCharToChar( (unsigned short *) traceMessage.c_str(), wcslen( traceMessage.c_str() ) + 1,
  78. temp, MAX_MESSAGE_SIZE );
  79. FormatOutput( level,
  80. temp );
  81. }
  82. }
  83. // SendTraceOutput
  84. // char interface.
  85. void
  86. MTrace::SendTraceOutput( TraceLevel level,
  87. string traceMessage )
  88. {
  89. if( level >= _level )
  90. {
  91. FormatOutput( level, traceMessage );
  92. }
  93. }
  94. void
  95. MTrace::FormatOutput( TraceLevel level,
  96. string traceMessage )
  97. {
  98. FILE* stream;
  99. string levelToPrint;
  100. switch( level )
  101. {
  102. case INFO:
  103. levelToPrint = "INFO :";
  104. break;
  105. case WARNING:
  106. levelToPrint = "WARNING :";
  107. break;
  108. case SEVERE_ERROR:
  109. levelToPrint = "SEVERE_ERROR :";
  110. break;
  111. default :
  112. levelToPrint = "Unknown Level :";
  113. break;
  114. }
  115. // get time.
  116. struct tm when;
  117. time_t now;
  118. time( &now );
  119. when = *( localtime( &now ) );
  120. // write to standard output
  121. cout << levelToPrint << asctime( &when ) << traceMessage << endl;
  122. // write to trace file
  123. stream = fopen( _traceFile.c_str(), "a+");
  124. if( stream == 0 )
  125. {
  126. cout << "not able to write trace data to file" << endl;
  127. }
  128. else
  129. {
  130. fprintf( stream, levelToPrint.c_str() );
  131. fprintf( stream, asctime( &when ) );
  132. fprintf( stream, traceMessage.c_str() );
  133. fclose( stream );
  134. }
  135. }
  136. // GetLevel
  137. //
  138. MTrace::TraceLevel
  139. MTrace::GetLevel()
  140. {
  141. return _level;
  142. }
  143. // SetLevel
  144. //
  145. void
  146. MTrace::SetLevel( TraceLevel level)
  147. {
  148. _level = level;
  149. }
  150. // TRACE
  151. // char version
  152. void
  153. MYTRACE( int lineNum, string fileName, MTrace::TraceLevel level, string traceMessage )
  154. {
  155. MTrace* instance = MTrace::Instance();
  156. // use lineNum information to send to output string.
  157. char temp[100];
  158. sprintf( temp, "%d", lineNum );
  159. string newMessage = fileName + " :" + string( temp ) + " : " + traceMessage;
  160. instance->SendTraceOutput( level, newMessage );
  161. }
  162. // TRACE
  163. // wchar version
  164. void
  165. MYTRACE( int lineNum, string fileName, MTrace::TraceLevel level, wstring traceMessage )
  166. {
  167. MTrace* instance = MTrace::Instance();
  168. wchar_t temp[100];
  169. swprintf( temp, L"%d", lineNum );
  170. wchar_t fileName_wc[1000];
  171. CharToWChar( (char *) fileName.c_str(), strlen( fileName.c_str() ) + 1,
  172. fileName_wc, 1000 );
  173. wstring newMessage = wstring( fileName_wc ) + L" :" + wstring( temp ) + L" :" + traceMessage;
  174. instance->SendTraceOutput( level, newMessage );
  175. }
  176. // utility functions.
  177. //
  178. void
  179. CharToWChar( PCHAR pchCharString,
  180. int iSizeOfCharString,
  181. PWCHAR pwchCharString,
  182. int iSizeOfWideCharString )
  183. {
  184. for (int i=0; i<= iSizeOfCharString; i++)
  185. {
  186. pwchCharString[i] = (CHAR)(pchCharString[i]);
  187. }
  188. }
  189. void
  190. WCharToChar( PWCHAR pwchWideCharString,
  191. int iSizeOfWCharString,
  192. PCHAR pchCharString,
  193. int iSizeOfCharString )
  194. {
  195. for (int i=0; i<= iSizeOfWCharString; i++)
  196. {
  197. pchCharString[i] = (CHAR)(pwchWideCharString[i]);
  198. }
  199. }