diff --git a/src/main.zig b/src/main.zig index bbcce98..5a319f8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -8,6 +8,11 @@ const windows = @cImport({ @cInclude("windows.h"); }); +const raylib_h = @cImport({ + @cInclude("stdio.h"); + @cInclude("raylib.h"); +}); + const log = std.log; const Allocator = std.mem.Allocator; @@ -15,15 +20,45 @@ const FontFace = @import("font-face.zig"); const assert = std.debug.assert; const Vec2 = rl.Vector2; -fn toTraceLogLevel(log_level: std.log.Level) rl.TraceLogLevel { +fn toRaylibTraceLogLevel(log_level: std.log.Level) rl.TraceLogLevel { return switch (log_level) { .err => rl.TraceLogLevel.log_error, .warn => rl.TraceLogLevel.log_warning, .info => rl.TraceLogLevel.log_info, - .debug => rl.TraceLogLevel.log_all, + .debug => rl.TraceLogLevel.log_trace, }; } +fn toZigLogLevel(log_type: c_int) ?std.log.Level { + return switch (log_type) { + @intFromEnum(rl.TraceLogLevel.log_trace) => std.log.Level.debug, + @intFromEnum(rl.TraceLogLevel.log_debug) => std.log.Level.debug, + @intFromEnum(rl.TraceLogLevel.log_info) => std.log.Level.info, + @intFromEnum(rl.TraceLogLevel.log_warning) => std.log.Level.warn, + @intFromEnum(rl.TraceLogLevel.log_error) => std.log.Level.err, + @intFromEnum(rl.TraceLogLevel.log_fatal) => std.log.Level.err, + else => null + }; +} + +fn raylibTraceLogCallback(logType: c_int, text: [*c]const u8, args: raylib_h.va_list) callconv(.C) void { + const log_level = toZigLogLevel(logType) orelse return; + + // TODO: Skip formatting buffer, if logging is not enabled for that level. + const max_tracelog_msg_length = 256; // from utils.c in raylib + var buffer: [max_tracelog_msg_length:0]u8 = undefined; + @memset(&buffer, 0); + _ = raylib_h.vsnprintf(&buffer, buffer.len, text, args); + + const raylib_log = std.log.scoped(.raylib); + switch (log_level) { + .debug => raylib_log.debug("{s}", .{ buffer }), + .info => raylib_log.info("{s}", .{ buffer }), + .warn => raylib_log.warn("{s}", .{ buffer }), + .err => raylib_log.err("{s}", .{ buffer }) + } +} + fn remap(from_min: f32, from_max: f32, to_min: f32, to_max: f32, value: f32) f32 { const t = (value - from_min) / (from_max - from_min); return std.math.lerp(to_min, to_max, t); @@ -83,8 +118,8 @@ pub fn main() !void { // _ = windows.ShowWindow(hWnd, windows.SW_SHOW); // } - // TODO: SetTraceLogCallback - rl.setTraceLogLevel(toTraceLogLevel(std.log.default_level)); + raylib_h.SetTraceLogCallback(raylibTraceLogCallback); + rl.setTraceLogLevel(toRaylibTraceLogLevel(std.log.default_level)); var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator();