Introduce new logging API, fixes #858
PiperOrigin-RevId: 930744288 Change-Id: I6ec1203b55c031390f3eef23192e2337508ce886
This commit is contained in:
committed by
Copybara-Service
parent
a2abaf7aef
commit
58f6d52491
+7
-9
@@ -34,8 +34,7 @@
|
||||
#include <mujoco/mujoco.h>
|
||||
|
||||
extern "C" {
|
||||
MJAPI void _mjPRIVATE__set_tls_error_fn(decltype(mju_user_error));
|
||||
MJAPI decltype(mju_user_error) _mjPRIVATE__get_tls_error_fn();
|
||||
MJAPI mjfLogHandler _mjPRIVATE_setTlsLogHandler(mjfLogHandler handler);
|
||||
}
|
||||
|
||||
namespace mujoco {
|
||||
@@ -138,21 +137,20 @@ auto MjuErrorMessageFrom(Return (*func)(Args...)) {
|
||||
thread_local std::jmp_buf current_jmp_buf;
|
||||
thread_local char err_msg[1000];
|
||||
|
||||
auto* old_error_handler = _mjPRIVATE__get_tls_error_fn();
|
||||
auto* new_error_handler = +[](const char* msg) -> void {
|
||||
std::strncpy(err_msg, msg, sizeof(err_msg));
|
||||
auto new_error_handler = +[](const mjLogMessage* msg) -> void {
|
||||
if (msg->level != mjLOG_ERROR) return;
|
||||
std::strncpy(err_msg, msg->subject, sizeof(err_msg));
|
||||
std::longjmp(current_jmp_buf, 1);
|
||||
};
|
||||
|
||||
return [func, old_error_handler,
|
||||
new_error_handler](Args... args) -> std::string {
|
||||
return [func, new_error_handler](Args... args) -> std::string {
|
||||
auto old_handler = _mjPRIVATE_setTlsLogHandler(new_error_handler);
|
||||
if (setjmp(current_jmp_buf) == 0) {
|
||||
err_msg[0] = '\0';
|
||||
_mjPRIVATE__set_tls_error_fn(new_error_handler);
|
||||
func(args...);
|
||||
}
|
||||
|
||||
_mjPRIVATE__set_tls_error_fn(old_error_handler);
|
||||
_mjPRIVATE_setTlsLogHandler(old_handler);
|
||||
return err_msg;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user