4 Commits

Author SHA1 Message Date
abdelrahman c67a448d00 Add basic logging functionality
Release / release (push) Successful in 6s
2026-05-17 18:40:10 +01:00
abdelrahman 2e5163ba33 Implement functions to get stdin, stdout & stderr
Release / release (push) Successful in 2s
2026-05-17 12:21:23 +01:00
abdelrahman 515493b963 Fix queue MSVC errors 2026-05-17 12:20:47 +01:00
abdelrahman 8061692801 Add standard streams
Release / release (push) Successful in 3s
2026-05-17 11:13:40 +01:00
14 changed files with 232 additions and 9 deletions
+1 -1
View File
@@ -1 +1 @@
1.0.4 1.1.0
+25 -2
View File
@@ -30,7 +30,10 @@ GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue,
GenericQueue *output = queue; GenericQueue *output = queue;
u64 capacity = wapp_array_capacity(queue->items); u64 capacity = wapp_array_capacity(queue->items);
if (queue->count >= capacity) {
// NOTE (Abdelrahman): Extracted into variable to fix MSVC error
b8 queue_full = queue->count >= capacity;
if (queue_full) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(capacity * 2); u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(capacity * 2);
u64 array_size = _array_calc_alloc_size(new_capacity, item_size); u64 array_size = _array_calc_alloc_size(new_capacity, item_size);
u64 alloc_size = sizeof(GenericQueue) + array_size; u64 alloc_size = sizeof(GenericQueue) + array_size;
@@ -51,7 +54,27 @@ GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue,
void *copy_boundary = (void *)((uptr)(queue->items) + (queue->front * item_size)); void *copy_boundary = (void *)((uptr)(queue->items) + (queue->front * item_size));
memcpy(output->items, copy_boundary, front_count * item_size); memcpy(output->items, copy_boundary, front_count * item_size);
if (back_count > 0) {
/**
* NOTE (Abdelrahman): Since this is a ring buffer, the elements at the beginning of the array
* aren't always the ones at the front of the queue. When that's the case, the memcpy above
* will only copy a subset of the elements. This is why we need to copy the remaining ones.
*
* Example: Take a queue that looks like this with a capacity of 5 elements
*
* 0 1 | 2 3 4
* ---------------|-----------------------
* | * | * | * | * | * |
* ---------------|-----------------------
* |
* queue_front = 2
* queue_back = 2
*
* In this case, the first memcpy will only copy elements 2-4. The memcpy below will be
* responsible for copying elements 0-1.
*/
b8 items_left_to_copy = back_count > 0;
if (items_left_to_copy) {
void *back_copy_dst = (void *)((uptr)(output->items) + (front_count * item_size)); void *back_copy_dst = (void *)((uptr)(output->items) + (front_count * item_size));
memcpy(back_copy_dst, queue->items, back_count * item_size); memcpy(back_copy_dst, queue->items, back_count * item_size);
} }
+99
View File
@@ -0,0 +1,99 @@
// vim:fileencoding=utf-8:foldmethod=marker
#include "log.h"
#include "../common/aliases/aliases.h"
#include "../common/assert/assert.h"
#include "../os/file/file.h"
#define LOG_LEVEL_STR_LENGTH 8
#define LOG_PREFIX_BUF_LENGTH 16
typedef struct {
WFile *outlog;
WFile *errlog;
LogLevel level;
} LogConfig;
wapp_intern LogConfig LOG_CONFIG = {
.level = WAPP_LOG_DEBUG,
};
wapp_intern Str8RO LOG_LEVEL_STRINGS[COUNT_LOG_LEVEL] = {
[WAPP_LOG_FATAL] = wapp_str8_lit_ro_initialiser_list("[ FATAL ] "),
[WAPP_LOG_CRITICAL] = wapp_str8_lit_ro_initialiser_list("[ CRITICAL ] "),
[WAPP_LOG_ERROR] = wapp_str8_lit_ro_initialiser_list("[ ERROR ] "),
[WAPP_LOG_WARNING] = wapp_str8_lit_ro_initialiser_list("[ WARNING ] "),
[WAPP_LOG_INFO] = wapp_str8_lit_ro_initialiser_list("[ INFO ] "),
[WAPP_LOG_DEBUG] = wapp_str8_lit_ro_initialiser_list("[ DEBUG ] "),
};
wapp_intern void _write_log_line(WFile *fp, const Logger *logger, Str8 msg, LogLevel level);
void wapp_log_set_level(LogLevel level) {
LOG_CONFIG.level = level;
}
void wapp_log_configure(WFile *outlog, WFile *errlog, LogLevel level) {
LOG_CONFIG.outlog = outlog;
LOG_CONFIG.errlog = errlog;
LOG_CONFIG.level = level;
}
Logger wapp_log_make_logger(Str8 name) {
return (Logger){ .name = name };
}
void wapp_log_debug(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_DEBUG) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
_write_log_line(fp, logger, msg, WAPP_LOG_DEBUG);
}
void wapp_log_info(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_INFO) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
_write_log_line(fp, logger, msg, WAPP_LOG_INFO);
}
void wapp_log_warning(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_WARNING) { return; }
WFile *fp = LOG_CONFIG.outlog != NULL ? LOG_CONFIG.outlog : wapp_file_stdout();
_write_log_line(fp, logger, msg, WAPP_LOG_WARNING);
}
void wapp_log_error(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_ERROR) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
_write_log_line(fp, logger, msg, WAPP_LOG_ERROR);
}
void wapp_log_critical(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_CRITICAL) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
_write_log_line(fp, logger, msg, WAPP_LOG_CRITICAL);
}
void wapp_log_fatal(const Logger *logger, Str8 msg) {
wapp_debug_assert(logger != NULL, "`logger` should not be NULL");
if (LOG_CONFIG.level < WAPP_LOG_FATAL) { return; }
WFile *fp = LOG_CONFIG.errlog != NULL ? LOG_CONFIG.errlog : wapp_file_stderr();
_write_log_line(fp, logger, msg, WAPP_LOG_FATAL);
}
wapp_intern void _write_log_line(WFile *fp, const Logger *logger, Str8 msg, LogLevel level) {
wapp_file_write((void *)LOG_LEVEL_STRINGS[level].buf, fp, LOG_LEVEL_STRINGS[level].size);
wapp_file_write((void *)logger->name.buf, fp, logger->name.size);
wapp_file_write((void *)": ", fp, 2);
wapp_file_write((void *)msg.buf, fp, msg.size);
wapp_file_write((void *)"\n", fp, 1);
}
+34
View File
@@ -0,0 +1,34 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef LOG_H
#define LOG_H
#include "../os/file/file.h"
#include "../base/strings/str8/str8.h"
typedef enum {
WAPP_LOG_FATAL,
WAPP_LOG_CRITICAL,
WAPP_LOG_ERROR,
WAPP_LOG_WARNING,
WAPP_LOG_INFO,
WAPP_LOG_DEBUG,
COUNT_LOG_LEVEL,
} LogLevel;
typedef struct {
Str8 name;
} Logger;
void wapp_log_set_level(LogLevel level);
void wapp_log_configure(WFile *outlog, WFile *errlog, LogLevel level);
Logger wapp_log_make_logger(Str8 name);
void wapp_log_debug(const Logger *logger, Str8 msg);
void wapp_log_info(const Logger *logger, Str8 msg);
void wapp_log_warning(const Logger *logger, Str8 msg);
void wapp_log_error(const Logger *logger, Str8 msg);
void wapp_log_critical(const Logger *logger, Str8 msg);
void wapp_log_fatal(const Logger *logger, Str8 msg);
#endif // !LOG_H
+10
View File
@@ -0,0 +1,10 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef WAPP_LOG_C
#define WAPP_LOG_C
#include "log.c"
#include "../base/wapp_base.c"
#include "../os/wapp_os.c"
#endif // !WAPP_LOG_C
+11
View File
@@ -0,0 +1,11 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef WAPP_LOG_H
#define WAPP_LOG_H
#include "log.h"
#include "../common/wapp_common.h"
#include "../base/wapp_base.h"
#include "../os/wapp_os.h"
#endif // !WAPP_LOG_H
+12 -3
View File
@@ -12,9 +12,6 @@ BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP #endif // !WAPP_PLATFORM_CPP
typedef struct WFile WFile; typedef struct WFile WFile;
// wapp_extern WFile *WF_STDIN;
// wapp_extern WFile *WF_STDOUT;
// wapp_extern WFile *WF_STDERR;
typedef enum { typedef enum {
WAPP_ACCESS_READ, // Equivalent to r WAPP_ACCESS_READ, // Equivalent to r
@@ -37,6 +34,18 @@ typedef enum {
FILE_SEEK_ORIGIN_COUNT, FILE_SEEK_ORIGIN_COUNT,
} FileSeekOrigin; } FileSeekOrigin;
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stdin to get the standard input stream
wapp_extern WFile *wapp_file_stdin(void);
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stdout to get the standard output stream
wapp_extern WFile *wapp_file_stdout(void);
// Return value should not be cached as it's not guaranteed to remain the same. Always call
// wapp_file_stderr to get the standard error stream
wapp_extern WFile *wapp_file_stderr(void);
WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode); WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode);
i64 wapp_file_get_current_position(WFile *file); i64 wapp_file_get_current_position(WFile *file);
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin); i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
+15
View File
@@ -49,6 +49,21 @@ wapp_intern i32 file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_END] = SEEK_END, [WAPP_SEEK_END] = SEEK_END,
}; };
WFile *wapp_file_stdin(void) {
wapp_persist WFile _stdin = { .fd = STDIN_FILENO };
return &_stdin;
}
WFile *wapp_file_stdout(void) {
wapp_persist WFile _stdout = { .fd = STDOUT_FILENO };
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wapp_persist WFile _stderr = { .fd = STDERR_FILENO };
return &_stderr;
}
WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) { WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0}; wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX); memset(tmp, 0, WAPP_PATH_MAX);
+18
View File
@@ -54,6 +54,24 @@ wapp_intern DWORD file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
[WAPP_SEEK_END] = FILE_END, [WAPP_SEEK_END] = FILE_END,
}; };
WFile *wapp_file_stdin(void) {
wapp_persist WFile _stdin = { .fh = INVALID_HANDLE_VALUE };
_stdin.fh = GetStdHandle(STD_INPUT_HANDLE);
return &_stdin;
}
WFile *wapp_file_stdout(void) {
wapp_persist WFile _stdout = { .fh = INVALID_HANDLE_VALUE };
_stdout.fh = GetStdHandle(STD_OUTPUT_HANDLE);
return &_stdout;
}
WFile *wapp_file_stderr(void) {
wapp_persist WFile _stderr = { .fh = INVALID_HANDLE_VALUE };
_stderr.fh = GetStdHandle(STD_ERROR_HANDLE);
return &_stderr;
}
WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) { WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0}; wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
memset(tmp, 0, WAPP_PATH_MAX); memset(tmp, 0, WAPP_PATH_MAX);
+2
View File
@@ -12,6 +12,8 @@
BEGIN_C_LINKAGE BEGIN_C_LINKAGE
#endif // !WAPP_PLATFORM_CPP #endif // !WAPP_PLATFORM_CPP
// TODO (Abdelrahman): Look into moving away from stdio in the implementation
void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour); void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour);
void wapp_shell_termcolour_clear_colour(void); void wapp_shell_termcolour_clear_colour(void);
+2 -1
View File
@@ -6,8 +6,9 @@
#include "wapp.h" #include "wapp.h"
#include "base/wapp_base.c" #include "base/wapp_base.c"
#include "os/wapp_os.c" #include "os/wapp_os.c"
#include "log/wapp_log.c"
#include "prng/wapp_prng.c" #include "prng/wapp_prng.c"
#include "uuid/uuid.c" #include "uuid/wapp_uuid.c"
#include "testing/wapp_testing.c" #include "testing/wapp_testing.c"
#endif // !WAPP_C #endif // !WAPP_C
+1
View File
@@ -6,6 +6,7 @@
#include "common/wapp_common.h" #include "common/wapp_common.h"
#include "base/wapp_base.h" #include "base/wapp_base.h"
#include "os/wapp_os.h" #include "os/wapp_os.h"
#include "log/wapp_log.h"
#include "prng/wapp_prng.h" #include "prng/wapp_prng.h"
#include "uuid/wapp_uuid.h" #include "uuid/wapp_uuid.h"
#include "testing/wapp_testing.h" #include "testing/wapp_testing.h"
+1 -1
View File
@@ -47,7 +47,7 @@ TestFuncResult test_queue_push_alloc(void) {
u64 remaining = wapp_queue_capacity(&queue) - queue.count; u64 remaining = wapp_queue_capacity(&queue) - queue.count;
for (u64 i = 0; i < remaining; ++i) { for (u64 i = 0; i < remaining; ++i) {
item = remaining + i; item = (i32)(remaining + i);
wapp_queue_push(i32, &queue, &item); wapp_queue_push(i32, &queue, &item);
} }
+1 -1
View File
@@ -45,7 +45,7 @@ TestFuncResult test_queue_push_alloc(void) {
u64 remaining = wapp_queue_capacity(&queue) - queue.count; u64 remaining = wapp_queue_capacity(&queue) - queue.count;
for (u64 i = 0; i < remaining; ++i) { for (u64 i = 0; i < remaining; ++i) {
item = remaining + i; item = (i32)(remaining + i);
wapp_queue_push(i32, &queue, &item); wapp_queue_push(i32, &queue, &item);
} }