75 lines
2.9 KiB
C
75 lines
2.9 KiB
C
// SPDX-License-Identifier: Apache-2.0
|
|
// C printf-style wrappers for Rust logging FFI
|
|
|
|
#pragma once
|
|
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "rust_backend_ffi.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// Buffer size for formatted messages. Documented truncation behavior.
|
|
#define RUST_LOG_FMT_BUFSZ 2048
|
|
|
|
static inline void rust_log_vprintf_level(RustLogLevel level, const char* fmt, va_list ap) {
|
|
if (fmt == NULL) {
|
|
rust_log_event_with_level("(null format)", level);
|
|
return;
|
|
}
|
|
char buf[RUST_LOG_FMT_BUFSZ];
|
|
int ret = vsnprintf(buf, RUST_LOG_FMT_BUFSZ, fmt, ap);
|
|
if (ret < 0) {
|
|
rust_log_event_with_level("(format error)", level);
|
|
return;
|
|
}
|
|
// vsnprintf guarantees NUL-termination as long as size>0
|
|
buf[RUST_LOG_FMT_BUFSZ - 1] = '\0';
|
|
rust_log_event_with_level(buf, level);
|
|
}
|
|
|
|
static inline void rust_log_printf_level(RustLogLevel level, const char* fmt, ...) {
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
rust_log_vprintf_level(level, fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
|
|
// Convenience per-level functions
|
|
static inline void rust_logf_fatal(const char* fmt, ...) {
|
|
va_list ap; va_start(ap, fmt); rust_log_vprintf_level(RUST_LOG_LEVEL_FATAL, fmt, ap); va_end(ap);
|
|
}
|
|
static inline void rust_logf_error(const char* fmt, ...) {
|
|
va_list ap; va_start(ap, fmt); rust_log_vprintf_level(RUST_LOG_LEVEL_ERROR, fmt, ap); va_end(ap);
|
|
}
|
|
static inline void rust_logf_warn(const char* fmt, ...) {
|
|
va_list ap; va_start(ap, fmt); rust_log_vprintf_level(RUST_LOG_LEVEL_WARN, fmt, ap); va_end(ap);
|
|
}
|
|
static inline void rust_logf_info(const char* fmt, ...) {
|
|
va_list ap; va_start(ap, fmt); rust_log_vprintf_level(RUST_LOG_LEVEL_INFO, fmt, ap); va_end(ap);
|
|
}
|
|
static inline void rust_logf_debug(const char* fmt, ...) {
|
|
va_list ap; va_start(ap, fmt); rust_log_vprintf_level(RUST_LOG_LEVEL_DEBUG, fmt, ap); va_end(ap);
|
|
}
|
|
static inline void rust_logf_trace(const char* fmt, ...) {
|
|
va_list ap; va_start(ap, fmt); rust_log_vprintf_level(RUST_LOG_LEVEL_TRACE, fmt, ap); va_end(ap);
|
|
}
|
|
|
|
// Convenience macros for C/C++ callers (variadic macro)
|
|
// Support both C99 and C++ compilers that support variadic macros
|
|
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus)
|
|
#define RUST_LOGF_FATAL(fmt, ...) rust_log_printf_level(RUST_LOG_LEVEL_FATAL, (fmt), ##__VA_ARGS__)
|
|
#define RUST_LOGF_ERROR(fmt, ...) rust_log_printf_level(RUST_LOG_LEVEL_ERROR, (fmt), ##__VA_ARGS__)
|
|
#define RUST_LOGF_WARN(fmt, ...) rust_log_printf_level(RUST_LOG_LEVEL_WARN, (fmt), ##__VA_ARGS__)
|
|
#define RUST_LOGF_INFO(fmt, ...) rust_log_printf_level(RUST_LOG_LEVEL_INFO, (fmt), ##__VA_ARGS__)
|
|
#define RUST_LOGF_DEBUG(fmt, ...) rust_log_printf_level(RUST_LOG_LEVEL_DEBUG, (fmt), ##__VA_ARGS__)
|
|
#define RUST_LOGF_TRACE(fmt, ...) rust_log_printf_level(RUST_LOG_LEVEL_TRACE, (fmt), ##__VA_ARGS__)
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|