the log wrapper in C/C++ accepts formats

This commit is contained in:
2026-03-17 13:22:44 +11:00
parent 2b3fe0630f
commit bdbd94eda2
9 changed files with 259 additions and 7 deletions

View File

@@ -44,3 +44,9 @@ void rust_log_event_with_level(const char* event, RustLogLevel level);
#define RUST_LOG_DEBUG(msg) rust_log_event_with_level((msg), RUST_LOG_LEVEL_DEBUG)
#define RUST_LOG_TRACE(msg) rust_log_event_with_level((msg), RUST_LOG_LEVEL_TRACE)
// C wrapper: printf-style helpers (header-only)
#include "rust_backend_logging_c.h"
#ifdef __cplusplus
#include "rust_backend_logging_cpp.h"
#endif

View File

@@ -0,0 +1,74 @@
// 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

View File

@@ -0,0 +1,75 @@
// SPDX-License-Identifier: Apache-2.0
// C++ stream-style wrappers for Rust logging FFI
#pragma once
// This header expects `rust_backend_ffi.h` to have been included first
// so that `RustLogLevel` and `rust_log_event_with_level` are available.
#include <sstream>
#include <string>
#include <ostream>
class RustLogStream {
public:
explicit RustLogStream(RustLogLevel level) noexcept : level_(level) {}
// Generic stream insertion
template<typename T>
RustLogStream& operator<<(const T& v) {
try {
oss_ << v;
} catch (...) {
// swallow; avoid exceptions crossing FFI
}
return *this;
}
// Support manipulators like std::endl
RustLogStream& operator<<(std::ostream& (*manip)(std::ostream&)) {
try {
manip(oss_);
} catch (...) {
}
return *this;
}
// Destructor emits buffered message
~RustLogStream() noexcept {
try {
const std::string s = oss_.str();
if (!s.empty()) {
rust_log_event_with_level(s.c_str(), level_);
}
} catch (...) {
// never throw
}
}
// Non-copyable
RustLogStream(const RustLogStream&) = delete;
RustLogStream& operator=(const RustLogStream&) = delete;
// Movable
RustLogStream(RustLogStream&&) = default;
RustLogStream& operator=(RustLogStream&&) = default;
private:
RustLogLevel level_;
std::ostringstream oss_;
};
// Factories and macros
inline RustLogStream rust_log_info_stream() { return RustLogStream(RUST_LOG_LEVEL_INFO); }
inline RustLogStream rust_log_warn_stream() { return RustLogStream(RUST_LOG_LEVEL_WARN); }
inline RustLogStream rust_log_error_stream() { return RustLogStream(RUST_LOG_LEVEL_ERROR); }
inline RustLogStream rust_log_debug_stream() { return RustLogStream(RUST_LOG_LEVEL_DEBUG); }
inline RustLogStream rust_log_trace_stream() { return RustLogStream(RUST_LOG_LEVEL_TRACE); }
inline RustLogStream rust_log_fatal_stream() { return RustLogStream(RUST_LOG_LEVEL_FATAL); }
#define RUST_COUT() rust_log_info_stream()
#define RUST_CERR() rust_log_error_stream()
#define RUST_CDEBUG() rust_log_debug_stream()
// Usage example:
// RUST_COUT() << "User=" << user << " logged in" << std::endl;