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.

84 lines
2.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #pragma once
  8. #include <tuple>
  9. #include <string>
  10. #include <sstream>
  11. template <class Object, class RecordType>
  12. class InstanceLogger
  13. {
  14. public:
  15. typedef RecordType RecordType_t;
  16. protected:
  17. void InstanceLog( const char* pMsg )
  18. {
  19. m_records.AddToTail( BuildRecord( pMsg ) );
  20. }
  21. void DumpInstanceLog( )
  22. {
  23. CUtlString str;
  24. Msg( "--------\nDumping instance log for 0x%08p\n", this );
  25. for ( auto it : m_records )
  26. {
  27. FormatRecord( &str, it );
  28. Msg( "%s\n", str.Get() );
  29. }
  30. Msg( "========\nInstance log complete for 0x%08p\n", this );
  31. }
  32. virtual RecordType_t BuildRecord( const char* pMsg ) = 0;
  33. private:
  34. CUtlVector< RecordType > m_records;
  35. };
  36. // // Your class needs to implement something like this. You don't have to use a tuple,
  37. // // but if you don't you also need to implement FormatRecord for your type.
  38. // RecordType BuildRecord( const char* pMsg )
  39. // {
  40. // return std::make_tuple( this, pMsg, g_FrameNum, Plat_FloatTime(), m_nAllocatedWidth, m_nAllocatedHeight, m_nAllocatedDepth, m_nTargetResidence, m_nCurrentResidence );
  41. // }
  42. // helper function to print a tuple of any size
  43. template<class Tuple, std::size_t N>
  44. struct TuplePrinter {
  45. static void print( std::stringstream& os, const Tuple& t )
  46. {
  47. TuplePrinter<Tuple, N - 1>::print( os, t );
  48. os << ", " << std::get<N - 1>( t );
  49. }
  50. };
  51. template<class Tuple>
  52. struct TuplePrinter < Tuple, 1 > {
  53. static void print( std::stringstream& os, const Tuple& t )
  54. {
  55. os << std::get<0>( t );
  56. }
  57. };
  58. template<class... Args>
  59. void format( std::stringstream& os, const std::tuple<Args...>& t )
  60. {
  61. os << "( ";
  62. TuplePrinter<decltype( t ), sizeof...( Args )>::print( os, t );
  63. os << " )";
  64. }
  65. // end helper function
  66. template <class RecordType>
  67. void FormatRecord( CUtlString *pOutStr, const RecordType& rt )
  68. {
  69. std::stringstream stream;
  70. format( stream, rt );
  71. ( *pOutStr ) = stream.str().c_str();
  72. }