From ea7c45ae01d63dc2a7f225172b024160c2a916e7 Mon Sep 17 00:00:00 2001 From: Nick Koirala Date: Thu, 6 Nov 2025 15:15:33 +1300 Subject: [PATCH] feat: Log with levels from AngelScript --- include/ScriptBindings.h | 2 ++ scripts/update.as | 3 ++ src/ScriptBindings.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/include/ScriptBindings.h b/include/ScriptBindings.h index 388e0f5..217f05e 100644 --- a/include/ScriptBindings.h +++ b/include/ScriptBindings.h @@ -8,5 +8,7 @@ public: private: static void Print(asIScriptGeneric* gen); + // Log(level, message) - generic to get script context + static void Log(asIScriptGeneric* gen); static void AS_DrawText(const std::string &text, int x, int y, int fontSize, unsigned int color); }; \ No newline at end of file diff --git a/scripts/update.as b/scripts/update.as index 2c9c18f..97afa74 100644 --- a/scripts/update.as +++ b/scripts/update.as @@ -6,5 +6,8 @@ void Update(float dt) { if (x > 800) { x = 0; Print("X position reset!"); + Log(LOG_INFO, "Log INFO: reset happened"); + Log(LOG_WARNING, "Log WARNING: reset happened"); + Log(LOG_ERROR, "Log ERROR: reset happened"); } } \ No newline at end of file diff --git a/src/ScriptBindings.cpp b/src/ScriptBindings.cpp index 60f1f8a..d715063 100644 --- a/src/ScriptBindings.cpp +++ b/src/ScriptBindings.cpp @@ -4,12 +4,39 @@ #include #include "log/log.h" +// Expose log level constants to AngelScript +static int AS_LOG_TRACE = 1; +static int AS_LOG_DEBUG = 2; +static int AS_LOG_INFO = 3; +static int AS_LOG_WARNING = 4; +static int AS_LOG_ERROR = 5; +static int AS_LOG_FATAL = 6; + void ScriptBindings::RegisterAll(asIScriptEngine* engine) { // Register Print function with generic calling convention to access context int r = engine->RegisterGlobalFunction("void Print(const string &in)", asFUNCTION(Print), asCALL_GENERIC); assert(r >= 0); + // Register log level constants so scripts can refer to LOG_INFO, LOG_ERROR, etc. + r = engine->RegisterGlobalProperty("int LOG_TRACE", (void*)&AS_LOG_TRACE); + assert(r >= 0); + r = engine->RegisterGlobalProperty("int LOG_DEBUG", (void*)&AS_LOG_DEBUG); + assert(r >= 0); + r = engine->RegisterGlobalProperty("int LOG_INFO", (void*)&AS_LOG_INFO); + assert(r >= 0); + r = engine->RegisterGlobalProperty("int LOG_WARNING", (void*)&AS_LOG_WARNING); + assert(r >= 0); + r = engine->RegisterGlobalProperty("int LOG_ERROR", (void*)&AS_LOG_ERROR); + assert(r >= 0); + r = engine->RegisterGlobalProperty("int LOG_FATAL", (void*)&AS_LOG_FATAL); + assert(r >= 0); + + // Register Log(level, message) to allow scripts to log at different levels + r = engine->RegisterGlobalFunction("void Log(int, const string &in)", + asFUNCTION(Log), asCALL_GENERIC); + assert(r >= 0); + // Register DrawText function r = engine->RegisterGlobalFunction("void DrawText(const string &in, int, int, int, uint)", asFUNCTION(AS_DrawText), asCALL_CDECL); @@ -43,6 +70,38 @@ void ScriptBindings::Print(asIScriptGeneric* gen) { } } +void ScriptBindings::Log(asIScriptGeneric* gen) { + // arg0: int level, arg1: string message + int level = gen->GetArgDWord(0); + std::string* msg = static_cast(gen->GetArgObject(1)); + + // Obtain script context to fetch file/line + asIScriptContext* ctx = asGetActiveContext(); + if (ctx) { + const char* section = nullptr; + int line = 0; + line = ctx->GetLineNumber(0, nullptr, §ion); + if (section && line > 0) { + // Use the library's log_log to preserve filename/line info + log_log(level, section, line, "%s", msg ? msg->c_str() : ""); + return; + } + } + + // Fallback: log without script location + switch (level) { + case LOG_TRACE: log_trace("%s", msg ? msg->c_str() : ""); break; + case LOG_DEBUG: log_debug("%s", msg ? msg->c_str() : ""); break; + case LOG_WARNING: log_warn("%s", msg ? msg->c_str() : ""); break; + case LOG_ERROR: log_error("%s", msg ? msg->c_str() : ""); break; + case LOG_FATAL: log_fatal("%s", msg ? msg->c_str() : ""); break; + case LOG_INFO: + default: + log_info("%s", msg ? msg->c_str() : ""); + break; + } +} + Color ColorFromUInt(unsigned int c) { Color col; col.r = (c >> 24) & 0xFF;