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.

131 lines
8.1 KiB

  1. //========= Copyright (c), Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef GCLOGGER_H
  8. #define GCLOGGER_H
  9. #ifdef GC
  10. #include "gc_convar.h"
  11. #endif
  12. namespace GCSDK
  13. {
  14. //a class that defines an output group for messages that can be output to the console or to a log. This allows for individual filtering of different groups to different
  15. //outputs to control log spamming
  16. class CGCEmitGroup
  17. {
  18. public:
  19. //the different severity levels for a message
  20. enum EMsgLevel
  21. {
  22. kMsg_Error = 1,
  23. kMsg_Warning = 2,
  24. kMsg_Msg = 3,
  25. kMsg_Verbose = 4
  26. };
  27. //constructs a group given a static string for the name of the group, and the name for the console variables that control the console and log output levels. Note
  28. //that to help with consistency, DECLARE_GC_EMIT_GROUP should be used for declaring these objects over manually providing names for all fields
  29. CGCEmitGroup( const char* pszGroupName, const char* pszConsoleVar, const char* pszLogVar, const char* pszDefaultConsole, const char* pszDefaultLog )
  30. : m_pszGroupName( pszGroupName )
  31. #ifdef GC
  32. , m_LogLevel( pszLogVar, pszDefaultLog, 0, "The output level to log for this system. 1 means log only critical errors, 2 means log errors and warnings, 3 means log errors, warnings and messages, 4 means log everything", true, 0.0f, true, 4.0f )
  33. , m_ConsoleLevel( pszConsoleVar, pszDefaultConsole, 0, "The output level to log for this system. 1 means log only critical errors, 2 means log errors and warnings, 3 means log errors, warnings and messages, 4 means log everything", true, 0.0f, true, 4.0f )
  34. #endif
  35. {}
  36. //get the name of the group and current filter levels
  37. const char* GetName() const { return m_pszGroupName; }
  38. #ifdef GC
  39. int GetConsoleLevel() const { return m_ConsoleLevel.GetInt(); }
  40. int GetLogLevel() const { return m_LogLevel.GetInt(); }
  41. #else
  42. int GetConsoleLevel() const { return 3; }
  43. int GetLogLevel() const { return 3; }
  44. #endif
  45. //these will output text for each of the appropriate severities, from Verbose (defaulted to filtered out), to critical error. These are not intended to be called directly, as the preparation
  46. //of all the command line arguments can be costly, and the EG_XXXX macros should be used instead to avoid this cost.
  47. void Internal_AssertError( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  48. void Internal_Error( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  49. void Internal_Warning( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  50. void Internal_Msg( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  51. void Internal_Verbose( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  52. void Internal_Emit( EMsgLevel eLvl, PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 3, 4 );
  53. void Internal_BoldMsg( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  54. //same as the above, but takes a var args structure
  55. void AssertErrorV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  56. void ErrorV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  57. void WarningV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  58. void MsgV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  59. void VerboseV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  60. void EmitV( EMsgLevel eLvl, PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  61. void BoldMsgV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  62. private:
  63. //the display name of this group, must be a static string
  64. const char* m_pszGroupName;
  65. #ifdef GC
  66. //the console variable used to control the log level that will be recorded to the ouput logs [0..4]
  67. GCConVar m_LogLevel;
  68. //the console variable used to control the level of output that will be displayed to the console [0..4]
  69. GCConVar m_ConsoleLevel;
  70. #endif
  71. };
  72. //macros to emit to an emit group. These should be used so that you don't have to pay the cost of formatting parameters that won't be used if the output isn't turned on anyway.
  73. //Note the use of do{}while, this to prevent the if from pairing with a following else if they don't use braces around the print
  74. #define EG_ASSERTERROR( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Error || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Error ) EmitGroup.Internal_AssertError( __VA_ARGS__ ); } while(0)
  75. #define EG_ERROR( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Error || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Error ) EmitGroup.Internal_Error( __VA_ARGS__ ); } while(0)
  76. #define EG_WARNING( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Warning || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Warning ) EmitGroup.Internal_Warning( __VA_ARGS__ ); } while(0)
  77. #define EG_MSG( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Msg || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Msg ) EmitGroup.Internal_Msg( __VA_ARGS__ ); } while(0)
  78. #define EG_VERBOSE( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Verbose || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Verbose ) EmitGroup.Internal_Verbose( __VA_ARGS__ ); } while(0)
  79. #define EG_EMIT( EmitGroup, eLvl, ... ) do{ if( EmitGroup.GetConsoleLevel() >= eLvl || EmitGroup.GetLogLevel() >= eLvl ) EmitGroup.Internal_Emit( eLvl, __VA_ARGS__ ); } while(0)
  80. #define EG_BOLDMSG( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Error || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Error ) EmitGroup.Internal_BoldMsg( __VA_ARGS__ ); } while(0)
  81. //----------------------------------------
  82. //Legacy interface support - Where possible, use the CGCEmitGroup above for more robust functionality
  83. //----------------------------------------
  84. extern CGCEmitGroup SPEW_SYSTEM_MISC;
  85. extern CGCEmitGroup SPEW_JOB;
  86. extern CGCEmitGroup SPEW_CONSOLE;
  87. extern CGCEmitGroup SPEW_GC;
  88. extern CGCEmitGroup SPEW_SQL;
  89. extern CGCEmitGroup SPEW_NETWORK;
  90. extern CGCEmitGroup SPEW_SHAREDOBJ;
  91. extern CGCEmitGroup SPEW_MICROTXN;
  92. extern CGCEmitGroup SPEW_PROMO;
  93. extern CGCEmitGroup SPEW_PKGITEM;
  94. extern CGCEmitGroup SPEW_ECONOMY;
  95. extern CGCEmitGroup SPEW_THREADS;
  96. //this one is a macro since it is called in many places and can add considerable overhead to the formatting
  97. void EGInternal_EmitInfo( const CGCEmitGroup& Group, int iLevel, int iLevelLog, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 4, 5 );
  98. #define EmitInfo( EmitGroup, ConsoleLevel, LogLevel, ... ) do{ if( EmitGroup.GetConsoleLevel() >= ( ConsoleLevel ) || EmitGroup.GetLogLevel() >= ( LogLevel ) ) EGInternal_EmitInfo( EmitGroup, ConsoleLevel, LogLevel, __VA_ARGS__ ); } while(0)
  99. void EmitInfoV( const CGCEmitGroup& Group, int iLevel, int iLevelLog, PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs );
  100. void EmitWarning( const CGCEmitGroup& Group, int iLevel, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 3, 4 );
  101. void EmitError( const CGCEmitGroup& Group, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 2, 3 );
  102. // Emit an assert-like error, generating a minidump
  103. void EmitAssertError( const CGCEmitGroup& Group, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 2, 3 );
  104. } // namespace GCSDK
  105. //the use of the typedef is to catch issues with people putting quotes around the group name. Note that the emit group comes first in case they precede this with 'static'
  106. #define DECLARE_GC_EMIT_GROUP_DEFAULTS( VarName, GroupName, ConsoleLevel, LogLevel ) \
  107. GCSDK::CGCEmitGroup VarName( #GroupName, "console_level_" #GroupName, "log_level_" #GroupName, #ConsoleLevel, #LogLevel ); \
  108. typedef uint32 __TEmitGroupSanityCheck_##GroupName_##ConsoleLevel_##LogLevel;
  109. //a utility macro that assists in creating emit groups in a name consistent manner
  110. #define DECLARE_GC_EMIT_GROUP( VarName, GroupName ) DECLARE_GC_EMIT_GROUP_DEFAULTS( VarName, GroupName, 3, 3 )
  111. #endif // GCLOGGER_H