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.

123 lines
3.6 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // Tier2 logging helpers.
  4. //
  5. //===============================================================================
  6. #include "tier2_logging.h"
  7. #include "filesystem.h"
  8. #include "interfaces/interfaces.h"
  9. CFileLoggingListener::CFileLoggingListener()
  10. {
  11. for ( int i = 0; i < MAX_SIMULTANEOUS_LOGGING_FILE_COUNT; ++ i )
  12. {
  13. m_OpenFiles[i].Reset();
  14. }
  15. for ( int i = 0; i < MAX_LOGGING_CHANNEL_COUNT; ++ i )
  16. {
  17. m_FileIndices[i] = INVALID_LOGGING_FILE_HANDLE;
  18. }
  19. }
  20. CFileLoggingListener::~CFileLoggingListener()
  21. {
  22. for ( int i = 0; i < MAX_SIMULTANEOUS_LOGGING_FILE_COUNT; ++ i )
  23. {
  24. if ( m_OpenFiles[i].IsOpen() )
  25. {
  26. g_pFullFileSystem->Close( m_OpenFiles[i].m_FileHandle );
  27. m_OpenFiles[i].Reset();
  28. }
  29. }
  30. }
  31. LoggingFileHandle_t CFileLoggingListener::BeginLoggingToFile( const char *pFilename, const char *pOptions, const char *pPathID /* = NULL */ )
  32. {
  33. int fileHandle = GetUnusedFileInfo();
  34. if ( fileHandle != INVALID_LOGGING_FILE_HANDLE )
  35. {
  36. m_OpenFiles[fileHandle].m_FileHandle = g_pFullFileSystem->Open( pFilename, pOptions, pPathID );
  37. }
  38. return fileHandle;
  39. }
  40. void CFileLoggingListener::EndLoggingToFile( LoggingFileHandle_t fileHandle )
  41. {
  42. Assert( fileHandle >= 0 && fileHandle < MAX_SIMULTANEOUS_LOGGING_FILE_COUNT );
  43. /* if the file had any channels associated with it, disassociate them. */
  44. if ( fileHandle != INVALID_LOGGING_FILE_HANDLE )
  45. {
  46. for ( int i = 0 ; i < MAX_LOGGING_CHANNEL_COUNT ; ++i )
  47. {
  48. if ( m_FileIndices[i] == fileHandle )
  49. UnassignLogChannel( i );
  50. }
  51. }
  52. g_pFullFileSystem->Close( m_OpenFiles[fileHandle].m_FileHandle );
  53. m_OpenFiles[fileHandle].Reset();
  54. }
  55. void CFileLoggingListener::AssignLogChannel( LoggingChannelID_t channelID, LoggingFileHandle_t loggingFileHandle )
  56. {
  57. Assert( loggingFileHandle >= 0 && loggingFileHandle < MAX_SIMULTANEOUS_LOGGING_FILE_COUNT );
  58. Assert( m_OpenFiles[loggingFileHandle].IsOpen() );
  59. Assert( channelID >= 0 && channelID < MAX_LOGGING_CHANNEL_COUNT );
  60. m_FileIndices[channelID] = loggingFileHandle;
  61. }
  62. void CFileLoggingListener::UnassignLogChannel( LoggingChannelID_t channelID )
  63. {
  64. Assert( channelID >= 0 && channelID < MAX_LOGGING_CHANNEL_COUNT );
  65. m_FileIndices[channelID] = INVALID_LOGGING_FILE_HANDLE;
  66. }
  67. void CFileLoggingListener::AssignAllLogChannels( LoggingFileHandle_t loggingFileHandle )
  68. {
  69. Assert( loggingFileHandle >= 0 && loggingFileHandle < MAX_SIMULTANEOUS_LOGGING_FILE_COUNT );
  70. Assert( m_OpenFiles[loggingFileHandle].IsOpen() );
  71. for ( int i = 0; i < MAX_LOGGING_CHANNEL_COUNT; ++ i )
  72. {
  73. m_FileIndices[i] = loggingFileHandle;
  74. }
  75. }
  76. void CFileLoggingListener::UnassignAllLogChannels()
  77. {
  78. for ( int i = 0; i < MAX_LOGGING_CHANNEL_COUNT; ++ i )
  79. {
  80. m_FileIndices[i] = INVALID_LOGGING_FILE_HANDLE;
  81. }
  82. }
  83. void CFileLoggingListener::Log( const LoggingContext_t *pContext, const char *pMessage )
  84. {
  85. if ( ( pContext->m_Flags & LCF_CONSOLE_ONLY ) != 0 )
  86. {
  87. return;
  88. }
  89. Assert( pContext->m_ChannelID >= 0 && pContext->m_ChannelID < MAX_LOGGING_CHANNEL_COUNT );
  90. int nFileIndex = m_FileIndices[pContext->m_ChannelID];
  91. if ( nFileIndex >= 0 && nFileIndex < MAX_SIMULTANEOUS_LOGGING_FILE_COUNT )
  92. {
  93. // Shouldn't be trying to log to a closed file.
  94. Assert( m_OpenFiles[nFileIndex].IsOpen() );
  95. g_pFullFileSystem->Write( pMessage, Q_strlen( pMessage ), m_OpenFiles[nFileIndex].m_FileHandle );
  96. g_pFullFileSystem->Flush( m_OpenFiles[nFileIndex].m_FileHandle );
  97. }
  98. }
  99. int CFileLoggingListener::GetUnusedFileInfo() const
  100. {
  101. for ( int i = 0; i < MAX_SIMULTANEOUS_LOGGING_FILE_COUNT; ++ i )
  102. {
  103. if ( !m_OpenFiles[i].IsOpen() )
  104. {
  105. return i;
  106. }
  107. }
  108. return INVALID_LOGGING_FILE_HANDLE;
  109. }