feat: Log with levels from AngelScript

This commit is contained in:
2025-11-06 15:15:33 +13:00
parent a337b9002f
commit ea7c45ae01
3 changed files with 64 additions and 0 deletions

View File

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

View File

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

View File

@@ -4,12 +4,39 @@
#include <assert.h>
#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<std::string*>(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, &section);
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;