diff --git a/examples/utils/utils.go b/examples/utils/utils.go new file mode 100644 index 0000000..1dd5581 --- /dev/null +++ b/examples/utils/utils.go @@ -0,0 +1,46 @@ +package main + +import ( + "github.com/gen2brain/raylib-go/raylib" + "log" +) + +func main() { + rl.SetTraceLogCallback(func(logType int, str string) { + level := "" + switch logType { + case rl.LogDebug: + level = "Debug" + case rl.LogError: + level = "Error" + case rl.LogInfo: + level = "Info" + case rl.LogTrace: + level = "Trace" + case rl.LogWarning: + level = "Warning" + case rl.LogFatal: + level = "Fatal" + } + if logType != rl.LogFatal { + log.Printf("%s - %s", level, str) + } else { + log.Fatalf("%s - %s", level, str) + } + }) + + rl.SetConfigFlags(rl.FlagVsyncHint) + rl.InitWindow(800, 450, "raylib [utils] example - SetTraceLogCallback") + + for !rl.WindowShouldClose() { + rl.BeginDrawing() + + rl.ClearBackground(rl.RayWhite) + + rl.DrawText("The raylib trace log is controlled in GO!", 190, 200, 20, rl.LightGray) + + rl.EndDrawing() + } + + rl.CloseWindow() +} diff --git a/raylib/utils_log.c b/raylib/utils_log.c new file mode 100644 index 0000000..212a4ae --- /dev/null +++ b/raylib/utils_log.c @@ -0,0 +1,18 @@ +#include "raylib.h" +#include "utils_log.h" +#include // Required for: vprintf() +#include // Required for: strcpy(), strcat() + +#define MAX_TRACELOG_BUFFER_SIZE 128 // As defined in utils.c from raylib + +void rayLogWrapperCallback(int logType, const char *text, va_list args) { + char buffer[MAX_TRACELOG_BUFFER_SIZE] = { 0 }; + + vsprintf(buffer, text, args); + + internalTraceLogCallbackGo(logType, buffer, strlen(buffer)); +} + +void setLogCallbackWrapper(void) { + SetTraceLogCallback(rayLogWrapperCallback); +} diff --git a/raylib/utils_log.go b/raylib/utils_log.go new file mode 100644 index 0000000..d116ae2 --- /dev/null +++ b/raylib/utils_log.go @@ -0,0 +1,26 @@ +package rl + +/* +#include "utils_log.h" +*/ +import "C" + +import "unsafe" + +// TraceLogCallbackFun - function that will recive the trace log messages +type TraceLogCallbackFun func(int, string) + +var internalTraceLogCallbackFun TraceLogCallbackFun = func(int, string) {} + +// SetTraceLogCallback - set a call-back function for trace log +func SetTraceLogCallback(fn TraceLogCallbackFun) { + internalTraceLogCallbackFun = fn + C.setLogCallbackWrapper() +} + +//export internalTraceLogCallbackGo +func internalTraceLogCallbackGo(logType C.int, cstr unsafe.Pointer, len C.int) { + str := string(C.GoBytes(cstr, len)) + lt := int(logType) + internalTraceLogCallbackFun(lt, str) +} diff --git a/raylib/utils_log.h b/raylib/utils_log.h new file mode 100644 index 0000000..8842e4b --- /dev/null +++ b/raylib/utils_log.h @@ -0,0 +1,11 @@ +#if defined(__cplusplus) +extern "C" { // Prevents name mangling of functions +#endif + + +void setLogCallbackWrapper(void); // enable the call-back +void internalTraceLogCallbackGo(int, void*, int); // Go function that will get called + +#if defined(__cplusplus) +} +#endif