Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ea689e7357 | |||
| a97b8f742a | |||
| 99902cfa99 |
+50
-15
@@ -1,13 +1,19 @@
|
|||||||
// vim:fileencoding=utf-8:foldmethod=marker
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "../base/strings/str8/str8.h"
|
||||||
#include "../common/aliases/aliases.h"
|
#include "../common/aliases/aliases.h"
|
||||||
#include "../common/assert/assert.h"
|
#include "../common/assert/assert.h"
|
||||||
#include "../os/file/file.h"
|
|
||||||
#include "../common/misc/misc_utils.h"
|
#include "../common/misc/misc_utils.h"
|
||||||
|
#include "../os/file/file.h"
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define LOG_LEVEL_STR_LENGTH 8
|
#define MIN_LOG_MSG_LENGTH 32
|
||||||
#define LOG_PREFIX_BUF_LENGTH 16
|
#define TIME_BUF_CAPACITY 70
|
||||||
|
|
||||||
|
wapp_intern Str8RO L_BRACKET = wapp_str8_lit_ro("[");
|
||||||
|
wapp_intern Str8RO R_BRACKET_SPACE = wapp_str8_lit_ro("] ");
|
||||||
|
wapp_intern Str8RO R_BRACKET_NEWLINE = wapp_str8_lit_ro("]\n");
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
WFile *outlog;
|
WFile *outlog;
|
||||||
@@ -21,14 +27,15 @@ wapp_intern LogConfig LOG_CONFIG = {
|
|||||||
.level = WAPP_LOG_DEBUG,
|
.level = WAPP_LOG_DEBUG,
|
||||||
};
|
};
|
||||||
wapp_intern Str8RO LOG_LEVEL_STRINGS[COUNT_LOG_LEVEL] = {
|
wapp_intern Str8RO LOG_LEVEL_STRINGS[COUNT_LOG_LEVEL] = {
|
||||||
[WAPP_LOG_FATAL] = wapp_str8_lit_ro_initialiser_list("[ FATAL ] "),
|
[WAPP_LOG_FATAL] = wapp_str8_lit_ro_initialiser_list("fatal "),
|
||||||
[WAPP_LOG_CRITICAL] = wapp_str8_lit_ro_initialiser_list("[ CRITICAL ] "),
|
[WAPP_LOG_CRITICAL] = wapp_str8_lit_ro_initialiser_list("critical "),
|
||||||
[WAPP_LOG_ERROR] = wapp_str8_lit_ro_initialiser_list("[ ERROR ] "),
|
[WAPP_LOG_ERROR] = wapp_str8_lit_ro_initialiser_list("error "),
|
||||||
[WAPP_LOG_WARNING] = wapp_str8_lit_ro_initialiser_list("[ WARNING ] "),
|
[WAPP_LOG_WARNING] = wapp_str8_lit_ro_initialiser_list("warning "),
|
||||||
[WAPP_LOG_INFO] = wapp_str8_lit_ro_initialiser_list("[ INFO ] "),
|
[WAPP_LOG_INFO] = wapp_str8_lit_ro_initialiser_list("info "),
|
||||||
[WAPP_LOG_DEBUG] = wapp_str8_lit_ro_initialiser_list("[ DEBUG ] "),
|
[WAPP_LOG_DEBUG] = wapp_str8_lit_ro_initialiser_list("debug "),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
wapp_intern void _get_current_time_string(Str8 *dst);
|
||||||
wapp_intern void _write_log_line(WFile *fp, const Logger *logger, Str8 msg, LogLevel level);
|
wapp_intern void _write_log_line(WFile *fp, const Logger *logger, Str8 msg, LogLevel level);
|
||||||
|
|
||||||
void wapp_log_set_level(LogLevel level) {
|
void wapp_log_set_level(LogLevel level) {
|
||||||
@@ -93,10 +100,38 @@ void wapp_log_fatal(const Logger *logger, Str8 msg) {
|
|||||||
_write_log_line(fp, logger, msg, WAPP_LOG_FATAL);
|
_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_intern void _get_current_time_string(Str8 *dst) {
|
||||||
wapp_file_write((void *)LOG_LEVEL_STRINGS[level].buf, fp, LOG_LEVEL_STRINGS[level].size);
|
// TODO (Abdelrahman): Replace with proper date/time utilities
|
||||||
wapp_file_write((void *)logger->name.buf, fp, logger->name.size);
|
char buf[TIME_BUF_CAPACITY];
|
||||||
wapp_file_write((void *)": ", fp, 2);
|
time_t now = time(NULL);
|
||||||
wapp_file_write((void *)msg.buf, fp, msg.size);
|
struct tm utc;
|
||||||
wapp_file_write((void *)"\n", fp, 1);
|
gmtime_r(&now, &utc);
|
||||||
|
strftime(buf, sizeof(buf), "%FT%TZ ", &utc);
|
||||||
|
wapp_str8_copy_cstr_capped(dst, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
wapp_intern void _write_log_line(WFile *fp, const Logger *logger, Str8 msg, LogLevel level) {
|
||||||
|
Str8 padding = wapp_str8_buf(MIN_LOG_MSG_LENGTH);
|
||||||
|
u32 padding_size = msg.size < MIN_LOG_MSG_LENGTH ? MIN_LOG_MSG_LENGTH - msg.size + 1 : 0;
|
||||||
|
wapp_str8_format(&padding, "%-*s", padding_size, " ");
|
||||||
|
|
||||||
|
Str8 time_str = wapp_str8_buf(TIME_BUF_CAPACITY);
|
||||||
|
_get_current_time_string(&time_str);
|
||||||
|
|
||||||
|
Str8RO **strings = wapp_array(
|
||||||
|
Str8RO *,
|
||||||
|
&time_str,
|
||||||
|
&L_BRACKET,
|
||||||
|
&LOG_LEVEL_STRINGS[level],
|
||||||
|
&R_BRACKET_SPACE,
|
||||||
|
&msg,
|
||||||
|
&padding,
|
||||||
|
&L_BRACKET,
|
||||||
|
&logger->name,
|
||||||
|
&R_BRACKET_NEWLINE
|
||||||
|
);
|
||||||
|
|
||||||
|
for (u64 i = 0; i < wapp_array_count(strings); ++i) {
|
||||||
|
wapp_file_write_str8(strings[i], fp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,16 @@ i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count) {
|
|||||||
return _file_write(src_buf, file, byte_count);
|
return _file_write(src_buf, file, byte_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u64 wapp_file_read_str8(Str8 *str, WFile *file) {
|
||||||
|
wapp_debug_assert(str != NULL, "`str` should not be NULL.");
|
||||||
|
return wapp_file_read((void *)(str->buf), file, str->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
i64 wapp_file_write_str8(Str8RO *str, WFile *file) {
|
||||||
|
wapp_debug_assert(str != NULL, "`str` should not be NULL.");
|
||||||
|
return wapp_file_write((void *)(str->buf), file, str->size);
|
||||||
|
}
|
||||||
|
|
||||||
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count) {
|
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count) {
|
||||||
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
||||||
"`dst_buf` and `file` should not be NULL.");
|
"`dst_buf` and `file` should not be NULL.");
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
|
|||||||
i64 wapp_file_get_length(WFile *file);
|
i64 wapp_file_get_length(WFile *file);
|
||||||
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count);
|
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count);
|
||||||
i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count);
|
i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count);
|
||||||
|
u64 wapp_file_read_str8(Str8 *str, WFile *file);
|
||||||
|
i64 wapp_file_write_str8(Str8RO *str, WFile *file);
|
||||||
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count);
|
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count);
|
||||||
i64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count);
|
i64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count);
|
||||||
i32 wapp_file_flush(WFile *file);
|
i32 wapp_file_flush(WFile *file);
|
||||||
|
|||||||
Reference in New Issue
Block a user