DeterminismLog.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. //#define JPH_ENABLE_DETERMINISM_LOG
  5. #ifdef JPH_ENABLE_DETERMINISM_LOG
  6. #include <Jolt/Physics/Body/BodyID.h>
  7. #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
  8. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  9. #include <iomanip>
  10. #include <fstream>
  11. JPH_SUPPRESS_WARNINGS_STD_END
  12. JPH_NAMESPACE_BEGIN
  13. /// A simple class that logs the state of the simulation. The resulting text file can be used to diff between platforms and find issues in determinism.
  14. class DeterminismLog
  15. {
  16. private:
  17. JPH_INLINE uint32 Convert(float inValue) const
  18. {
  19. return *(uint32 *)&inValue;
  20. }
  21. public:
  22. DeterminismLog()
  23. {
  24. mLog.open("detlog.txt", std::ios::out | std::ios::trunc | std::ios::binary); // Binary because we don't want a difference between Unix and Windows line endings.
  25. mLog.fill('0');
  26. }
  27. DeterminismLog & operator << (char inValue)
  28. {
  29. mLog << inValue;
  30. return *this;
  31. }
  32. DeterminismLog & operator << (const char *inValue)
  33. {
  34. mLog << std::dec << inValue;
  35. return *this;
  36. }
  37. DeterminismLog & operator << (const string &inValue)
  38. {
  39. mLog << std::dec << inValue;
  40. return *this;
  41. }
  42. DeterminismLog & operator << (const BodyID &inValue)
  43. {
  44. mLog << std::hex << std::setw(8) << inValue.GetIndexAndSequenceNumber();
  45. return *this;
  46. }
  47. DeterminismLog & operator << (const SubShapeID &inValue)
  48. {
  49. mLog << std::hex << std::setw(8) << inValue.GetValue();
  50. return *this;
  51. }
  52. DeterminismLog & operator << (float inValue)
  53. {
  54. mLog << std::hex << std::setw(8) << Convert(inValue);
  55. return *this;
  56. }
  57. DeterminismLog & operator << (int inValue)
  58. {
  59. mLog << inValue;
  60. return *this;
  61. }
  62. DeterminismLog & operator << (uint32 inValue)
  63. {
  64. mLog << std::hex << std::setw(8) << inValue;
  65. return *this;
  66. }
  67. DeterminismLog & operator << (uint64 inValue)
  68. {
  69. mLog << std::hex << std::setw(16) << inValue;
  70. return *this;
  71. }
  72. DeterminismLog & operator << (Vec3Arg inValue)
  73. {
  74. mLog << std::hex << std::setw(8) << Convert(inValue.GetX()) << " " << std::setw(8) << Convert(inValue.GetY()) << " " << std::setw(8) << Convert(inValue.GetZ());
  75. return *this;
  76. }
  77. DeterminismLog & operator << (Vec4Arg inValue)
  78. {
  79. mLog << std::hex << std::setw(8) << Convert(inValue.GetX()) << " " << std::setw(8) << Convert(inValue.GetY()) << " " << std::setw(8) << Convert(inValue.GetZ()) << " " << std::setw(8) << Convert(inValue.GetW());
  80. return *this;
  81. }
  82. DeterminismLog & operator << (const Float3 &inValue)
  83. {
  84. mLog << std::hex << std::setw(8) << Convert(inValue.x) << " " << std::setw(8) << Convert(inValue.y) << " " << std::setw(8) << Convert(inValue.z);
  85. return *this;
  86. }
  87. DeterminismLog & operator << (Mat44Arg inValue)
  88. {
  89. *this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetColumn4(3);
  90. return *this;
  91. }
  92. DeterminismLog & operator << (QuatArg inValue)
  93. {
  94. *this << inValue.GetXYZW();
  95. return *this;
  96. }
  97. // Singleton instance
  98. static DeterminismLog sLog;
  99. private:
  100. std::ofstream mLog;
  101. };
  102. /// Will log something to the determinism log, usage: JPH_DET_LOG("label " << value);
  103. #define JPH_DET_LOG(...) DeterminismLog::sLog << __VA_ARGS__ << '\n'
  104. JPH_NAMESPACE_END
  105. #else
  106. JPH_SUPPRESS_WARNING_PUSH
  107. JPH_SUPPRESS_WARNINGS
  108. /// By default we log nothing
  109. #define JPH_DET_LOG(...)
  110. JPH_SUPPRESS_WARNING_POP
  111. #endif // JPH_ENABLE_DETERMINISM_LOG