From f6db3a722eec200e88ee93705860be0dad92c0d2 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Thu, 23 Jun 2022 23:24:07 +0200 Subject: [PATCH] Fix issue 257 --- include/reactphysics3d/utils/DefaultLogger.h | 39 +++++++++++++++++--- src/systems/CollisionDetectionSystem.cpp | 4 +- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/include/reactphysics3d/utils/DefaultLogger.h b/include/reactphysics3d/utils/DefaultLogger.h index f4d2e927..3dcebd82 100644 --- a/include/reactphysics3d/utils/DefaultLogger.h +++ b/include/reactphysics3d/utils/DefaultLogger.h @@ -36,6 +36,7 @@ #include #include #include +#include /// ReactPhysics3D namespace namespace reactphysics3d { @@ -80,6 +81,27 @@ class DefaultLogger : public Logger { /// Format a log message virtual std::string format(const time_t& time, const std::string& physicsWorldName, const std::string& message, Level level, Category category, const char* filename, int lineNumber) = 0; + + /// Return the current date and time + std::tm getLocalTime(const std::time_t& time) const { + + std::tm bt = std::tm(); + + // This is because std::localtime is not thread-safe + +#if defined(__unix__) + localtime_r(&time, &bt); +#elif defined(_MSC_VER) + localtime_s(&bt, &time); +#else + static std::mutex mtx; + std::lock_guard lock(mtx); + bt = *std::localtime(&time); +#endif + + return bt; + } + }; class TextFormatter : public Formatter { @@ -101,12 +123,14 @@ class DefaultLogger : public Logger { // Get current date auto now = std::chrono::system_clock::now(); - auto time = std::chrono::system_clock::to_time_t(now); + std::time_t time = std::chrono::system_clock::to_time_t(now); + + auto localTime = getLocalTime(time); std::stringstream ss; ss << "ReactPhysics3D Logs" << std::endl; ss << "ReactPhysics3D Version: " << RP3D_VERSION << std::endl; - ss << "Date: " << std::put_time(std::localtime(&time), "%Y-%m-%d") << std::endl; + ss << "Date: " << std::put_time(&localTime, "%Y-%m-%d") << std::endl; ss << "---------------------------------------------------------" << std::endl; return ss.str(); @@ -117,8 +141,10 @@ class DefaultLogger : public Logger { Level level, Category category, const char* filename, int lineNumber) override { std::stringstream ss; + auto localTime = getLocalTime(time); + // Time - ss << std::put_time(std::localtime(&time), "%X") << " "; + ss << std::put_time(&localTime, "%X") << " "; // World ss << "World:" << physicsWorldName << std::endl; @@ -152,6 +178,7 @@ class DefaultLogger : public Logger { // Get current date auto now = std::chrono::system_clock::now(); auto time = std::chrono::system_clock::to_time_t(now); + auto localTime = getLocalTime(time); std::stringstream ss; ss << "" << std::endl; @@ -164,7 +191,7 @@ class DefaultLogger : public Logger { ss << "

ReactPhysics3D Logs

" << std::endl; ss << "
" << std::endl; ss << "

ReactPhysics3D version: " << RP3D_VERSION << "

" << std::endl; - ss << "

Date: " << std::put_time(std::localtime(&time), "%Y-%m-%d") << "

" << std::endl; + ss << "

Date: " << std::put_time(&localTime, "%Y-%m-%d") << "

" << std::endl; ss << "
" << std::endl; ss << "
"; @@ -275,11 +302,13 @@ class DefaultLogger : public Logger { std::stringstream ss; + auto localTime = getLocalTime(time); + ss << "
"; // Time ss << "
"; - ss << std::put_time(std::localtime(&time), "%X"); + ss << std::put_time(&localTime, "%X"); ss << "
"; // Message diff --git a/src/systems/CollisionDetectionSystem.cpp b/src/systems/CollisionDetectionSystem.cpp index a61b4ee4..8d2f2ec5 100644 --- a/src/systems/CollisionDetectionSystem.cpp +++ b/src/systems/CollisionDetectionSystem.cpp @@ -499,8 +499,8 @@ void CollisionDetectionSystem::computeConvexVsConcaveMiddlePhase(OverlappingPair const bool isCollider2Trigger = mCollidersComponents.mIsTrigger[collider2Index]; reportContacts = reportContacts && !isCollider1Trigger && !isCollider2Trigger; - CollisionShape* shape1; - CollisionShape* shape2; + CollisionShape* shape1 = nullptr; + CollisionShape* shape2 = nullptr; if (overlappingPair.isShape1Convex) { shape1 = convexShape;