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.

200 lines
3.4 KiB

  1. //
  2. // Universal Resource Consumer: Just an innocent stress program
  3. // Copyright (c) Microsoft Corporation, 1997, 1998, 1999
  4. //
  5. //
  6. // module: error.cxx
  7. // author: silviuc
  8. // created: Fri Apr 10 14:30:35 1998
  9. //
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <windows.h>
  14. #include "error.hxx"
  15. //
  16. // Variable:
  17. //
  18. // ReportingModuleInitialized
  19. //
  20. // Description:
  21. //
  22. // This boolean is false if none of the error reporting
  23. // functions have been called. True otherwise. Every reporting
  24. // function checks this value first and if it is false, then it
  25. // initiates the module setup.
  26. //
  27. static BOOL ReportingModuleInitialized = FALSE;
  28. //
  29. // Variable:
  30. //
  31. // ErrorReportingLock
  32. //
  33. // Description:
  34. //
  35. // All error reporting functions acquire this lock before printing
  36. // anything.
  37. //
  38. static CRITICAL_SECTION ErrorReportingLock;
  39. //
  40. // Variable:
  41. //
  42. // DebugReportingLock
  43. //
  44. // Description:
  45. //
  46. // All debug reporting functions acquire this lock before printing
  47. // anything into debugger.
  48. //
  49. static CRITICAL_SECTION DebugReportingLock;
  50. //
  51. // Function:
  52. //
  53. // CheckIfModuleIsInitialized
  54. //
  55. // Description:
  56. //
  57. // This function is called whenever an error reporting function is called.
  58. // If module is not initialized then we do that.
  59. //
  60. static void
  61. CheckIfModuleIsInitialized ()
  62. {
  63. if (ReportingModuleInitialized == FALSE) {
  64. InitializeCriticalSection (& ErrorReportingLock);
  65. InitializeCriticalSection (& DebugReportingLock);
  66. ReportingModuleInitialized = TRUE;
  67. }
  68. }
  69. //
  70. // Function:
  71. //
  72. // Error
  73. //
  74. // Description:
  75. //
  76. // Printf like function that prints an error message and exits
  77. // with error code 1.
  78. //
  79. void
  80. __cdecl
  81. Error (char *fmt, ...)
  82. {
  83. va_list prms;
  84. CheckIfModuleIsInitialized ();
  85. EnterCriticalSection (& ErrorReportingLock);
  86. va_start (prms, fmt);
  87. printf ("Consume: Error: ");
  88. vprintf (fmt, prms);
  89. printf("\n");
  90. va_end (prms);
  91. LeaveCriticalSection (& ErrorReportingLock);
  92. exit (1);
  93. }
  94. //
  95. // Function:
  96. //
  97. // Warning
  98. //
  99. // Description:
  100. //
  101. // Printf like function that print a warning message.
  102. //
  103. void
  104. __cdecl
  105. Warning (char *fmt, ...)
  106. {
  107. va_list prms;
  108. CheckIfModuleIsInitialized ();
  109. EnterCriticalSection (& ErrorReportingLock);
  110. va_start (prms, fmt);
  111. printf ("Consume: Warning: ");
  112. vprintf (fmt, prms);
  113. printf("\n");
  114. va_end (prms);
  115. LeaveCriticalSection (& ErrorReportingLock);
  116. }
  117. //
  118. // Function:
  119. //
  120. // Message
  121. //
  122. // Description:
  123. //
  124. // Printf like function that prints a message.
  125. //
  126. void
  127. __cdecl
  128. Message (char *fmt, ...)
  129. {
  130. va_list prms;
  131. CheckIfModuleIsInitialized ();
  132. EnterCriticalSection (& ErrorReportingLock);
  133. va_start (prms, fmt);
  134. printf ("Consume: Message: ");
  135. vprintf (fmt, prms);
  136. printf("\n");
  137. va_end (prms);
  138. LeaveCriticalSection (& ErrorReportingLock);
  139. }
  140. //
  141. // Function:
  142. //
  143. // DebugMessage
  144. //
  145. // Description:
  146. //
  147. // Printf like function that prints a message into debugger.
  148. //
  149. void
  150. __cdecl
  151. DebugMessage (char *fmt, ...)
  152. {
  153. va_list prms;
  154. TCHAR Buffer [1024];
  155. TCHAR SecondBuffer [1024];
  156. CheckIfModuleIsInitialized ();
  157. EnterCriticalSection (& DebugReportingLock);
  158. va_start (prms, fmt);
  159. vsprintf (Buffer, fmt, prms);
  160. sprintf (SecondBuffer, "%s %s\n", "Consume: Debug: ", Buffer);
  161. OutputDebugString (SecondBuffer);
  162. va_end (prms);
  163. LeaveCriticalSection (& DebugReportingLock);
  164. }
  165. //
  166. // end of module: error.cxx
  167. //