diff --git a/src/os_api.zig b/src/os_api.zig index 59ee330..e6667a8 100644 --- a/src/os_api.zig +++ b/src/os_api.zig @@ -12,13 +12,37 @@ pub fn setup(allocator_: Allocator) void { allocator = allocator_; c.ecs_os_set_api_defaults(); - var os_api = c.ecs_os_api; - os_api.malloc_ = flecsMalloc; - os_api.realloc_ = flecsRealloc; - os_api.calloc_ = flecsCalloc; - os_api.free_ = flecsFree; - c.ecs_os_set_api(&os_api); - _ = c.ecs_log_set_level(-1); // No tracing. + + c.ecs_os_api.log_ = flecsLog; + c.ecs_os_api.abort_ = flecsAbort; + + c.ecs_os_api.malloc_ = flecsMalloc; + c.ecs_os_api.realloc_ = flecsRealloc; + c.ecs_os_api.calloc_ = flecsCalloc; + c.ecs_os_api.free_ = flecsFree; + + _ = c.ecs_log_set_level(-2); +} + +// From the looks of it, Flecs does not log errors to stderr +// by default, so let's just use our own logging function. +fn flecsLog(level: i32, file: [*c]const u8, line: i32, msg: [*c]const u8) callconv(.C) void { + const log = std.log.scoped(.Flecs); + const fmt = "{s} at {s}:{d}\n{s}"; + switch (level) { + -4 => log.err(fmt, .{ "Fatal", file, line, msg }), + -3 => log.err(fmt, .{ "Error", file, line, msg }), + -2 => log.warn(fmt, .{ "Warning", file, line, msg }), + -1 => std.debug.panic("Attempting to use unused log level -1", .{}), + 0 => log.debug(fmt, .{ "Tracing", file, line, msg }), + else => log.debug(fmt, .{ "Debug tracing", file, line, msg }), + } +} + +fn flecsAbort() callconv(.C) void { + std.debug.dumpCurrentStackTrace(@returnAddress()); + @breakpoint(); + std.posix.exit(1); } fn flecsMalloc(size: i32) callconv(.C) ?*anyopaque {