From 7d87bb625c45f885a652e5e1438b8ac2fb5d3d5f Mon Sep 17 00:00:00 2001 From: juan-g-bonilla Date: Thu, 19 Jun 2025 23:14:19 -0700 Subject: [PATCH 1/2] Set MuJoCo warning and error callback --- .../mujocoDynamics/_GeneralModuleFiles/MJScene.cpp | 4 ++++ .../mujocoDynamics/_GeneralModuleFiles/MJUtils.cpp | 14 ++++++++++++++ .../mujocoDynamics/_GeneralModuleFiles/MJUtils.h | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJScene.cpp b/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJScene.cpp index 5c9883df2a..9fd25949cf 100644 --- a/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJScene.cpp +++ b/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJScene.cpp @@ -38,6 +38,10 @@ MJScene::MJScene(std::string xml, const std::vector& files) { this->AddFwdKinematicsToDynamicsTask(MJScene::FWD_KINEMATICS_PRIORITY); this->integrator = new svIntegratorRK4(this); + + // Replace default MuJoCo error/warning handling with our own + mju_user_error = MJBasilisk::detail::logMujocoError; + mju_user_warning = MJBasilisk::detail::logMujocoWarning; } MJScene MJScene::fromFile(const std::string& fileName) diff --git a/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.cpp b/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.cpp index 5b1e06726b..f1a77a5979 100644 --- a/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.cpp +++ b/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.cpp @@ -19,6 +19,20 @@ #include "MJUtils.h" +using namespace std::string_literals; + namespace MJBasilisk::detail { +void +logMujocoError(const char* err) +{ + logAndThrow("MuJoCo internal error: "s + err); +} + +void +logMujocoWarning(const char* err) +{ + BSKLogger{}.bskLog(BSK_WARNING, ("MuJoCo internal warning: "s + err).c_str()); +} + } // namespace MJBasilisk::detail diff --git a/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.h b/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.h index a4c015829f..8db662b9c5 100644 --- a/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.h +++ b/src/simulation/mujocoDynamics/_GeneralModuleFiles/MJUtils.h @@ -64,6 +64,18 @@ template throw T(error); } +/** Calls ``logAndThrow`` with the given input + * + * Meant to be used as an error callback for MuJoCo's ``mju_user_error``. +*/ +void logMujocoError(const char* err); + +/** Calls ``BSKLogger::bskLog`` with the given input + * + * Meant to be used as an error callback for MuJoCo's ``mju_user_warning``. +*/ +void logMujocoWarning(const char* err); + } // namespace MJBasilisk::detail #endif From 834904a5dd2f802c0af2853f141738f6a07b15cf Mon Sep 17 00:00:00 2001 From: juan-g-bonilla Date: Thu, 19 Jun 2025 23:15:06 -0700 Subject: [PATCH 2/2] update release notes --- docs/source/Support/bskReleaseNotes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/Support/bskReleaseNotes.rst b/docs/source/Support/bskReleaseNotes.rst index ec6dba363c..1c48aaf2a4 100644 --- a/docs/source/Support/bskReleaseNotes.rst +++ b/docs/source/Support/bskReleaseNotes.rst @@ -54,6 +54,7 @@ Version |release| - Improve reading speed of recorded messages by about 75%. - Added support for Vizard 2.3.0 +- Redirected MuJoCo errors and warnings to :ref:`bskLogging` instead of printing to file. Version 2.7.0 (April 20, 2025)