Back to Raylib Rs

Callbacks and logging

book/src/modules/callbacks-and-logging.md

6.0.04.0 KB
Original Source

Callbacks and logging

raylib exposes hooks for several behaviours: trace-log output, custom file I/O loaders/savers, and audio stream processing. raylib-rs wraps the trace-log hook as a plain Rust function pointer so you can redirect raylib's diagnostic output to your own logger.

The logging free functions (trace_log, set_trace_log) live in raylib::core::logging and are re-exported through raylib::prelude.

API surface

Example

Install a trace-log callback and emit one message. No window needed to install the callback, but trace_log calls into ffi::TraceLog which does require an active raylib context, so this example is no_run.

rust,no_run
# extern crate raylib;
use raylib::core::callbacks::set_trace_log_callback;
use raylib::core::logging::trace_log;
use raylib::consts::TraceLogLevel;

fn my_logger(level: TraceLogLevel, msg: &str) {
    eprintln!("[{level:?}] {msg}");
}

fn main() {
    // Install the callback before init so we capture startup messages too.
    set_trace_log_callback(my_logger).expect("callback already set");

    let (_rl, _thread) = raylib::init()
        .size(1, 1)
        .title("callback demo")
        .build();

    // Emit a custom message through the same callback.
    trace_log(TraceLogLevel::LOG_INFO, "Hello from the custom logger");
}

Gotchas

  • Callbacks are global state. Installing a new trace-log callback replaces the previous one. set_trace_log_callback always returns Ok(()). The old methods on RaylibHandle (e.g., rl.set_trace_log_callback(...)) are #[deprecated] — use the free function form instead.
  • The audio callback was removed in 6.0. The old per-sample audio callback API is gone entirely. Use AudioStream with is_processed / update_audio_stream for custom PCM generation, or attach a processor via attach_audio_stream_processor_to_music.
  • File I/O callbacks replace raylib's I/O for the lifetime of the process. There is no "unset" for set_load_file_data_callback etc. — they can only be set once per callback slot.

See also

Showcase examples

Showcase examples that exercise this module: