Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 94 additions & 12 deletions src/kernel/logging/klog.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,123 @@
#include "klog.h"

#include <stdbool.h>
#include <stdio.h>

Check failure on line 4 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang-macos-lto

'stdio.h' file not found

Check failure on line 4 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang-macos

'stdio.h' file not found
#include <stdarg.h>
#include <stdbigos/types.h>

// NOTE: In the future this lib should manage logging (formating and outputing to the screen or uart) on its own.
// Right now I will leave it as is, will change once the basic kernel at least runs.
#include "debug/debug_stdio.h"

static u32 g_indent_level = 0;
// static u32 g_indent_level = 0;
static const char* g_prefixes[] = {"[ERROR]", "[WARNING]", "[ ]", "[~]"};
static const char* overwrite_warning = "[WARNING]Some of the logs were lost due to too small size of buffer";

Check warning on line 14 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang

invalid case style for global variable 'overwrite_warning' [readability-identifier-naming]

void klog_indent_increase() {
++g_indent_level;
#ifdef __DEBUG__
#define RING_BUF_SIZE 1<<20
#else
#define RING_BUF_SIZE 1<<16
#endif // !__DEBUG__

#define TEMP_BUF_SIZE 1<<12

typedef struct {
char ring[RING_BUF_SIZE];
i32 read;
i32 write;
bool full;
} ring_buffer;

Check warning on line 29 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang

invalid case style for typedef 'ring_buffer' [readability-identifier-naming]

static ring_buffer buffer;

Check warning on line 31 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang

invalid case style for global variable 'buffer' [readability-identifier-naming]

size_t available_space(const ring_buffer* buf) {
if (buf->full) return 0;
if (buf->write >= buf->read)

Check warning on line 35 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang

statement should be inside braces [readability-braces-around-statements]
return RING_BUF_SIZE - (buf->write - buf->read);
else

Check warning on line 37 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang

statement should be inside braces [readability-braces-around-statements]

Check warning on line 37 in src/kernel/logging/klog.c

View workflow job for this annotation

GitHub Actions / build-clang

do not use 'else' after 'return' [readability-else-after-return]
return buf->read - buf->write;
}

void put_char(ring_buffer* buf, char c){
buf->ring[buf->write++] = c;
buf->write %= RING_BUF_SIZE;
if(buf->full){
buf->read = (buf->read + 1) % RING_BUF_SIZE;
}
buf->full = buf->read == buf->write;
}

void put_string(ring_buffer* buf, const char* str){
size_t it = 0;
while(str[it]){
put_char(buf, str[it++]);
}
}

void klog_indent_decrease() {
if (g_indent_level != 0)
--g_indent_level;
void put_msg(ring_buffer* buf, const char* msg, int msg_len){
if ((size_t)msg_len > available_space(buf))
put_string(buf, overwrite_warning);
put_string(buf, msg);
}

char get_char(ring_buffer* buf){
if(buf->read == buf->write && !buf->full)
return 0;
char ret = buf->ring[buf->read++];
buf->read %= RING_BUF_SIZE;
buf->full = false;
return ret;
}

// void klog_indent_increase() {
// ++g_indent_level;
// }

// void klog_indent_decrease() {
// if (g_indent_level != 0)
// --g_indent_level;
// }

static void klogv(klog_severity_level_t loglvl, const char* fmt, va_list va) {
(void)fmt;
(void)va;
static char temp_buffer[TEMP_BUF_SIZE];

if (loglvl > KLSL_TRACE) {
KLOGLN_ERROR("Invalid loglvl passed to klog");
loglvl = KLSL_ERROR;
}

DEBUG_PUTGAP(g_indent_level);
DEBUG_PRINTF("%s ", g_prefixes[loglvl]);
DEBUG_VPRINTF(fmt, va);
const char* prefix = g_prefixes[loglvl];
size_t prefix_len = 0;
while (prefix[prefix_len]) ++prefix_len;

for (size_t i=0;i<prefix_len;i++)
temp_buffer[i] = prefix[i];

va_list copy;
va_copy(copy, va);
int written = vsnprintf(temp_buffer + prefix_len, sizeof(temp_buffer) - prefix_len, fmt, copy);
va_end(copy);

// TODO: proper error handling
if (written < 0) {
// Format error
written = 0;
} else if ((size_t)written >= sizeof(temp_buffer)) {
// Truncated
written = sizeof(temp_buffer) - 1;
}

put_msg(&buffer, &temp_buffer, written);

// DEBUG_PUTGAP(g_indent_level);
// DEBUG_PRINTF("%s ", g_prefixes[loglvl]);
// DEBUG_VPRINTF(fmt, va);
}

static void kloglnv(klog_severity_level_t loglvl, const char* fmt, va_list va) {
klogv(loglvl, fmt, va);
DEBUG_PUTC('\n');
put_msg(&buffer, "\n", 1);
// DEBUG_PUTC('\n');
}

void klog(klog_severity_level_t loglvl, const char* fmt, ...) {
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/logging/klog.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ typedef enum {
KLSL_TRACE = 3,
} klog_severity_level_t;

void klog_indent_increase();
void klog_indent_decrease();
// void klog_indent_increase();
// void klog_indent_decrease();

[[gnu::format(printf, 2, 3)]]
void klog(klog_severity_level_t loglvl, const char* fmt, ...);
Expand Down
Loading