summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-04-28 07:50:31 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-04-28 09:23:47 +0200
commit63eb46698b1e19d3f36944992b948c54a7a3740b (patch)
tree1aa8b686039e2f9291d680c21a47427de5daa12b /libraries
parent0ed91795a2db720e688fd2daefd22f7e9c754c2f (diff)
downloadstalag-63eb46698b1e19d3f36944992b948c54a7a3740b.tar.gz
Compiler settings and macOS port
Diffstat (limited to 'libraries')
-rw-r--r--libraries/nonstd.h87
-rw-r--r--libraries/vfs.h16
2 files changed, 10 insertions, 93 deletions
diff --git a/libraries/nonstd.h b/libraries/nonstd.h
index 531834d..f1848d3 100644
--- a/libraries/nonstd.h
+++ b/libraries/nonstd.h
@@ -77,12 +77,12 @@ NONSTD_DEF void *safe_realloc(void *ptr, size_t item_size, size_t count);
#define UNUSED(value) (void)(value)
#define TODO(message) \
do { \
- fprintf(stderr, "%s:%d: TODO: %s\n", __FILE__, __LINE__, message); \
+ TraceLog(LOG_ERROR, "%s:%d: TODO: %s", __FILE__, __LINE__, message); \
abort(); \
} while (0)
#define UNREACHABLE(message) \
do { \
- fprintf(stderr, "%s:%d: UNREACHABLE: %s\n", __FILE__, __LINE__, message); \
+ TraceLog(LOG_ERROR, "%s:%d: UNREACHABLE: %s", __FILE__, __LINE__, message); \
abort(); \
} while (0)
@@ -315,29 +315,6 @@ NONSTD_DEF stringb read_entire_file_sb(const char *filepath);
NONSTD_DEF int write_file_sv(const char *filepath, stringv sv);
NONSTD_DEF int write_file_sb(const char *filepath, const stringb *sb);
-// Logging
-typedef enum {
- LOG_ERROR,
- LOG_WARN,
- LOG_INFO,
- LOG_DEBUG,
-} LogLevel;
-
-NONSTD_DEF void set_log_level(LogLevel level);
-NONSTD_DEF LogLevel get_log_level_from_env(void);
-NONSTD_DEF void log_message(FILE *stream, LogLevel level, const char *format, ...);
-
-#define LOG_INFO_MSG(...) log_message(stdout, LOG_INFO, __VA_ARGS__)
-#define LOG_DEBUG_MSG(...) log_message(stdout, LOG_DEBUG, __VA_ARGS__)
-#define LOG_WARN_MSG(...) log_message(stderr, LOG_WARN, __VA_ARGS__)
-#define LOG_ERROR_MSG(...) log_message(stderr, LOG_ERROR, __VA_ARGS__)
-
-#define COLOR_RESET "\033[0m"
-#define COLOR_INFO "\033[32m"
-#define COLOR_DEBUG "\033[36m"
-#define COLOR_WARNING "\033[33m"
-#define COLOR_ERROR "\033[31m"
-
#ifdef NONSTD_IMPLEMENTATION
NONSTD_DEF void *safe_malloc(size_t item_size, size_t count) {
@@ -587,66 +564,6 @@ NONSTD_DEF int write_file_sb(const char *filepath, const stringb *sb) {
return write_entire_file(filepath, sb->data, sb->length);
}
-// Logging Implementation
-
-static LogLevel max_level = LOG_INFO;
-
-static const char *level_strings[] = {
- "ERROR",
- "WARN",
- "INFO",
- "DEBUG",
-};
-
-static const char *level_colors[] = {
- COLOR_ERROR,
- COLOR_WARNING,
- COLOR_INFO,
- COLOR_DEBUG,
-};
-
-NONSTD_DEF void set_log_level(LogLevel level) {
- max_level = level;
-}
-
-NONSTD_DEF LogLevel get_log_level_from_env(void) {
- const char *env = getenv("LOG_LEVEL");
- if (env) {
- int level = atoi(env);
- if (level >= 0 && level <= 3) {
- return (LogLevel)level;
- }
- }
-
- return max_level;
-}
-
-NONSTD_DEF void log_message(FILE *stream, LogLevel level, const char *format, ...) {
- if (max_level < level)
- return;
-
- struct timeval tv;
- gettimeofday(&tv, NULL);
- struct tm *tm_info = localtime(&tv.tv_sec);
-
- char time_str[24];
- strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", tm_info);
-
- const char *color = isatty(fileno(stream)) ? level_colors[level] : "";
- const char *reset = isatty(fileno(stream)) ? COLOR_RESET : "";
-
- const char *log_format = "%s[%s.%03d] [%-5s] ";
- fprintf(stream, log_format, color, time_str, (int)(tv.tv_usec / 1000), level_strings[level]);
-
- va_list args;
- va_start(args, format);
- vfprintf(stream, format, args);
- va_end(args);
-
- fprintf(stream, "%s\n", reset);
- fflush(stream);
-}
-
// PPM Image Implementation
NONSTD_DEF Canvas ppm_init(u32 width, u32 height) {
diff --git a/libraries/vfs.h b/libraries/vfs.h
index 6a243cc..6017407 100644
--- a/libraries/vfs.h
+++ b/libraries/vfs.h
@@ -57,28 +57,28 @@ void vfs_init(const char* pak_path) {
const char* debug_env = getenv("DEBUG");
if (debug_env && (strcmp(debug_env, "1") == 0 || strcmp(debug_env, "true") == 0)) {
g_disk_mode = 1;
- log_message(stdout, LOG_INFO, "VFS: Operating in Disk Mode (DEBUG enabled)");
+ TraceLog(LOG_INFO, "VFS: Operating in Disk Mode (DEBUG enabled)");
return;
}
FILE* f = fopen(pak_path, "rb");
if (!f) {
- log_message(stderr, LOG_ERROR, "VFS: Failed to open pak file: %s (Error: %s)", pak_path, strerror(errno));
- log_message(stderr, LOG_WARN, "VFS: Falling back to Disk Mode");
+ TraceLog(LOG_ERROR, "VFS: Failed to open pak file: %s (Error: %s)", pak_path, strerror(errno));
+ TraceLog(LOG_WARNING, "VFS: Falling back to Disk Mode");
g_disk_mode = 1;
return;
}
VfsHeader header;
if (fread(&header, sizeof(VfsHeader), 1, f) != 1) {
- log_message(stderr, LOG_ERROR, "VFS: Failed to read header from %s", pak_path);
+ TraceLog(LOG_ERROR, "VFS: Failed to read header from %s", pak_path);
fclose(f);
g_disk_mode = 1;
return;
}
if (memcmp(header.magic, "DRP1", 4) != 0) {
- log_message(stderr, LOG_ERROR, "VFS: Invalid magic in %s", pak_path);
+ TraceLog(LOG_ERROR, "VFS: Invalid magic in %s", pak_path);
fclose(f);
g_disk_mode = 1;
return;
@@ -87,7 +87,7 @@ void vfs_init(const char* pak_path) {
g_num_entries = header.num_files;
g_entries = (VfsEntry*)malloc(sizeof(VfsEntry) * g_num_entries);
if (fread(g_entries, sizeof(VfsEntry), g_num_entries, f) != g_num_entries) {
- log_message(stderr, LOG_ERROR, "VFS: Failed to read index table from %s", pak_path);
+ TraceLog(LOG_ERROR, "VFS: Failed to read index table from %s", pak_path);
free(g_entries);
g_entries = NULL;
fclose(f);
@@ -97,7 +97,7 @@ void vfs_init(const char* pak_path) {
fclose(f);
strncpy(g_pak_path, pak_path, sizeof(g_pak_path) - 1);
- log_message(stdout, LOG_INFO, "VFS: Loaded %u files from %s", g_num_entries, pak_path);
+ TraceLog(LOG_INFO, "VFS: Loaded %u files from %s", g_num_entries, pak_path);
}
void vfs_shutdown(void) {
@@ -137,7 +137,7 @@ VfsFile* vfs_open(const char* path) {
}
}
- log_message(stderr, LOG_WARN, "VFS: File not found: %s", path);
+ TraceLog(LOG_WARNING, "VFS: File not found: %s", path);
return NULL;
}