From ced85f0e5360b66af87b61988afe6784df2df50f Mon Sep 17 00:00:00 2001 From: Nick Koirala Date: Thu, 6 Nov 2025 11:01:04 +1300 Subject: [PATCH] feat: use log.c library in raylib --- .gitignore | 3 +- CMakeLists.txt | 2 + include/Application.h | 1 + scripts/test.as | 2 +- src/Application.cpp | 17 +++- src/log/log.c | 208 ++++++++++++++++++++++++++++++++++++++++++ src/log/log.h | 59 ++++++++++++ 7 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 src/log/log.c create mode 100644 src/log/log.h diff --git a/.gitignore b/.gitignore index d163863..27ba0d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build/ \ No newline at end of file +build/ +log.txt \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 0fa76cd..d60dd8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,10 +34,12 @@ add_executable(simian src/ScriptEngine.cpp src/ScriptBindings.cpp src/HotReload.cpp + src/log/log.c ) target_include_directories(simian PUBLIC include + src/log external/raylib/src external/angelscript/sdk/angelscript/include external/angelscript/sdk/add_on/scriptstdstring diff --git a/include/Application.h b/include/Application.h index c50413f..0a25659 100644 --- a/include/Application.h +++ b/include/Application.h @@ -15,6 +15,7 @@ private: ScriptEngine scriptEngine; HotReload* hotReload; bool scriptCompilationError; + FILE* logFile; static const int WINDOW_WIDTH = 800; static const int WINDOW_HEIGHT = 600; diff --git a/scripts/test.as b/scripts/test.as index 209cfb9..f207f86 100644 --- a/scripts/test.as +++ b/scripts/test.as @@ -8,4 +8,4 @@ void Update(float dt) { void Draw() { DrawText("Hello from AngelScript - Working perfectly!", int(x), int(y), 20, 0xFF0000FF); -} \ No newline at end of file +} diff --git a/src/Application.cpp b/src/Application.cpp index d458f4c..b31c408 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -3,11 +3,11 @@ #include #include #include - +#include "log/log.h" const char* Application::WINDOW_TITLE = "Raylib + AngelScript"; const char* Application::SCRIPT_FILE = "scripts/test.as"; -Application::Application() : hotReload(nullptr), scriptCompilationError(false) { +Application::Application() : hotReload(nullptr), scriptCompilationError(false), logFile(nullptr) { } Application::~Application() { @@ -15,6 +15,10 @@ Application::~Application() { } bool Application::Initialize() { + logFile = fopen("log.txt", "w"); + log_add_fp(logFile, LOG_TRACE); + + SetTraceLogCallback(raylib_log); // Initialize Raylib InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE); SetTargetFPS(TARGET_FPS); @@ -84,5 +88,14 @@ void Application::Shutdown() { } scriptEngine.Shutdown(); + + // Clear the trace log callback before closing window to prevent logging after cleanup + SetTraceLogCallback(nullptr); CloseWindow(); + + // Close log file after everything else is cleaned up + if (logFile) { + fclose(logFile); + logFile = nullptr; + } } \ No newline at end of file diff --git a/src/log/log.c b/src/log/log.c new file mode 100644 index 0000000..ffc1a8a --- /dev/null +++ b/src/log/log.c @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2020 rxi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "log.h" + +#define MAX_CALLBACKS 32 + +typedef struct +{ + log_LogFn fn; + void *udata; + int level; +} Callback; + +static struct +{ + void *udata; + log_LockFn lock; + int level; + bool quiet; + Callback callbacks[MAX_CALLBACKS]; +} L; + +static const char *level_strings[] = { + "", "TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "FATAL"}; + +#ifdef LOG_USE_COLOR +static const char *level_colors[] = { + "","\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"}; +#endif + +static void stdout_callback(log_Event *ev) +{ + char buf[16]; + buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; +#ifdef LOG_USE_COLOR + if (ev->file) + { + fprintf( + ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", + buf, level_colors[ev->level], level_strings[ev->level], + ev->file, ev->line); + } + else + { + fprintf( + ev->udata, "%s %s%-5s\x1b[0m ", + buf, level_colors[ev->level], level_strings[ev->level]); + } +#else + if (ev->file) + { + fprintf( + ev->udata, "%s %-5s %s:%d: ", + buf, level_strings[ev->level], ev->file, ev->line); + } + else + { + fprintf( + ev->udata, "%s %-5s: ", + buf, level_strings[ev->level]); + } + +#endif + vfprintf(ev->udata, ev->fmt, ev->ap); + fprintf(ev->udata, "\n"); + fflush(ev->udata); +} + +static void file_callback(log_Event *ev) +{ + char buf[64]; + buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0'; + if (ev->file) + { + fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file, ev->line); + } + else + { + fprintf(ev->udata, "%s %-5s: ", buf, level_strings[ev->level]); + } + vfprintf(ev->udata, ev->fmt, ev->ap); + fprintf(ev->udata, "\n"); + fflush(ev->udata); +} + +static void lock(void) +{ + if (L.lock) + { + L.lock(true, L.udata); + } +} + +static void unlock(void) +{ + if (L.lock) + { + L.lock(false, L.udata); + } +} + +const char *log_level_string(int level) +{ + return level_strings[level]; +} + +void log_set_lock(log_LockFn fn, void *udata) +{ + L.lock = fn; + L.udata = udata; +} + +void log_set_level(int level) +{ + L.level = level; +} + +void log_set_quiet(bool enable) +{ + L.quiet = enable; +} + +int log_add_callback(log_LogFn fn, void *udata, int level) +{ + for (int i = 0; i < MAX_CALLBACKS; i++) + { + if (!L.callbacks[i].fn) + { + L.callbacks[i] = (Callback){fn, udata, level}; + return 0; + } + } + return -1; +} + +int log_add_fp(FILE *fp, int level) +{ + return log_add_callback(file_callback, fp, level); +} + +static void init_event(log_Event *ev, void *udata) +{ + if (!ev->time) + { + time_t t = time(NULL); + ev->time = localtime(&t); + } + ev->udata = udata; +} + +void log_log(int level, const char *file, int line, const char *fmt, ...) +{ + log_Event ev = { + .fmt = fmt, + .file = file, + .line = line, + .level = level, + }; + + lock(); + + if (!L.quiet && level >= L.level) + { + init_event(&ev, stderr); + va_start(ev.ap, fmt); + stdout_callback(&ev); + va_end(ev.ap); + } + + for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) + { + Callback *cb = &L.callbacks[i]; + if (level >= cb->level) + { + init_event(&ev, cb->udata); + va_start(ev.ap, fmt); + cb->fn(&ev); + va_end(ev.ap); + } + } + + unlock(); +} + +void raylib_log(int msgType, const char *text, va_list args) +{ + log_log(msgType, NULL, 0, text, args); +} \ No newline at end of file diff --git a/src/log/log.h b/src/log/log.h new file mode 100644 index 0000000..6d62bc6 --- /dev/null +++ b/src/log/log.h @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2020 rxi + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See `log.c` for details. + */ + +#ifndef LOG_H +#define LOG_H + +#include +#include +#include +#include +#include "raylib.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define LOG_VERSION "0.1.0" +#define LOG_USE_COLOR +typedef struct { + va_list ap; + const char *fmt; + const char *file; + struct tm *time; + void *udata; + int line; + int level; +} log_Event; + +typedef void (*log_LogFn)(log_Event *ev); +typedef void (*log_LockFn)(bool lock, void *udata); + +#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) +#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) +#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__) +#define log_warn(...) log_log(LOG_WARNING, __FILE__, __LINE__, __VA_ARGS__) +#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__) +#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__) + +const char* log_level_string(int level); +void log_set_lock(log_LockFn fn, void *udata); +void log_set_level(int level); +void log_set_quiet(bool enable); +int log_add_callback(log_LogFn fn, void *udata, int level); +int log_add_fp(FILE *fp, int level); + +void log_log(int level, const char *file, int line, const char *fmt, ...); + + +void raylib_log(int msgType, const char *text, va_list args); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file