Skip to content

Commit

Permalink
Merge pull request #59 from laytan/add-raylib-logger-example
Browse files Browse the repository at this point in the history
add Raylib logger integration example
  • Loading branch information
laytan authored Oct 1, 2024
2 parents 4969cad + 3864ec5 commit d3b4b8f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
odin check math/noise/draw_texture $FLAGS
odin check raylib/game_of_life $FLAGS
odin check raylib/log $FLAGS
odin check raylib/microui $FLAGS
odin check raylib/ports/shaders/shaders_mesh_instancing.odin -file $FLAGS
odin check raylib/ports/shapes/shapes_basic_shapes.odin -file $FLAGS
Expand Down
58 changes: 58 additions & 0 deletions raylib/log/main.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package raylib_example_log

import "base:runtime"

import "core:log"
import "core:c"

import rl "vendor:raylib"
import stbsp "vendor:stb/sprintf"

SCREEN_WIDTH :: 800
SCREEN_HEIGHT :: 450

g_ctx: runtime.Context

main :: proc() {
context.logger = log.create_console_logger(.Debug)
g_ctx = context

rl.SetTraceLogLevel(.ALL)
rl.SetTraceLogCallback(proc "c" (rl_level: rl.TraceLogLevel, message: cstring, args: ^c.va_list) {
context = g_ctx

level: log.Level
switch rl_level {
case .TRACE, .DEBUG: level = .Debug
case .INFO: level = .Info
case .WARNING: level = .Warning
case .ERROR: level = .Error
case .FATAL: level = .Fatal
case .ALL, .NONE: fallthrough
case: log.panicf("unexpected log level %v", rl_level)
}

@static buf: [dynamic]byte
log_len: i32
for {
buf_len := i32(len(buf))
log_len = stbsp.vsnprintf(raw_data(buf), buf_len, message, args)
if log_len <= buf_len {
break
}

non_zero_resize(&buf, max(128, len(buf)*2))
}

context.logger.procedure(context.logger.data, level, string(buf[:log_len]), context.logger.options)
})

rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib log callback")
defer rl.CloseWindow()

for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RAYWHITE)
rl.EndDrawing()
}
}

0 comments on commit d3b4b8f

Please sign in to comment.