Counter Strike : Global Offensive Source Code
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.

139 lines
9.2 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. namespace GCSDK
  10. {
  11. //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
  12. //outputs to control log spamming
  13. class CGCEmitGroup
  14. {
  15. public:
  16. //the different severity levels for a message
  17. enum EMsgLevel
  18. {
  19. kMsg_Error = 1,
  20. kMsg_Warning = 2,
  21. kMsg_Msg = 3,
  22. kMsg_Verbose = 4
  23. };
  24. //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
  25. //that to help with consistency, DECLARE_GC_EMIT_GROUP should be used for declaring these objects over manually providing names for all fields
  26. CGCEmitGroup( const char* pszGroupName, const char* pszConsoleVar, const char* pszLogVar, const char* pszDefaultConsole, const char* pszDefaultLog )
  27. : m_pszGroupName( pszGroupName )
  28. #ifdef GC
  29. , 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 )
  30. , 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 )
  31. #endif
  32. {}
  33. //get the name of the group and current filter levels
  34. const char* GetName() const { return m_pszGroupName; }
  35. #ifdef GC
  36. int GetConsoleLevel() const { return m_ConsoleLevel.GetInt(); }
  37. int GetLogLevel() const { return m_LogLevel.GetInt(); }
  38. #else
  39. int GetConsoleLevel() const { return 3; }
  40. int GetLogLevel() const { return 3; }
  41. #endif
  42. //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
  43. //of all the command line arguments can be costly, and the EG_XXXX macros should be used instead to avoid this cost.
  44. void Internal_AssertError( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  45. void Internal_Error( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  46. void Internal_Warning( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  47. void Internal_Msg( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  48. void Internal_Verbose( PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 2, 3 );
  49. void Internal_Emit( EMsgLevel eLvl, PRINTF_FORMAT_STRING const char *pchMsg, ... ) const FMTFUNCTION( 3, 4 );
  50. //same as the above, but takes a var args structure
  51. void AssertErrorV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  52. void ErrorV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  53. void WarningV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  54. void MsgV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  55. void VerboseV( PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  56. void EmitV( EMsgLevel eLvl, PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs ) const;
  57. private:
  58. //the display name of this group, must be a static string
  59. const char* m_pszGroupName;
  60. #ifdef GC
  61. //the console variable used to control the log level that will be recorded to the ouput logs [0..4]
  62. GCConVar m_LogLevel;
  63. //the console variable used to control the level of output that will be displayed to the console [0..4]
  64. GCConVar m_ConsoleLevel;
  65. #endif
  66. };
  67. //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.
  68. //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
  69. #define EG_ASSERTERROR( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Error || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Error ) EmitGroup.Internal_AssertError( __VA_ARGS__ ); } while(0)
  70. #define EG_ERROR( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Error || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Error ) EmitGroup.Internal_Error( __VA_ARGS__ ); } while(0)
  71. #define EG_WARNING( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Warning || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Warning ) EmitGroup.Internal_Warning( __VA_ARGS__ ); } while(0)
  72. #define EG_MSG( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Msg || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Msg ) EmitGroup.Internal_Msg( __VA_ARGS__ ); } while(0)
  73. #define EG_VERBOSE( EmitGroup, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Verbose || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Verbose ) EmitGroup.Internal_Verbose( __VA_ARGS__ ); } while(0)
  74. #define EG_EMIT( EmitGroup, eLvl, ... ) do{ if( EmitGroup.GetConsoleLevel() >= eLvl || EmitGroup.GetLogLevel() >= eLvl ) EmitGroup.Internal_Emit( eLvl, __VA_ARGS__ ); } while(0)
  75. #ifdef GC
  76. // Return true if we have extra logging enabled for the specified Steam ID.
  77. extern bool BDetailedLoggingEnabledForAnySteamID();
  78. extern bool BDetailedLoggingEnabledForSteamID( CSteamID steamID );
  79. extern bool BDetailedLoggingEnabledForAccountID( AccountID_t nAccountID );
  80. // Emit info about a particular steam ID. This will emit/log if the level is enabled for
  81. // the specified spew group, *or* if debugging is enabled for that particular SteamID.
  82. #define EG_MSG_STEAMID( EmitGroup, steamID, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Msg || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Msg || BDetailedLoggingEnabledForSteamID( steamID ) ) EmitGroup.Internal_Msg( __VA_ARGS__ ); } while(0)
  83. #define EG_VERBOSE_STEAMID( EmitGroup, steamID, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Verbose || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Verbose || BDetailedLoggingEnabledForSteamID( steamID ) ) EmitGroup.Internal_Verbose( __VA_ARGS__ ); } while(0)
  84. #define EG_MSG_ACCOUNTID( EmitGroup, accountID, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Msg || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Msg || BDetailedLoggingEnabledForAccountID( accountID ) ) EmitGroup.Internal_Msg( __VA_ARGS__ ); } while(0)
  85. #define EG_VERBOSE_ACCOUNTID( EmitGroup, accountID, ... ) do{ if( EmitGroup.GetConsoleLevel() >= CGCEmitGroup::kMsg_Verbose || EmitGroup.GetLogLevel() >= CGCEmitGroup::kMsg_Verbose || BDetailedLoggingEnabledForAccountID( accountID ) ) EmitGroup.Internal_Verbose( __VA_ARGS__ ); } while(0)
  86. #endif
  87. //----------------------------------------
  88. //Legacy interface support - Where possible, use the CGCEmitGroup above for more robust functionality
  89. //----------------------------------------
  90. extern CGCEmitGroup SPEW_SYSTEM_MISC;
  91. extern CGCEmitGroup SPEW_JOB;
  92. extern CGCEmitGroup SPEW_CONSOLE;
  93. extern CGCEmitGroup SPEW_GC;
  94. extern CGCEmitGroup SPEW_SQL;
  95. extern CGCEmitGroup SPEW_NETWORK;
  96. extern CGCEmitGroup SPEW_SHAREDOBJ;
  97. extern CGCEmitGroup SPEW_MICROTXN;
  98. extern CGCEmitGroup SPEW_PROMO;
  99. extern CGCEmitGroup SPEW_PKGITEM;
  100. extern CGCEmitGroup SPEW_ECONOMY;
  101. extern CGCEmitGroup SPEW_THREADS;
  102. //this one is a macro since it is called in many places and can add considerable overhead to the formatting
  103. void EGInternal_EmitInfo( const CGCEmitGroup& Group, int iLevel, int iLevelLog, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 4, 5 );
  104. #define EmitInfo( EmitGroup, ConsoleLevel, LogLevel, ... ) do{ if( EmitGroup.GetConsoleLevel() >= ( ConsoleLevel ) || EmitGroup.GetLogLevel() >= ( LogLevel ) ) EGInternal_EmitInfo( EmitGroup, ConsoleLevel, LogLevel, __VA_ARGS__ ); } while(0)
  105. void EmitInfoV( const CGCEmitGroup& Group, int iLevel, int iLevelLog, PRINTF_FORMAT_STRING const char *pchMsg, va_list vaArgs );
  106. void EmitWarning( const CGCEmitGroup& Group, int iLevel, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 3, 4 );
  107. void EmitError( const CGCEmitGroup& Group, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 2, 3 );
  108. // Emit an assert-like error, generating a minidump
  109. void EmitAssertError( const CGCEmitGroup& Group, PRINTF_FORMAT_STRING const char *pchMsg, ... ) FMTFUNCTION( 2, 3 );
  110. } // namespace GCSDK
  111. //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'
  112. #define DECLARE_GC_EMIT_GROUP_DEFAULTS( VarName, GroupName, ConsoleLevel, LogLevel ) \
  113. GCSDK::CGCEmitGroup VarName( #GroupName, "console_level_" #GroupName, "log_level_" #GroupName, #ConsoleLevel, #LogLevel ); \
  114. typedef uint32 __TEmitGroupSanityCheck_##GroupName_##ConsoleLevel_##LogLevel;
  115. //a utility macro that assists in creating emit groups in a name consistent manner
  116. #define DECLARE_GC_EMIT_GROUP( VarName, GroupName ) DECLARE_GC_EMIT_GROUP_DEFAULTS( VarName, GroupName, 3, 3 )
  117. #endif // GCLOGGER_H