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.

174 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "textstatsmgr.h"
  9. #include "tier0/dbg.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. CTextStatsMgr g_TextStatsMgr; // The default text stats manager.
  13. // ------------------------------------------------------------------------------------------ //
  14. // CTextStatsMgr implementation.
  15. // ------------------------------------------------------------------------------------------ //
  16. CTextStatsMgr::CTextStatsMgr( void )
  17. {
  18. m_szStatFilename[0] = 0;
  19. }
  20. bool CTextStatsMgr::WriteFile( IFileSystem *pFileSys, const char *pFilename )
  21. {
  22. // If no filename was specified, use out preset one
  23. if ( !pFilename )
  24. {
  25. pFilename = m_szStatFilename;
  26. }
  27. FileHandle_t hFile = pFileSys->Open( pFilename, "wt", "LOGDIR" );
  28. if ( hFile == FILESYSTEM_INVALID_HANDLE )
  29. return false;
  30. CTextStat *pHead = CTextStat::GetTextStatsList();
  31. for ( CTextStat *pCur=pHead->m_pNext; pCur != pHead; pCur=pCur->m_pNext )
  32. {
  33. if ( pCur->m_pMgr == this )
  34. pCur->m_PrintFn( pFileSys, hFile, pCur->m_pUserData );
  35. }
  36. pFileSys->Close( hFile );
  37. // Call each CTextStatFile..
  38. for( CTextStatFile *pCurFile=CTextStatFile::s_pHead; pCurFile; pCurFile=pCurFile->m_pNext )
  39. {
  40. pCurFile->m_pFn();
  41. }
  42. return true;
  43. }
  44. char *CTextStatsMgr::GetStatsFilename( void )
  45. {
  46. return m_szStatFilename;
  47. }
  48. void CTextStatsMgr::SetStatsFilename( char *sFilename )
  49. {
  50. Assert( sFilename && sFilename[0] );
  51. Q_strncpy( m_szStatFilename, sFilename, sizeof(m_szStatFilename) );
  52. }
  53. // ------------------------------------------------------------------------------------------ //
  54. // CTextStat implementation.
  55. // ------------------------------------------------------------------------------------------ //
  56. CTextStat::CTextStat()
  57. {
  58. m_pPrev = m_pNext = this;
  59. m_pMgr = NULL;
  60. }
  61. CTextStat::CTextStat( TextStatPrintFn printFn, void *pUserData, CTextStatsMgr *pMgr )
  62. {
  63. m_pPrev = m_pNext = this;
  64. Init( printFn, pUserData, pMgr );
  65. }
  66. CTextStat::~CTextStat()
  67. {
  68. Term();
  69. }
  70. void CTextStat::Init( TextStatPrintFn printFn, void *pUserData, CTextStatsMgr *pMgr )
  71. {
  72. Term();
  73. m_pPrev = GetTextStatsList();
  74. m_pNext = GetTextStatsList()->m_pNext;
  75. m_pPrev->m_pNext = m_pNext->m_pPrev = this;
  76. m_PrintFn = printFn;
  77. m_pUserData = pUserData;
  78. m_pMgr = pMgr;
  79. }
  80. void CTextStat::Term()
  81. {
  82. // Remove from the global list.
  83. m_pPrev->m_pNext = m_pNext;
  84. m_pNext->m_pPrev = m_pPrev;
  85. m_pPrev = m_pNext = this;
  86. m_pMgr = NULL;
  87. }
  88. CTextStat::CTextStat( bool bGlobalListHead )
  89. {
  90. Assert( bGlobalListHead );
  91. m_pPrev = m_pNext = this;
  92. }
  93. CTextStat* CTextStat::GetTextStatsList()
  94. {
  95. static CTextStat theList( true );
  96. return &theList;
  97. }
  98. void CTextStat::RemoveFn( void *pUserData )
  99. {
  100. CTextStat *pReg = (CTextStat*)pUserData;
  101. pReg->Term();
  102. }
  103. // ------------------------------------------------------------------------------------------ //
  104. // CTextStatInt implementation.
  105. // ------------------------------------------------------------------------------------------ //
  106. CTextStatInt::CTextStatInt( const char *pName, int initialValue, CTextStatsMgr *pMgr )
  107. {
  108. m_pName = pName;
  109. m_Value = initialValue;
  110. m_Reg.Init( &CTextStatInt::PrintFn, this, pMgr );
  111. }
  112. void CTextStatInt::PrintFn( IFileSystem *pFileSys, FileHandle_t hFile, void *pUserData )
  113. {
  114. CTextStatInt *pStat = (CTextStatInt*)pUserData;
  115. pFileSys->FPrintf( hFile, "%s %d\n", pStat->m_pName, pStat->m_Value );
  116. }
  117. // ------------------------------------------------------------------------------------------ //
  118. // CTextStatFile functions.
  119. // ------------------------------------------------------------------------------------------ //
  120. CTextStatFile *CTextStatFile::s_pHead = NULL;
  121. CTextStatFile::CTextStatFile( TextStatFileFn fn )
  122. {
  123. m_pFn = fn;
  124. m_pNext = CTextStatFile::s_pHead;
  125. CTextStatFile::s_pHead = this;
  126. }