From fb9f9d30ed4bc9c40b6d6b744dc624cda7bcb476 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sun, 25 Jan 2026 14:19:27 +0900 Subject: [PATCH] logging --- engine/include/GearSrc/TypeDefs.h | 9 +++ engine/src/client.c | 116 ++++++++++++++++++++++-------- engine/src/gl_common.c | 12 ++-- engine/src/log.c | 21 ++++++ 4 files changed, 124 insertions(+), 34 deletions(-) diff --git a/engine/include/GearSrc/TypeDefs.h b/engine/include/GearSrc/TypeDefs.h index f7ea5f1..ef6e5a1 100644 --- a/engine/include/GearSrc/TypeDefs.h +++ b/engine/include/GearSrc/TypeDefs.h @@ -58,6 +58,7 @@ typedef struct _GSModel* GSModel; typedef struct _GSModelFace GSModelFace; typedef struct _GSModelMaterial GSModelMaterial; typedef struct _GSModelUseMaterial GSModelUseMaterial; +typedef struct _GSEngineLog GSEngineLog; #else typedef void* GSClient; typedef void* GSServer; @@ -183,6 +184,12 @@ struct _GSServer { GSEngine engine; }; +struct _GSEngineLog { + char log[1024]; + int level; + long tick; +}; + struct _GSEngine { GSEngineParam* param; GSClient client; @@ -192,6 +199,8 @@ struct _GSEngine { int height; GSNumber tps; GSNumber tps_sampled; + + GSEngineLog* log; }; struct _GSFile { diff --git a/engine/src/client.c b/engine/src/client.c index a1fd595..19f681f 100644 --- a/engine/src/client.c +++ b/engine/src/client.c @@ -7,6 +7,8 @@ #include #include +#include + GSClient GSClientCreate(GSEngine engine) { GSClient client = malloc(sizeof(*client)); void* ptr[] = { @@ -99,10 +101,12 @@ void GSClientStep(GSClient client) { GSVector4 yellow = {1, 1, 0, 1}; GSVector4 half = {0, 0, 0, 0.5}; GSVector2 sq[4]; + int i, j, k, c; GSGLClear(client->gl); GSGLCameraSetup(client->gl); GSGLSetLight(client->gl); + GSGLSetColor(client->gl, white); GSGLTextBold(client->gl, 0); @@ -117,27 +121,6 @@ void GSClientStep(GSClient client) { scene(client); GSGLShadowEnd(client->gl); - sprintf(buf, "%.1f FPS", client->engine->tps_sampled); - sprintf(buf2, "%s%s", client->engine->tps_sampled < 10 ? " " : "", buf); - - tw = GSGLTextWidth(client->gl, buf2); - th = GSGLTextHeight(client->gl, buf2); - - sq[0][0] = client->engine->width - tw, sq[0][1] = 0; - sq[1][0] = client->engine->width - tw, sq[1][1] = th; - sq[2][0] = client->engine->width, sq[2][1] = th; - sq[3][0] = client->engine->width, sq[3][1] = 0; - - GSGLSetColor(client->gl, half); - GSGLBegin2D(client->gl); - GSGLPolygon2D(client->gl, 4, sq, NULL); - GSGLEnd2D(client->gl); - - GSGLSetColor(client->gl, yellow); - GSGLTextBold(client->gl, 1); - GSGLText(client->gl, client->engine->width - tw, 0, buf2); - GSGLTextBold(client->gl, 0); - GSVersionGet(&ver); sprintf(buf, "Engine version %s", ver.string); @@ -154,9 +137,9 @@ void GSClientStep(GSClient client) { sh = th + th2; sq[0][0] = client->engine->width - sw, sq[0][1] = client->engine->height - sh; - sq[1][0] = client->engine->width - sw, sq[1][1] = client->engine->height; - sq[2][0] = client->engine->width, sq[2][1] = client->engine->height; - sq[3][0] = client->engine->width, sq[3][1] = client->engine->height - sh; + sq[1][0] = sq[0][0], sq[1][1] = client->engine->height; + sq[2][0] = client->engine->width, sq[2][1] = sq[1][1]; + sq[3][0] = sq[2][0], sq[3][1] = sq[0][1]; GSGLSetColor(client->gl, half); GSGLBegin2D(client->gl); @@ -167,13 +150,90 @@ void GSClientStep(GSClient client) { GSGLText(client->gl, client->engine->width - tw, client->engine->height - th * 2, buf); GSGLText(client->gl, client->engine->width - tw2, client->engine->height - th2, buf2); + for(k = 0; k < 2; k++) { + c = 0; + for(i = arrlen(client->engine->log) - 1, j = 0; i >= 0 && j < 10; i--, j++) { + if(client->engine->param->get_tick != NULL) { + if((client->engine->param->get_tick() - client->engine->log[i].tick) > 3000) continue; + } + + if(k == 1) { + GSNumber y = c * GSGLTextHeight(client->gl, "M"); + GSNumber w = 0; + char str[6]; + int level = client->engine->log[i].level; + GSVector4 col = {0, 0, 0, 1}; + + str[0] = 0; + + if(level == GSLogInfo) { + strcpy(str, "INFO"); + + col[0] = col[1] = col[2] = 1; + } else if(level == GSLogWarn) { + strcpy(str, "WARN"); + + col[0] = col[2] = 1; + } else if(level == GSLogError) { + strcpy(str, "ERROR"); + + col[0] = 1; + } else if(level == GSLogDebug) { + strcpy(str, "DEBUG"); + + col[2] = 1; + } + GSGLSetColor(client->gl, col); + + GSGLTextBold(client->gl, 1); + GSGLText(client->gl, (5 - strlen(str)) * GSGLTextWidth(client->gl, "M") / 2, y, str); + GSGLTextBold(client->gl, 0); + + GSGLSetColor(client->gl, white); + GSGLText(client->gl, 0, y, " |"); + GSGLText(client->gl, GSGLTextWidth(client->gl, "M") * 6, y, client->engine->log[i].log); + } + + c++; + } + + if(k == 0) { + sq[0][0] = 0, sq[0][1] = 0; + sq[1][0] = 0, sq[1][1] = c * GSGLTextHeight(client->gl, "M"); + sq[2][0] = client->engine->width, sq[2][1] = sq[1][1]; + sq[3][0] = sq[2][0], sq[3][1] = 0; + + GSGLSetColor(client->gl, half); + GSGLBegin2D(client->gl); + GSGLPolygon2D(client->gl, 4, sq, NULL); + GSGLEnd2D(client->gl); + } + } + + sprintf(buf, "%.1f FPS", client->engine->tps_sampled); + sprintf(buf2, "%s%s", client->engine->tps_sampled < 10 ? " " : "", buf); + + tw = GSGLTextWidth(client->gl, buf2); + th = GSGLTextHeight(client->gl, buf2); + + sq[0][0] = client->engine->width - tw, sq[0][1] = 0; + sq[1][0] = sq[0][0], sq[1][1] = th; + sq[2][0] = client->engine->width, sq[2][1] = sq[1][1]; + sq[3][0] = sq[2][0], sq[3][1] = 0; + + GSGLSetColor(client->gl, half); + GSGLBegin2D(client->gl); + GSGLPolygon2D(client->gl, 4, sq, NULL); + GSGLEnd2D(client->gl); + + GSGLSetColor(client->gl, yellow); + GSGLTextBold(client->gl, 1); + GSGLText(client->gl, client->engine->width - tw, 0, buf2); + GSGLTextBold(client->gl, 0); + client->engine->param->gl_swapbuffer(); if(client->engine->param->after_render != NULL) client->engine->param->after_render(client->engine); - - static double r = 0; - - r += 0.1; } GSGL GSClientGetGL(GSClient client) { diff --git a/engine/src/gl_common.c b/engine/src/gl_common.c index 45e2938..df86d48 100644 --- a/engine/src/gl_common.c +++ b/engine/src/gl_common.c @@ -97,14 +97,14 @@ void GSGLTextEx(GSGL gl, GSNumber x, GSNumber y, const char* text, GSNumber sx, gy = gy * sh; v[0][0] = px, v[0][1] = py; - v[1][0] = px, v[1][1] = py + gh; - v[2][0] = px + gw, v[2][1] = py + gh; - v[3][0] = px + gw, v[3][1] = py; + v[1][0] = v[0][0], v[1][1] = py + gh; + v[2][0] = px + gw, v[2][1] = v[1][1]; + v[3][0] = v[2][0], v[3][1] = v[0][1]; t[0][0] = gx, t[0][1] = gy; - t[1][0] = gx, t[1][1] = gy + sh; - t[2][0] = gx + sw, t[2][1] = gy + sh; - t[3][0] = gx + sw, t[3][1] = gy; + t[1][0] = t[0][0], t[1][1] = gy + sh; + t[2][0] = gx + sw, t[2][1] = t[1][1]; + t[3][0] = t[2][0], t[3][1] = t[0][1]; GSGLPolygon2D(gl, 4, v, t); diff --git a/engine/src/log.c b/engine/src/log.c index a90d7fd..397e84b 100644 --- a/engine/src/log.c +++ b/engine/src/log.c @@ -1,5 +1,7 @@ #include +#include + void GSLog(GSEngine engine, int level, const char* fmt, ...) { va_list va; FILE* out = stdout; @@ -40,5 +42,24 @@ void GSLog(GSEngine engine, int level, const char* fmt, ...) { vfprintf(out, fmt, va); va_end(va); + if(engine != NULL) { + GSEngineLog log; + char str[6]; + + log.level = level; + + va_start(va, fmt); + vsprintf(log.log, fmt, va); + va_end(va); + + if(engine->param->get_tick == NULL) { + log.tick = 0; + } else { + log.tick = engine->param->get_tick(); + } + + arrput(engine->log, log); + } + fprintf(out, "\n"); }