Introduce new logging API, fixes #858

PiperOrigin-RevId: 930744288
Change-Id: I6ec1203b55c031390f3eef23192e2337508ce886
This commit is contained in:
Yuval Tassa
2026-06-11 14:36:17 -07:00
committed by Copybara-Service
parent a2abaf7aef
commit 58f6d52491
45 changed files with 2586 additions and 422 deletions
+268 -114
View File
@@ -18,17 +18,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif
#include "engine/engine_array_safety.h"
#include "engine/engine_crossplatform.h" // IWYU pragma: keep
#include "engine/engine_macro.h"
//------------------------- cross-platform aligned malloc/free -------------------------------------
// forward declaration for active log handler dispatch
static void mju_defaultLogHandler(const mjLogMessage* msg);
//------------------------------ malloc and free ---------------------------------------------------
// user memory handlers
void* (*mju_user_malloc) (size_t) = 0;
void (*mju_user_free) (void*) = 0;
// cross-platform aligned malloc
static inline void* mju_alignedMalloc(size_t size, size_t align) {
#ifdef _WIN32
return _aligned_malloc(size, align);
@@ -37,6 +49,7 @@ static inline void* mju_alignedMalloc(size_t size, size_t align) {
#endif
}
// cross-platform aligned free
static inline void mju_alignedFree(void* ptr) {
#ifdef _WIN32
_aligned_free(ptr);
@@ -45,98 +58,262 @@ static inline void mju_alignedFree(void* ptr) {
#endif
}
// allocate memory; byte-align on 64; pad size to multiple of 64
void* mju_malloc(size_t size) {
void* ptr = 0;
//------------------------- default user handlers --------------------------------------------------
if (mju_user_malloc) {
ptr = mju_user_malloc(size);
} else {
if (size > 0 && (size % 64)) {
size += 64 - (size % 64);
}
if (size > 0) {
ptr = mju_alignedMalloc(size, 64);
}
}
// define and clear handlers
if (!ptr && size > 0) {
mju_error("Could not allocate memory");
}
return ptr;
}
// free memory
void mju_free(void* ptr) {
if (!ptr) return;
if (mju_user_free) {
mju_user_free(ptr);
} else {
mju_alignedFree(ptr);
}
}
//------------------------------ logging configuration and handlers --------------------------------
// global log handler
static mjfLogHandler global_log_handler = mju_defaultLogHandler;
// legacy error/warning handlers (deprecated)
void (*mju_user_error) (const char*) = 0;
void (*mju_user_warning) (const char*) = 0;
void* (*mju_user_malloc) (size_t) = 0;
void (*mju_user_free) (void*) = 0;
// default handler configuration
static mjLogConfig log_config = {.logto_console = true,
.logto_file = true,
.logfile = "MUJOCO_LOG.TXT",
.topics = 0};
static mjtBool env_checked = 0;
// parse MUJOCO_LOG_TOPICS env var to seed initial topic bitmask
// example: MUJOCO_LOG_TOPICS="time_stp,sleep"
static void mju_initLogTopicsFromEnv(void) {
const char* env = getenv("MUJOCO_LOG_TOPICS");
if (!env) return;
// mjTOPIC_X enum names with the mjTOPIC_ prefix stripped and lowercased; keep in sync with mjtLogTopic
static const char* topic_names[mjNTOPIC] = {"time_stp", "time_cmp", "sleep"};
char buf[256];
strncpy(buf, env, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
char* token = buf;
while (*token) {
while (*token == ' ' || *token == ',') token++;
if (!*token) break;
char* end = token;
while (*end && *end != ',') end++;
bool trailing_comma = (*end == ',');
char* tEnd = end - 1;
while (tEnd > token && *tEnd == ' ') tEnd--;
*(tEnd + 1) = '\0';
for (int i = 0; i < mjNTOPIC; i++) {
if (strcasecmp(token, topic_names[i]) == 0) {
log_config.topics |= (1 << i);
break;
}
}
token = trailing_comma ? end + 1 : end;
}
}
// private pointer getter encapsulates lazy init with zero copy overhead
static const mjLogConfig* mju_getLogConfigPtr(void) {
if (!env_checked) {
mju_initLogTopicsFromEnv();
env_checked = 1;
}
return &log_config;
}
// check whether an info topic is enabled
mjtBool mju_isTopicEnabled(int topic) {
if (!topic) return 1;
const mjLogConfig* cfg = mju_getLogConfigPtr();
return ((cfg->topics & (1 << (topic - 1))) != 0);
}
// set the active log handler; return the previous handler
mjfLogHandler mju_setLogHandler(mjfLogHandler handler) {
mjfLogHandler prev = global_log_handler;
global_log_handler = handler ? handler : mju_defaultLogHandler;
return prev;
}
// get default handler configuration
mjLogConfig mju_getLogConfig(void) {
return *mju_getLogConfigPtr();
}
// set default handler configuration
void mju_setLogConfig(mjLogConfig config) {
env_checked = 1;
log_config = config;
}
// restore default processing
void mju_clearHandlers(void) {
global_log_handler = mju_defaultLogHandler;
log_config = (mjLogConfig){.logto_console = true,
.logto_file = true,
.logfile = "MUJOCO_LOG.TXT",
.topics = 0};
env_checked = 1;
mju_initLogTopicsFromEnv();
mju_user_error = 0;
mju_user_warning = 0;
mju_user_malloc = 0;
mju_user_free = 0;
}
//------------------------- internal-only handlers -------------------------------------------------
typedef void (*callback_fn)(const char*);
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_error_fn = NULL;
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_warning_fn = NULL;
callback_fn _mjPRIVATE__get_tls_error_fn(void) {
return _mjPRIVATE_tls_error_fn;
}
void _mjPRIVATE__set_tls_error_fn(callback_fn h) {
_mjPRIVATE_tls_error_fn = h;
}
callback_fn _mjPRIVATE__get_tls_warning_fn(void) {
return _mjPRIVATE_tls_warning_fn;
}
void _mjPRIVATE__set_tls_warning_fn(callback_fn h) {
_mjPRIVATE_tls_warning_fn = h;
}
//------------------------------ error hadling -----------------------------------------------------
// write datetime, type: message to MUJOCO_LOG.TXT
void mju_writeLog(const char* type, const char* msg) {
// fill buffer with formatted local time string (thread-safe)
static void mju_localTimeStr(char* buf, int buf_sz) {
time_t rawtime;
struct tm timeinfo;
FILE* fp = fopen("MUJOCO_LOG.TXT", "a+t");
if (fp) {
// get time
time(&rawtime);
time(&rawtime);
#if defined(_POSIX_C_SOURCE) || defined(__APPLE__) || defined(__STDC_VERSION_TIME_H__) || defined(__EMSCRIPTEN__)
localtime_r(&rawtime, &timeinfo);
localtime_r(&rawtime, &timeinfo);
#elif defined(_WIN32)
localtime_s(&timeinfo, &rawtime);
localtime_s(&timeinfo, &rawtime);
#elif __STDC_LIB_EXT1__
localtime_s(&rawtime, &timeinfo);
localtime_s(&rawtime, &timeinfo);
#else
#error "Thread-safe version of `localtime` is not present in the standard C library"
#error "Thread-safe version of `localtime` is not present in the standard C library"
#endif
// write to log file
fprintf(fp, "%s%s: %s\n\n", asctime(&timeinfo), type, msg);
fclose(fp);
}
strftime(buf, buf_sz, "%c", &timeinfo);
}
// write formatted message to stream
static void mju_fprint_message(FILE* stream, const char* timestr,
const mjLogMessage* msg) {
const char* type = msg->level == mjLOG_ERROR ? "ERROR" :
msg->level == mjLOG_WARNING ? "WARNING" :
msg->level == mjLOG_INFO ? "INFO" : "DEBUG";
fprintf(stream, "%s", type);
if (msg->func) fprintf(stream, " %s", msg->func);
if (msg->file && msg->line) fprintf(stream, " (%s:%d)", BaseName(msg->file), msg->line);
if (timestr[0]) fprintf(stream, " %s", timestr);
fprintf(stream, ": %s\n", msg->subject);
if (msg->body) fprintf(stream, "%s\n", msg->body);
void mju_error_raw(const char* msg) {
if (_mjPRIVATE_tls_error_fn) {
_mjPRIVATE_tls_error_fn(msg);
} else if (mju_user_error) {
mju_user_error(msg);
} else {
// write to log and console
mju_writeLog("ERROR", msg);
printf("ERROR: %s\n\n", msg);
// add blank line after message except for DEBUG, for compactness
if (msg->level != mjLOG_DEBUG) fprintf(stream, "\n");
}
// exit
// format legacy adapter string: "func: subject" or just "subject"
static const char* mju_legacy_text(const mjLogMessage* msg, char* buf, int bufsz) {
if (msg->func) {
snprintf(buf, bufsz, "%s: %s", msg->func, msg->subject);
return buf;
}
return msg->subject;
}
// default log handler: topic filtering, console/file output, legacy compat, exit on error
static void mju_defaultLogHandler(const mjLogMessage* msg) {
const mjLogConfig* cfg = mju_getLogConfigPtr();
if ((msg->level == mjLOG_INFO || msg->level == mjLOG_DEBUG) && !mju_isTopicEnabled(msg->topic)) {
return;
}
if (msg->level == mjLOG_ERROR && mju_user_error) {
char buf[1024];
mju_user_error(mju_legacy_text(msg, buf, sizeof(buf)));
return;
}
if (msg->level == mjLOG_WARNING && mju_user_warning) {
char buf[1024];
mju_user_warning(mju_legacy_text(msg, buf, sizeof(buf)));
return;
}
char timestr[64] = "";
if (msg->timestamp || (cfg->logto_file && cfg->logfile[0])) {
mju_localTimeStr(timestr, sizeof(timestr));
}
if (cfg->logto_file && cfg->logfile[0]) {
FILE* fp = fopen(cfg->logfile, "a+t");
if (fp) {
mju_fprint_message(fp, timestr, msg);
fclose(fp);
}
}
if (cfg->logto_console) {
FILE* stream = (msg->level >= mjLOG_WARNING) ? stderr : stdout;
mju_fprint_message(stream, msg->timestamp ? timestr : "", msg);
}
if (msg->level == mjLOG_ERROR) {
exit(EXIT_FAILURE);
}
}
void mju_error_v(const char* msg, va_list args) {
// Format msg into errmsg
char errmsg[1024];
vsnprintf(errmsg, mjSIZEOFARRAY(errmsg), msg, args);
mju_error_raw(errmsg);
//------------------------------ public message logging --------------------------------------------
// thread-local log handler override
static mjTHREADLOCAL mjfLogHandler _mjPRIVATE_tls_log_handler = NULL;
// recursion guard for log handler
static mjTHREADLOCAL bool in_log = false;
// dispatch to active handler (TLS > global)
static inline mjfLogHandler mju_activeHandler(void) {
return _mjPRIVATE_tls_log_handler ? _mjPRIVATE_tls_log_handler : global_log_handler;
}
// dispatch a structured log message to the active handler
void mju_message(const mjLogMessage* msg) {
// recursion guard: silently drop messages dispatched from within a handler
if (in_log) return;
// error handlers are expected to not return (longjmp or exit); we cannot set in_log
// around the call because longjmp would leave it permanently true on this thread
if (msg->level != mjLOG_ERROR) {
in_log = true;
mju_activeHandler()(msg);
in_log = false;
} else {
mju_activeHandler()(msg);
}
}
void mju_error_v(const char* msg, va_list args) {
mjLogMessage m = {.level = mjLOG_ERROR};
vsnprintf(m.subject, sizeof(m.subject), msg, args);
mju_message(&m);
}
// write message to logfile and console, pause and exit
void mju_error(const char* msg, ...) {
@@ -146,73 +323,50 @@ void mju_error(const char* msg, ...) {
va_end(args);
}
// write message to logfile and console
void mju_warning(const char* msg, ...) {
char wrnmsg[1024];
// Format msg into wrnmsg
mjLogMessage m = {.level = mjLOG_WARNING};
va_list args;
va_start(args, msg);
vsnprintf(wrnmsg, mjSIZEOFARRAY(wrnmsg), msg, args);
vsnprintf(m.subject, sizeof(m.subject), msg, args);
va_end(args);
mju_message(&m);
}
if (_mjPRIVATE_tls_warning_fn) {
_mjPRIVATE_tls_warning_fn(wrnmsg);
} else if (mju_user_warning) {
mju_user_warning(wrnmsg);
} else {
// write to log file and console
mju_writeLog("WARNING", wrnmsg);
printf("WARNING: %s\n\n", wrnmsg);
// log an info message
void mju_info(int topic, const char* msg, ...) {
mjLogMessage m = {.level = mjLOG_INFO, .topic = topic};
va_list args;
va_start(args, msg);
vsnprintf(m.subject, sizeof(m.subject), msg, args);
va_end(args);
mju_message(&m);
}
// (deprecated) write datetime, type: message to MUJOCO_LOG.TXT
void mju_writeLog(const char* type, const char* msg) {
char timestr[64];
mju_localTimeStr(timestr, sizeof(timestr));
FILE* fp = fopen("MUJOCO_LOG.TXT", "a+t");
if (fp) {
fprintf(fp, "%s\n%s: %s\n\n", timestr, type, msg);
fclose(fp);
}
}
//------------------------------ malloc and free ---------------------------------------------------
//------------------------------ internal helpers --------------------------------------------------
// allocate memory; byte-align on 64; pad size to multiple of 64
void* mju_malloc(size_t size) {
void* ptr = 0;
// user allocator
if (mju_user_malloc) {
ptr = mju_user_malloc(size);
}
// default allocator
else {
// pad size to multiple of 64
if (size > 0 && (size % 64)) {
size += 64 - (size % 64);
}
// allocate
if (size > 0) {
ptr = mju_alignedMalloc(size, 64);
}
}
// error if null pointer
if (!ptr && size > 0) {
mju_error("Could not allocate memory");
}
return ptr;
// set thread-local log handler; return previous
mjfLogHandler _mjPRIVATE_setTlsLogHandler(mjfLogHandler handler) {
mjfLogHandler prev = _mjPRIVATE_tls_log_handler;
_mjPRIVATE_tls_log_handler = handler;
return prev;
}
// free memory
void mju_free(void* ptr) {
// return if null
if (!ptr) {
return;
}
// free with user or built-in function
if (mju_user_free) {
mju_user_free(ptr);
} else {
mju_alignedFree(ptr);
}
// get the currently active global log handler (read-only, no modification)
mjfLogHandler _mjPRIVATE_getGlobalLogHandler(void) {
return global_log_handler;
}