Skip to content

Logging rewrite#92

Draft
Krzychuo wants to merge 4 commits intoOperacja-System:mainfrom
Krzychuo:ring-buffer-logging
Draft

Logging rewrite#92
Krzychuo wants to merge 4 commits intoOperacja-System:mainfrom
Krzychuo:ring-buffer-logging

Conversation

@Krzychuo
Copy link
Copy Markdown

@Krzychuo Krzychuo commented Mar 23, 2026

Work in progress.

Remaining things to do:

  • Simple UART interface
  • Flushing the ring buffer
  • Format the code
  • Proper error handling

@Kamilosok
Copy link
Copy Markdown
Collaborator

Consider renaming this to just 'Rewriting logging' or 'Logging rewrite' and just update the status with a checklist or comments

@Kamilosok Kamilosok added documentation Improvements or additions to documentation enhancement and removed documentation Improvements or additions to documentation labels Mar 24, 2026
@frogrammer9 frogrammer9 added rework Changes improving how existing code works and removed enhancement labels Mar 27, 2026
@Krzychuo Krzychuo changed the title Started rewriting logging Logging rewrite Mar 29, 2026
@Krzychuo Krzychuo force-pushed the ring-buffer-logging branch 10 times, most recently from d86385d to 51f9739 Compare March 29, 2026 21:47
@Krzychuo Krzychuo force-pushed the ring-buffer-logging branch from 51f9739 to f8261f5 Compare March 29, 2026 21:48
Copy link
Copy Markdown
Contributor

@frogrammer9 frogrammer9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UART is a specific protocol, while ‘serial’ is the more generic term for this type of communication interface. I would prefer the latter name to be used.

Comment thread src/kernel/logging/klog.c
for (u32 i = 0; i < g_indent_level && written < TEMP_BUF_SIZE - 1; i++) temp_buffer[written++] = ' ';

const char* prefix = g_prefixes[loglvl];
for (size_t i = 0; prefix[i] && written < TEMP_BUF_SIZE - 1; i++) temp_buffer[written++] = prefix[i];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a space after the loglvl prefix.

Comment thread src/kernel/logging/klog.c

static u32 g_indent_level = 0;
static const char* g_prefixes[] = {"[ERROR]", "[WARNING]", "[ ]", "[~]"};
static const char* g_overflow_warning = "[WARNING]Some of the logs were lost due to overflow";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a space after the loglvl prefix.

Comment thread src/kernel/logging/klog.c
static const char* g_overflow_warning = "[WARNING]Some of the logs were lost due to overflow";

#ifdef __DEBUG__
#define RING_BUF_SIZE (1 << 20)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point of using macros, just use const size_t RING_BUF_SIZE = ...;

Comment thread src/kernel/logging/klog.c
i32 write;
bool full;
bool overflown;
void (*uart_tx)(char c);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be a part of the ring buffer structure.

Comment thread src/kernel/logging/klog.c

static ring_buffer_t g_buffer;

static ring_buffer_t* get_buffer() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function necessary?

Comment thread src/kernel/logging/klog.c
while (str[it]) put_char(rb, str[it++]);
}

static void put_overflow_warning(ring_buffer_t* rb) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point in copying this message to the ring buffer since it is stored already in static memory.
When serial or tty is being initialized simply check if ring buffer has overflown and display this message before dumping the ring buffer contents.

Comment thread src/kernel/logging/klog.c
DEBUG_PRINTF("%s ", g_prefixes[loglvl]);
DEBUG_VPRINTF(fmt, va);
size_t written = 0;
for (u32 i = 0; i < g_indent_level && written < TEMP_BUF_SIZE - 1; i++) temp_buffer[written++] = ' ';
Copy link
Copy Markdown
Contributor

@frogrammer9 frogrammer9 Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storing '\t' chars in the ring buffer can be potentially wasteful. ASCII values between 0 and 8 are not and will be not important for logger so we can store those values and when printing interpret then as the size of the indentation.

Comment thread src/kernel/logging/klog.h
KLSL_TRACE = 3,
} klog_severity_level_t;

char ring_buffer_get_char();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existence of the ring buffer is some unimportant implementation detail for the logger user, there is no point in providing this information. Those function should be named something like "klog_flush_serial" "klog_set_serial_handler", the functionality of getting chars from the buffer is unnecessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rework Changes improving how existing code works

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants