From: Lukas Krickl Date: Mon, 23 Feb 2026 05:38:23 +0000 (+0100) Subject: engine: Added basic logging X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;ds=inline;p=lrts%2F.git engine: Added basic logging --- diff --git a/TODO.md b/TODO.md index b6dbda7..51a02a4 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1,18 @@ # TODO +## Gameplay + +## Goals + +- Portable +- Avoid libraries +- Simple +- C89 only (if possible should compile with gcc, clang and tcc) +- Built-in scripting language +- Built in map editor +- Built in sprite editor +- Resource files and assets are git-friendly +- Isometric map rendering +- Replays +- Networking is optional +- Lockstep networking diff --git a/src/lrts.c b/src/lrts.c index 364fdbc..ff85d87 100644 --- a/src/lrts.c +++ b/src/lrts.c @@ -10,7 +10,7 @@ struct lrts_config* lrts_cfg() { void lrts_help(int argc, char **argv) { u_fprintf(u_stderr, "%s\n", argv[0]); - u_fprintf(u_stderr, "Usage: %s [-%s] [-c=command]\n\n", + u_fprintf(u_stderr, "Usage: %s [-%s] [-c=command] [demo file]\n\n", argv[0], LRTS_OPTS); LRTS_HELP("h", "display this help and exit"); LRTS_HELP("V", "display version info and exit"); diff --git a/src/lrts.h b/src/lrts.h index d3ee797..b24dc6b 100644 --- a/src/lrts.h +++ b/src/lrts.h @@ -26,6 +26,7 @@ #include "p_pc/u_assert.c" #include "p_posix/p_init.c" #include "p_posix/p_getopt.c" +#include "p_pc/u_log.c" #else #error "No platform is provided" #endif @@ -48,6 +49,7 @@ #include "n_conn.c" #include "u_math.c" #include "t_tile.c" +#include "u_log.c" #endif diff --git a/src/p_pc/p_pc.h b/src/p_pc/p_pc.h new file mode 100644 index 0000000..a8bda81 --- /dev/null +++ b/src/p_pc/p_pc.h @@ -0,0 +1,6 @@ +#ifndef P_PC_H__ +#define P_PC_H__ + +int u_vfprintf(U_FILE* stream, const char *fmt, void *args); + +#endif diff --git a/src/p_pc/u_log.c b/src/p_pc/u_log.c new file mode 100644 index 0000000..48ef2b5 --- /dev/null +++ b/src/p_pc/u_log.c @@ -0,0 +1,18 @@ +#include "../u_log.h" +#include "../u_stdio.h" +#include "p_pc.h" +#include +#include + +int u_log(enum u_log_levels level, const char *fmt, ...) { + va_list args; + int res = 0; + + if (!u_log_should_log(level)) { + return 0; + } + va_start(args, fmt); + res = u_fprintf(u_stderr, fmt, args); + va_end(args); + return res; +} diff --git a/src/p_pc/u_stdio.c b/src/p_pc/u_stdio.c index ea7f489..4b73a30 100644 --- a/src/p_pc/u_stdio.c +++ b/src/p_pc/u_stdio.c @@ -6,14 +6,22 @@ void *u_stdin; void *u_stdout; void *u_stderr; + +int u_vfprintf(U_FILE* stream, const char *fmt, void *args) { + return vfprintf(stream, fmt, args); +} + int u_fprintf(U_FILE* stream, const char *fmt, ...) { int res = 0; va_list args; va_start(args, fmt); - res = vfprintf(stream, fmt, args); + u_vfprintf(stream, fmt, args); va_end(args); return res; } +int u_fputs(const char *s, U_FILE* stream) { + return fputs(s, stream); +} diff --git a/src/p_r_sdl/p_init.c b/src/p_r_sdl/p_init.c index d7784e9..fe05c0e 100644 --- a/src/p_r_sdl/p_init.c +++ b/src/p_r_sdl/p_init.c @@ -2,27 +2,31 @@ #include #include "p_window.h" #include "../u_defs.h" +#include "../u_log.h" #include "p_window.c" int p_render_init(void) { if (!SDL_Init(SDL_INIT_VIDEO)) { - u_fprintf(u_stderr, "Failed to init video: %s\n", + u_log(U_LOG_ERR, "Failed to init video: %s\n", SDL_GetError()); exit(-1); } p_main_window = SDL_CreateWindow(lrts_cfg()->name, 640, 640, 0); if (p_main_window == LRTS_NULL) { - u_fprintf(u_stderr, "Failed to create window: %s\n", SDL_GetError()); + u_log(U_LOG_ERR, "Failed to create window: %s\n", SDL_GetError()); exit(-1); } + u_log(U_LOG_DEBUG, "Init successful\n"); + return 0; } int p_renderer_finish(void) { SDL_DestroyWindow(p_main_window); SDL_Quit(); + u_log(U_LOG_DEBUG, "Exiting...\n"); return 0; } diff --git a/src/u_log.c b/src/u_log.c new file mode 100644 index 0000000..47d65ce --- /dev/null +++ b/src/u_log.c @@ -0,0 +1,6 @@ +#include "u_log.h" + +lrts_bool u_log_should_log(enum u_log_levels level) { + /* TODO: implement log levels */ + return LRTS_TRUE; +} diff --git a/src/u_log.h b/src/u_log.h new file mode 100644 index 0000000..40dc233 --- /dev/null +++ b/src/u_log.h @@ -0,0 +1,26 @@ +#ifndef U_LOG_H__ +#define U_LOG_H__ + +#include "u_defs.h" +#include "u_stdio.h" + +enum u_log_levels { + U_LOG_DEBUG, + U_LOG_INF, + U_LOG_WARN, + U_LOG_ERR, + U_LOG_CRIT +}; + +/** + * Logs a message to the log stream + * this is platform specific + */ +int u_log(enum u_log_levels level, const char *fmt, ...); + +/** + * informs the platform if u_log level should be logged + */ +lrts_bool u_log_should_log(enum u_log_levels level); + +#endif diff --git a/src/u_stdio.h b/src/u_stdio.h index ea2369a..88deabd 100644 --- a/src/u_stdio.h +++ b/src/u_stdio.h @@ -9,6 +9,7 @@ extern void *u_stdout; extern void *u_stderr; int u_fprintf(U_FILE* stream, const char *fmt, ...); +int u_fputs(const char *s, U_FILE* stream); #endif