Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
DeterminismLog.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2022 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7//#define JPH_ENABLE_DETERMINISM_LOG
8#ifdef JPH_ENABLE_DETERMINISM_LOG
9
12
14#include <iomanip>
15#include <fstream>
17
19
21class DeterminismLog
22{
23private:
24 JPH_INLINE uint32 Convert(float inValue) const
25 {
26 return *(uint32 *)&inValue;
27 }
28
29 JPH_INLINE uint64 Convert(double inValue) const
30 {
31 return *(uint64 *)&inValue;
32 }
33
34public:
36 inline static const char *sBasePath = nullptr;
37
38 void Open()
39 {
40 // Determine file name
41 char file_name[1024] = "detlog.txt";
42 if (sBasePath != nullptr)
43 snprintf(file_name, sizeof(file_name), "%s/detlog.txt", sBasePath);
44 Trace("Opening determinism log file: %s", file_name);
45#ifdef JPH_PLATFORM_ANDROID
46 Trace("Retrieve it using: adb pull %s detlog.txt", file_name);
47#endif
48
49 // Open the file
50 mLog.open(file_name, std::ios::out | std::ios::trunc | std::ios::binary); // Binary because we don't want a difference between Unix and Windows line endings.
51 mLog.fill('0');
52 JPH_ASSERT(mLog.is_open());
53 }
54
55 void Close()
56 {
57 mLog.close();
58 }
59
60 DeterminismLog & operator << (char inValue)
61 {
62 mLog << inValue;
63 return *this;
64 }
65
66 DeterminismLog & operator << (const char *inValue)
67 {
68 mLog << std::dec << inValue;
69 return *this;
70 }
71
72 DeterminismLog & operator << (const String &inValue)
73 {
74 mLog << std::dec << inValue;
75 return *this;
76 }
77
78 DeterminismLog & operator << (const BodyID &inValue)
79 {
80 mLog << std::hex << std::setw(8) << inValue.GetIndexAndSequenceNumber();
81 return *this;
82 }
83
84 DeterminismLog & operator << (const SubShapeID &inValue)
85 {
86 mLog << std::hex << std::setw(8) << inValue.GetValue();
87 return *this;
88 }
89
90 DeterminismLog & operator << (float inValue)
91 {
92 mLog << std::hex << std::setw(8) << Convert(inValue);
93 return *this;
94 }
95
96 DeterminismLog & operator << (int inValue)
97 {
98 mLog << inValue;
99 return *this;
100 }
101
102 DeterminismLog & operator << (uint32 inValue)
103 {
104 mLog << std::hex << std::setw(8) << inValue;
105 return *this;
106 }
107
108 DeterminismLog & operator << (uint64 inValue)
109 {
110 mLog << std::hex << std::setw(16) << inValue;
111 return *this;
112 }
113
114 DeterminismLog & operator << (Vec3Arg inValue)
115 {
116 mLog << std::hex << std::setw(8) << Convert(inValue.GetX()) << " " << std::setw(8) << Convert(inValue.GetY()) << " " << std::setw(8) << Convert(inValue.GetZ());
117 return *this;
118 }
119
120 DeterminismLog & operator << (DVec3Arg inValue)
121 {
122 mLog << std::hex << std::setw(16) << Convert(inValue.GetX()) << " " << std::setw(16) << Convert(inValue.GetY()) << " " << std::setw(16) << Convert(inValue.GetZ());
123 return *this;
124 }
125
126 DeterminismLog & operator << (Vec4Arg inValue)
127 {
128 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());
129 return *this;
130 }
131
132 DeterminismLog & operator << (const Float3 &inValue)
133 {
134 mLog << std::hex << std::setw(8) << Convert(inValue.x) << " " << std::setw(8) << Convert(inValue.y) << " " << std::setw(8) << Convert(inValue.z);
135 return *this;
136 }
137
138 DeterminismLog & operator << (Mat44Arg inValue)
139 {
140 *this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetColumn4(3);
141 return *this;
142 }
143
144 DeterminismLog & operator << (DMat44Arg inValue)
145 {
146 *this << inValue.GetColumn4(0) << " " << inValue.GetColumn4(1) << " " << inValue.GetColumn4(2) << " " << inValue.GetTranslation();
147 return *this;
148 }
149
150 DeterminismLog & operator << (QuatArg inValue)
151 {
152 *this << inValue.GetXYZW();
153 return *this;
154 }
155
156 // Singleton instance
157 static DeterminismLog sLog;
158
159private:
160 std::ofstream mLog;
161};
162
164#define JPH_DET_LOG_OPEN() DeterminismLog::sLog.Open()
165#define JPH_DET_LOG(...) DeterminismLog::sLog << __VA_ARGS__ << '\n'
166#define JPH_DET_LOG_CLOSE() DeterminismLog::sLog.Close()
167
169
170#else
171
174
176#define JPH_DET_LOG_OPEN()
177#define JPH_DET_LOG(...)
178#define JPH_DET_LOG_CLOSE()
179
181
182#endif // JPH_ENABLE_DETERMINISM_LOG
#define JPH_SUPPRESS_WARNINGS
Definition Core.h:344
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:439
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:452
std::uint64_t uint64
Definition Core.h:515
#define JPH_NAMESPACE_END
Definition Core.h:434
std::uint32_t uint32
Definition Core.h:513
#define JPH_NAMESPACE_BEGIN
Definition Core.h:428
TraceFunction Trace
Definition IssueReporting.cpp:14
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
std::basic_string< char, std::char_traits< char >, STLAllocator< char > > String
Definition STLAllocator.h:107
#define JPH_SUPPRESS_WARNING_POP
Definition ShaderCore.h:38
#define JPH_SUPPRESS_WARNING_PUSH
Definition ShaderCore.h:37
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition BodyID.h:13
uint32 GetIndexAndSequenceNumber() const
Returns the index and sequence number combined in an uint32.
Definition BodyID.h:59
Holds a 4x4 matrix of floats with the last column consisting of doubles.
Definition DMat44.h:13
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition DMat44.h:115
JPH_INLINE DVec3 GetTranslation() const
Definition DMat44.h:111
Definition DVec3.h:14
JPH_INLINE double GetZ() const
Definition DVec3.h:152
JPH_INLINE double GetY() const
Definition DVec3.h:151
JPH_INLINE double GetX() const
Get individual components.
Definition DVec3.h:150
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
float y
Definition Float3.h:39
float z
Definition Float3.h:40
float x
Definition Float3.h:38
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition Mat44.h:160
Definition Quat.h:33
JPH_INLINE Vec4 GetXYZW() const
Get the quaternion as a Vec4.
Definition Quat.h:85
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition SubShapeID.h:23
Type GetValue() const
Get the value of the path to the sub shape ID.
Definition SubShapeID.h:52
Definition Vec3.h:17
JPH_INLINE float GetX() const
Get individual components.
Definition Vec3.h:127
JPH_INLINE float GetY() const
Definition Vec3.h:128
JPH_INLINE float GetZ() const
Definition Vec3.h:129
Definition Vec4.h:14
JPH_INLINE float GetW() const
Definition Vec4.h:122
JPH_INLINE float GetX() const
Get individual components.
Definition Vec4.h:119
JPH_INLINE float GetZ() const
Definition Vec4.h:121
JPH_INLINE float GetY() const
Definition Vec4.h:120