Team Fortress 2 Source Code as on 22/4/2020
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.

191 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include <windows.h>
  9. #include "tier0/icommandline.h"
  10. #include <stdio.h>
  11. #include "tier0/dbg.h"
  12. static unsigned short g_InitialColor = 0xFFFF;
  13. static unsigned short g_LastColor = 0xFFFF;
  14. static unsigned short g_BadColor = 0xFFFF;
  15. static WORD g_BackgroundFlags = 0xFFFF;
  16. static void GetInitialColors( )
  17. {
  18. // Get the old background attributes.
  19. CONSOLE_SCREEN_BUFFER_INFO oldInfo;
  20. GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &oldInfo );
  21. g_InitialColor = g_LastColor = oldInfo.wAttributes & (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
  22. g_BackgroundFlags = oldInfo.wAttributes & (BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY);
  23. g_BadColor = 0;
  24. if (g_BackgroundFlags & BACKGROUND_RED)
  25. g_BadColor |= FOREGROUND_RED;
  26. if (g_BackgroundFlags & BACKGROUND_GREEN)
  27. g_BadColor |= FOREGROUND_GREEN;
  28. if (g_BackgroundFlags & BACKGROUND_BLUE)
  29. g_BadColor |= FOREGROUND_BLUE;
  30. if (g_BackgroundFlags & BACKGROUND_INTENSITY)
  31. g_BadColor |= FOREGROUND_INTENSITY;
  32. }
  33. static WORD SetConsoleTextColor( int red, int green, int blue, int intensity )
  34. {
  35. WORD ret = g_LastColor;
  36. g_LastColor = 0;
  37. if( red ) g_LastColor |= FOREGROUND_RED;
  38. if( green ) g_LastColor |= FOREGROUND_GREEN;
  39. if( blue ) g_LastColor |= FOREGROUND_BLUE;
  40. if( intensity ) g_LastColor |= FOREGROUND_INTENSITY;
  41. // Just use the initial color if there's a match...
  42. if (g_LastColor == g_BadColor)
  43. g_LastColor = g_InitialColor;
  44. SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), g_LastColor | g_BackgroundFlags );
  45. return ret;
  46. }
  47. static void RestoreConsoleTextColor( WORD color )
  48. {
  49. SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color | g_BackgroundFlags );
  50. g_LastColor = color;
  51. }
  52. void CmdLib_Exit( int exitCode )
  53. {
  54. TerminateProcess( GetCurrentProcess(), 1 );
  55. }
  56. CRITICAL_SECTION g_SpewCS;
  57. bool g_bSpewCSInitted = false;
  58. bool g_bSuppressPrintfOutput = false;
  59. SpewRetval_t CmdLib_SpewOutputFunc( SpewType_t type, char const *pMsg )
  60. {
  61. // Hopefully two threads won't call this simultaneously right at the start!
  62. if ( !g_bSpewCSInitted )
  63. {
  64. InitializeCriticalSection( &g_SpewCS );
  65. g_bSpewCSInitted = true;
  66. }
  67. WORD old;
  68. SpewRetval_t retVal;
  69. EnterCriticalSection( &g_SpewCS );
  70. {
  71. if (( type == SPEW_MESSAGE ) || (type == SPEW_LOG ))
  72. {
  73. old = SetConsoleTextColor( 1, 1, 1, 0 );
  74. retVal = SPEW_CONTINUE;
  75. }
  76. else if( type == SPEW_WARNING )
  77. {
  78. old = SetConsoleTextColor( 1, 1, 0, 1 );
  79. retVal = SPEW_CONTINUE;
  80. }
  81. else if( type == SPEW_ASSERT )
  82. {
  83. old = SetConsoleTextColor( 1, 0, 0, 1 );
  84. retVal = SPEW_DEBUGGER;
  85. #ifdef MPI
  86. // VMPI workers don't want to bring up dialogs and suchlike.
  87. if ( g_bUseMPI && !g_bMPIMaster )
  88. {
  89. VMPI_HandleCrash( pMsg, true );
  90. exit( 0 );
  91. }
  92. #endif
  93. }
  94. else if( type == SPEW_ERROR )
  95. {
  96. old = SetConsoleTextColor( 1, 0, 0, 1 );
  97. retVal = SPEW_ABORT; // doesn't matter.. we exit below so we can return an errorlevel (which dbg.dll doesn't do).
  98. }
  99. else
  100. {
  101. old = SetConsoleTextColor( 1, 1, 1, 1 );
  102. retVal = SPEW_CONTINUE;
  103. }
  104. if ( !g_bSuppressPrintfOutput || type == SPEW_ERROR )
  105. printf( "%s", pMsg );
  106. OutputDebugString( pMsg );
  107. if ( type == SPEW_ERROR )
  108. {
  109. printf( "\n" );
  110. OutputDebugString( "\n" );
  111. }
  112. RestoreConsoleTextColor( old );
  113. }
  114. LeaveCriticalSection( &g_SpewCS );
  115. if ( type == SPEW_ERROR )
  116. {
  117. CmdLib_Exit( 1 );
  118. }
  119. return retVal;
  120. }
  121. void InstallSpewFunction()
  122. {
  123. setvbuf( stdout, NULL, _IONBF, 0 );
  124. setvbuf( stderr, NULL, _IONBF, 0 );
  125. SpewOutputFunc( CmdLib_SpewOutputFunc );
  126. GetInitialColors();
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Tests the process.cpp stuff
  130. //-----------------------------------------------------------------------------
  131. int main( int argc, char **argv )
  132. {
  133. CommandLine()->CreateCmdLine( argc, argv );
  134. InstallSpewFunction();
  135. float flDelay = CommandLine()->ParmValue( "-delay", 0.0f );
  136. const char *pEndMessage = CommandLine()->ParmValue( "-message", "Test Finished!\n" );
  137. int nEndExtraBytes = CommandLine()->ParmValue( "-extrabytes", 0 );
  138. if ( flDelay > 0.0f )
  139. {
  140. float t = Plat_FloatTime();
  141. while ( Plat_FloatTime() - t < flDelay )
  142. {
  143. }
  144. }
  145. Msg( pEndMessage );
  146. if ( nEndExtraBytes )
  147. {
  148. while( --nEndExtraBytes >= 0 )
  149. {
  150. Msg( "%c", ( nEndExtraBytes % 10 ) + '0' );
  151. }
  152. }
  153. return 0;
  154. }