refactor: syslog viewer - update references to klog to syslog

This commit is contained in:
daniel committed 2026-10-01 10:14:12 +02:00
1 parent 63b4bd51e8
commit 7f12e1a150
2 files changed
+76 -77

No files matched your search

+1 -1
View File
@@ -12,4 +12,4 @@
#pragma once #pragma once
#define MONTAUK_BUILD_NUMBER 267 #define MONTAUK_BUILD_NUMBER 270
+75 -76
View File
@@ -1,7 +1,6 @@
/* /*
* main.cpp * main.cpp
* MontaukOS Kernel Log - standalone Window Server app * MontaukOS System Log viewer
* Preserves the old desktop-integrated log viewer layout
* Copyright (c) 2026 Daniel Hammer * Copyright (c) 2026 Daniel Hammer
*/ */
@@ -23,9 +22,9 @@ static constexpr int INIT_H = 449;
static constexpr int KLOG_READ_SIZE = 65536; static constexpr int KLOG_READ_SIZE = 65536;
static constexpr int KLOG_POLL_MS = 250; static constexpr int KLOG_POLL_MS = 250;
struct KlogState { struct SyslogState {
TerminalState term; TerminalState term;
char* klog_buf; char* syslog_buf;
int last_len; int last_len;
char last_tail_byte; char last_tail_byte;
uint64_t last_poll_ms; uint64_t last_poll_ms;
@@ -33,14 +32,14 @@ struct KlogState {
}; };
static WsWindow g_win; static WsWindow g_win;
static KlogState g_klog = {}; static SyslogState g_syslog = {};
static void klog_refeed(int n) { static void syslog_refeed(int n) {
int lines_needed = g_klog.term.rows + g_klog.term.max_scrollback; int lines_needed = g_syslog.term.rows + g_syslog.term.max_scrollback;
int start = 0; int start = 0;
int line_count = 0; int line_count = 0;
for (int i = n - 1; i >= 0; i--) { for (int i = n - 1; i >= 0; i--) {
if (g_klog.klog_buf[i] == '\n') { if (g_syslog.syslog_buf[i] == '\n') {
line_count++; line_count++;
if (line_count >= lines_needed) { if (line_count >= lines_needed) {
start = i + 1; start = i + 1;
@@ -49,73 +48,73 @@ static void klog_refeed(int n) {
} }
} }
g_klog.term.scrollback_lines = 0; g_syslog.term.scrollback_lines = 0;
g_klog.term.view_offset = 0; g_syslog.term.view_offset = 0;
g_klog.term.cursor_x = 0; g_syslog.term.cursor_x = 0;
g_klog.term.cursor_y = 0; g_syslog.term.cursor_y = 0;
g_klog.term.current_fg = colors::TERM_FG; g_syslog.term.current_fg = colors::TERM_FG;
g_klog.term.current_bg = colors::TERM_BG; g_syslog.term.current_bg = colors::TERM_BG;
TermCell* screen = term_screen_row(&g_klog.term, 0); TermCell* screen = term_screen_row(&g_syslog.term, 0);
int total = g_klog.term.cols * g_klog.term.rows; int total = g_syslog.term.cols * g_syslog.term.rows;
for (int i = 0; i < total; i++) for (int i = 0; i < total; i++)
screen[i] = {' ', colors::TERM_FG, colors::TERM_BG}; screen[i] = {' ', colors::TERM_FG, colors::TERM_BG};
terminal_feed(&g_klog.term, g_klog.klog_buf + start, n - start); terminal_feed(&g_syslog.term, g_syslog.syslog_buf + start, n - start);
} }
static bool klog_poll() { static bool syslog_poll() {
uint64_t now = montauk::get_milliseconds(); uint64_t now = montauk::get_milliseconds();
if (now - g_klog.last_poll_ms < KLOG_POLL_MS) return false; if (now - g_syslog.last_poll_ms < KLOG_POLL_MS) return false;
g_klog.last_poll_ms = now; g_syslog.last_poll_ms = now;
int n = (int)montauk::read_log(g_klog.klog_buf, KLOG_READ_SIZE); int n = (int)montauk::read_log(g_syslog.syslog_buf, KLOG_READ_SIZE);
if (n <= 0 && g_klog.last_len <= 0) return false; if (n <= 0 && g_syslog.last_len <= 0) return false;
if (n > g_klog.last_len) { if (n > g_syslog.last_len) {
terminal_feed(&g_klog.term, g_klog.klog_buf + g_klog.last_len, n - g_klog.last_len); terminal_feed(&g_syslog.term, g_syslog.syslog_buf + g_syslog.last_len, n - g_syslog.last_len);
} else if (n == g_klog.last_len && n > 0) { } else if (n == g_syslog.last_len && n > 0) {
if (g_klog.klog_buf[n - 1] != g_klog.last_tail_byte) { if (g_syslog.syslog_buf[n - 1] != g_syslog.last_tail_byte) {
klog_refeed(n); syslog_refeed(n);
} else { } else {
return false; return false;
} }
} else if (n < g_klog.last_len) { } else if (n < g_syslog.last_len) {
klog_refeed(n); syslog_refeed(n);
} }
g_klog.last_len = n; g_syslog.last_len = n;
if (n > 0) g_klog.last_tail_byte = g_klog.klog_buf[n - 1]; if (n > 0) g_syslog.last_tail_byte = g_syslog.syslog_buf[n - 1];
return true; return true;
} }
static bool klog_render() { static bool syslog_render() {
if (!g_win.pixels || !g_klog.term.cells) return false; if (!g_win.pixels || !g_syslog.term.cells) return false;
int new_cols = g_win.width / mono_cell_width(); int new_cols = g_win.width / mono_cell_width();
int new_rows = g_win.height / mono_cell_height(); int new_rows = g_win.height / mono_cell_height();
if (new_cols < 1) new_cols = 1; if (new_cols < 1) new_cols = 1;
if (new_rows < 1) new_rows = 1; if (new_rows < 1) new_rows = 1;
if (new_cols != g_klog.term.cols || new_rows != g_klog.term.rows) { if (new_cols != g_syslog.term.cols || new_rows != g_syslog.term.rows) {
terminal_resize(&g_klog.term, new_cols, new_rows); terminal_resize(&g_syslog.term, new_cols, new_rows);
if (g_klog.last_len > 0) if (g_syslog.last_len > 0)
klog_refeed(g_klog.last_len); syslog_refeed(g_syslog.last_len);
} }
if (g_win.pixels != g_klog.last_pixels) { if (g_win.pixels != g_syslog.last_pixels) {
g_klog.last_pixels = g_win.pixels; g_syslog.last_pixels = g_win.pixels;
g_klog.term.dirty = true; g_syslog.term.dirty = true;
} }
if (!g_klog.term.dirty) return false; if (!g_syslog.term.dirty) return false;
bool was_dirty = g_klog.term.dirty; bool was_dirty = g_syslog.term.dirty;
terminal_render(&g_klog.term, g_win.pixels, g_win.width, g_win.height); terminal_render(&g_syslog.term, g_win.pixels, g_win.width, g_win.height);
if (was_dirty && g_klog.term.scrollback_lines > 0) { if (was_dirty && g_syslog.term.scrollback_lines > 0) {
int cell_h = mono_cell_height(); int cell_h = mono_cell_height();
int total_rows = g_klog.term.scrollback_lines + g_klog.term.rows; int total_rows = g_syslog.term.scrollback_lines + g_syslog.term.rows;
int total_h = total_rows * cell_h; int total_h = total_rows * cell_h;
int view_h = g_win.height; int view_h = g_win.height;
if (total_h > view_h) { if (total_h > view_h) {
@@ -124,8 +123,8 @@ static bool klog_render() {
int sb_x = g_win.width - sb_w - 2; int sb_x = g_win.width - sb_w - 2;
int thumb_h = (view_h * view_h) / total_h; int thumb_h = (view_h * view_h) / total_h;
if (thumb_h < 20) thumb_h = 20; if (thumb_h < 20) thumb_h = 20;
int scroll_from_top = g_klog.term.scrollback_lines - g_klog.term.view_offset; int scroll_from_top = g_syslog.term.scrollback_lines - g_syslog.term.view_offset;
int max_scroll = g_klog.term.scrollback_lines; int max_scroll = g_syslog.term.scrollback_lines;
int thumb_y = (max_scroll > 0) int thumb_y = (max_scroll > 0)
? (scroll_from_top * (view_h - thumb_h)) / max_scroll : 0; ? (scroll_from_top * (view_h - thumb_h)) / max_scroll : 0;
c.fill_rect(sb_x, 0, sb_w, view_h, Color::from_rgb(0x33, 0x33, 0x33)); c.fill_rect(sb_x, 0, sb_w, view_h, Color::from_rgb(0x33, 0x33, 0x33));
@@ -136,22 +135,22 @@ static bool klog_render() {
return true; return true;
} }
static void klog_handle_mouse(const montauk::abi::WinEvent& ev) { static void syslog_handle_mouse(const montauk::abi::WinEvent& ev) {
if (ev.mouse.scroll == 0) return; if (ev.mouse.scroll == 0) return;
g_klog.term.view_offset += (ev.mouse.scroll < 0) ? 3 : -3; g_syslog.term.view_offset += (ev.mouse.scroll < 0) ? 3 : -3;
if (g_klog.term.view_offset < 0) g_klog.term.view_offset = 0; if (g_syslog.term.view_offset < 0) g_syslog.term.view_offset = 0;
if (g_klog.term.view_offset > g_klog.term.scrollback_lines) if (g_syslog.term.view_offset > g_syslog.term.scrollback_lines)
g_klog.term.view_offset = g_klog.term.scrollback_lines; g_syslog.term.view_offset = g_syslog.term.scrollback_lines;
g_klog.term.dirty = true; g_syslog.term.dirty = true;
} }
static void klog_cleanup() { static void syslog_cleanup() {
if (g_klog.term.cells) montauk::free(g_klog.term.cells); if (g_syslog.term.cells) montauk::free(g_syslog.term.cells);
if (g_klog.term.alt_cells) montauk::free(g_klog.term.alt_cells); if (g_syslog.term.alt_cells) montauk::free(g_syslog.term.alt_cells);
if (g_klog.term.render_cells) montauk::free(g_klog.term.render_cells); if (g_syslog.term.render_cells) montauk::free(g_syslog.term.render_cells);
if (g_klog.klog_buf) montauk::mfree(g_klog.klog_buf); if (g_syslog.syslog_buf) montauk::mfree(g_syslog.syslog_buf);
montauk::memset(&g_klog, 0, sizeof(g_klog)); montauk::memset(&g_syslog, 0, sizeof(g_syslog));
} }
extern "C" void _start() { extern "C" void _start() {
@@ -166,22 +165,22 @@ extern "C" void _start() {
if (cols < 1) cols = 1; if (cols < 1) cols = 1;
if (rows < 1) rows = 1; if (rows < 1) rows = 1;
terminal_init_cells(&g_klog.term, cols, rows, TERM_MAX_SCROLLBACK); terminal_init_cells(&g_syslog.term, cols, rows, TERM_MAX_SCROLLBACK);
if (!g_klog.term.cells || !g_klog.term.alt_cells) { if (!g_syslog.term.cells || !g_syslog.term.alt_cells) {
g_win.destroy(); g_win.destroy();
montauk::exit(1); montauk::exit(1);
} }
g_klog.klog_buf = (char*)montauk::malloc(KLOG_READ_SIZE); g_syslog.syslog_buf = (char*)montauk::malloc(KLOG_READ_SIZE);
if (!g_klog.klog_buf) { if (!g_syslog.syslog_buf) {
klog_cleanup(); syslog_cleanup();
g_win.destroy(); g_win.destroy();
montauk::exit(1); montauk::exit(1);
} }
g_klog.last_pixels = g_win.pixels; g_syslog.last_pixels = g_win.pixels;
int n = (int)montauk::read_log(g_klog.klog_buf, KLOG_READ_SIZE); int n = (int)montauk::read_log(g_syslog.syslog_buf, KLOG_READ_SIZE);
/* read_log can return CapabilityDenied or BadPointer */ /* read_log can return CapabilityDenied or BadPointer */
if (n < 0) { if (n < 0) {
@@ -191,22 +190,22 @@ extern "C" void _start() {
gui::dialogs::MESSAGE_BOX_OK gui::dialogs::MESSAGE_BOX_OK
); );
klog_cleanup(); syslog_cleanup();
g_win.destroy(); g_win.destroy();
montauk::exit(-(n)); montauk::exit(-(n));
} }
if (n > 0) { if (n > 0) {
klog_refeed(n); syslog_refeed(n);
g_klog.last_len = n; g_syslog.last_len = n;
g_klog.last_tail_byte = g_klog.klog_buf[n - 1]; g_syslog.last_tail_byte = g_syslog.syslog_buf[n - 1];
} }
if (klog_render()) if (syslog_render())
g_win.present(); g_win.present();
while (true) { while (true) {
bool redraw = klog_poll(); bool redraw = syslog_poll();
montauk::abi::WinEvent ev; montauk::abi::WinEvent ev;
bool quit = false; bool quit = false;
@@ -221,22 +220,22 @@ extern "C" void _start() {
} }
if (ev.type == 1) { if (ev.type == 1) {
klog_handle_mouse(ev); syslog_handle_mouse(ev);
} else if (ev.type == 2 || ev.type == 4) { } else if (ev.type == 2 || ev.type == 4) {
g_klog.term.dirty = true; g_syslog.term.dirty = true;
} }
} }
if (r < 0 || quit) break; if (r < 0 || quit) break;
if (redraw && klog_render()) if (redraw && syslog_render())
g_win.present(); g_win.present();
if (r == 0) if (r == 0)
montauk::sleep_ms(16); montauk::sleep_ms(16);
} }
klog_cleanup(); syslog_cleanup();
g_win.destroy(); g_win.destroy();
montauk::exit(0); montauk::exit(0);
} }