Merge pull request #110 from juan-medina/master

adding SetTraceLogCallback
This commit is contained in:
Milan Nikolic 2020-08-31 11:24:59 +02:00 committed by GitHub
commit f2de21371c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 0 deletions

46
examples/utils/utils.go Normal file
View file

@ -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()
}

18
raylib/utils_log.c Normal file
View file

@ -0,0 +1,18 @@
#include "raylib.h"
#include "utils_log.h"
#include <stdio.h> // Required for: vprintf()
#include <string.h> // 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);
}

26
raylib/utils_log.go Normal file
View file

@ -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)
}

11
raylib/utils_log.h Normal file
View file

@ -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