DeterminismLog.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. //#define JPH_ENABLE_DETERMINISM_LOG
  6. #ifdef JPH_ENABLE_DETERMINISM_LOG
  7. #include <Jolt/Physics/Body/BodyID.h>
  8. #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
  9. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  10. #include <iomanip>
  11. #include <fstream>
  12. JPH_SUPPRESS_WARNINGS_STD_END
  13. JPH_NAMESPACE_BEGIN
  14. /// 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.
  15. class DeterminismLog
  16. {
  17. private:
  18. JPH_INLINE uint32 Convert(float inValue) const
  19. {
  20. return *(uint32 *)&inValue;
  21. }
  22. JPH_INLINE uint64 Convert(double inValue) const
  23. {
  24. return *(uint64 *)&inValue;
  25. }
  26. public:
  27. DeterminismLog()
  28. {
  29. 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.
  30. mLog.fill('0');
  31. }
  32. DeterminismLog & operator << (char inValue)
  33. {
  34. mLog << inValue;
  35. return *this;
  36. }
  37. DeterminismLog & operator << (const char *inValue)
  38. {
  39. mLog << std::dec << inValue;
  40. return *this;
  41. }
  42. DeterminismLog & operator << (const string &inValue)
  43. {
  44. mLog << std::dec << inValue;
  45. return *this;
  46. }
  47. DeterminismLog & operator << (const BodyID &inValue)
  48. {
  49. mLog << std::hex << std::setw(8) << inValue.GetIndexAndSequenceNumber();
  50. return *this;
  51. }
  52. DeterminismLog & operator << (const SubShapeID &inValue)
  53. {
  54. mLog << std::hex << std::setw(8) << inValue.GetValue();
  55. return *this;
  56. }
  57. DeterminismLog & operator << (float inValue)
  58. {
  59. mLog << std::hex << std::setw(8) << Convert(inValue);
  60. return *this;
  61. }
  62. DeterminismLog & operator << (int inValue)
  63. {
  64. mLog << inValue;
  65. return *this;
  66. }
  67. DeterminismLog & operator << (uint32 inValue)
  68. {
  69. mLog << std::hex << std::setw(8) << inValue;
  70. return *this;
  71. }
  72. DeterminismLog & operator << (uint64 inValue)
  73. {
  74. mLog << std::hex << std::setw(16) << inValue;
  75. return *this;
  76. }
  77. DeterminismLog & operator << (Vec3Arg inValue)
  78. {
  79. mLog << std::hex << std::setw(8) << Convert(inValue.GetX()) << " " << std::setw(8) << Convert(inValue.GetY()) << " " << std::setw(8) << Convert(inValue.GetZ());
  80. return *this;
  81. }
  82. DeterminismLog & operator << (DVec3Arg inValue)
  83. {
  84. mLog << std::hex << std::setw(16) << Convert(inValue.GetX()) << " " << std::setw(16) << Convert(inValue.GetY()) << " " << std::setw(16) << Convert(inValue.GetZ());
  85. return *this;
  86. }
  87. DeterminismLog & operator << (Vec4Arg inValue)
  88. {
  89. 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());
  90. return *this;
  91. }
  92. DeterminismLog & operator << (const Float3 &inValue)
  93. {
  94. mLog << std::hex << std::setw(8) << Convert(inValue.x) << " " << std::setw(8) << Convert(inValue.y) << " " << std::setw(8) << Convert(inValue.z);
  95. return *this;
  96. }
  97. DeterminismLog & operator << (Mat44Arg inValue)
  98. {
  99. *this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetColumn4(3);
  100. return *this;
  101. }
  102. DeterminismLog & operator << (DMat44Arg inValue)
  103. {
  104. *this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetTranslation();
  105. return *this;
  106. }
  107. DeterminismLog & operator << (QuatArg inValue)
  108. {
  109. *this << inValue.GetXYZW();
  110. return *this;
  111. }
  112. // Singleton instance
  113. static DeterminismLog sLog;
  114. private:
  115. std::ofstream mLog;
  116. };
  117. /// Will log something to the determinism log, usage: JPH_DET_LOG("label " << value);
  118. #define JPH_DET_LOG(...) DeterminismLog::sLog << __VA_ARGS__ << '\n'
  119. JPH_NAMESPACE_END
  120. #else
  121. JPH_SUPPRESS_WARNING_PUSH
  122. JPH_SUPPRESS_WARNINGS
  123. /// By default we log nothing
  124. #define JPH_DET_LOG(...)
  125. JPH_SUPPRESS_WARNING_POP
  126. #endif // JPH_ENABLE_DETERMINISM_LOG