sylveos

Toy Operating System
Log | Files | Refs

commit a290d940cb0ea1b50e59e0939d72db86ba36bc39
parent 7c036dc0a73394d44b0e7d741d046f86ccfadbb2
Author: Sylvia Ivory <git@sivory.net>
Date:   Sun, 15 Mar 2026 23:40:38 -0700

Implement journal in Zig

Diffstat:
Asylveos/journal.zig | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msylveos/root.zig | 2++
Mtools/src/sylveos/mod.rs | 25+++++++++++++++++++++++++
3 files changed, 89 insertions(+), 0 deletions(-)

diff --git a/sylveos/journal.zig b/sylveos/journal.zig @@ -0,0 +1,62 @@ +const std = @import("std"); +const pi = @import("pi"); + +const uart = pi.devices.mini_uart; + +const JOURNAL_START: u8 = 0x1; +const JOURNAL_START_SENTINEL: u8 = 0x2; +const JOURNAL_END_SENTINEL: u8 = 0x2; + +pub const LogLevel = enum(u8) { + Critical = 0, + Error = 1, + Warning = 2, + Info = 3, + Trace = 4, + Debug = 5, +}; + +pub fn write_slice(level: LogLevel, slice: []const u8) void { + uart.write_byte(JOURNAL_START); + uart.write_byte(@intFromEnum(level)); + + var slice_len: [4]u8 = undefined; + std.mem.writeInt(u32, &slice_len, slice.len, .little); + + uart.write_slice(&slice_len); + uart.write_slice(slice); +} + +pub fn start_entry(level: LogLevel) void { + uart.write_byte(JOURNAL_START_SENTINEL); + uart.write_byte(@intFromEnum(level)); +} + +pub fn end_entry() void { + uart.write_byte(JOURNAL_END_SENTINEL); +} + +pub fn print(level: LogLevel, comptime fmt: []const u8, args: anytype) void { + start_entry(level); + uart.print(fmt, args); + end_entry(); +} + +pub fn critical(comptime fmt: []const u8, args: anytype) void { + print(.Critical, fmt, args); +} +pub fn err(comptime fmt: []const u8, args: anytype) void { + print(.Error, fmt, args); +} +pub fn warn(comptime fmt: []const u8, args: anytype) void { + print(.Warning, fmt, args); +} +pub fn info(comptime fmt: []const u8, args: anytype) void { + print(.Info, fmt, args); +} +pub fn trace(comptime fmt: []const u8, args: anytype) void { + print(.Trace, fmt, args); +} +pub fn debug(comptime fmt: []const u8, args: anytype) void { + print(.Debug, fmt, args); +} diff --git a/sylveos/root.zig b/sylveos/root.zig @@ -4,6 +4,8 @@ const pi = @import("pi"); const memory = @import("./memory.zig"); const loader = @import("./loader.zig"); const syscall = @import("./syscall.zig"); +const journal = @import("./journal.zig"); + const lua_binary = @embedFile("./lua"); const uart = pi.devices.mini_uart; diff --git a/tools/src/sylveos/mod.rs b/tools/src/sylveos/mod.rs @@ -243,5 +243,30 @@ async fn parse_message(port: &mut ReadHalf<SerialStream>) -> Result<Message> { return Ok(Message::Journal(JournalEntry { time, level, msg })); } + if byte == 2 { + // Journal entry based off a sentinel + let time = Local::now(); + let level = journal::LogLevel::from_byte(port.read_u8().await?); + + let mut buffer: Vec<u8> = Vec::new(); + loop { + let byte = port.read_u8().await?; + + if byte == 0 { + return Ok(Message::Disconnect); + } + + if byte == 2 { + break; + } + + buffer.push(byte); + } + + let msg = String::from_utf8_lossy(buffer.as_slice()).to_string(); + + return Ok(Message::Journal(JournalEntry { time, level, msg })); + } + Ok(Message::Console(byte)) }