pluto/src/kernel/log.zig

86 lines
2.9 KiB
Zig
Raw Normal View History

2019-06-22 10:00:57 +01:00
const serial = @import("serial.zig");
const fmt = @import("std").fmt;
pub const Level = enum {
INFO,
DEBUG,
WARNING,
ERROR,
2019-06-22 10:00:57 +01:00
};
fn logCallback(context: void, str: []const u8) anyerror!void {
2019-10-08 00:11:50 +01:00
serial.writeBytes(str, serial.Port.COM1);
2019-06-22 10:00:57 +01:00
}
2020-01-01 19:12:36 +00:00
///
/// Write a message to the log output stream with a certain logging level.
///
/// Arguments:
/// IN comptime level: Level - The logging level to use. Determines the message prefix and whether it is filtered.
/// IN comptime format: []const u8 - The message format. Uses the standard format specification options.
/// IN args: var - A struct of the parameters for the format string.
///
pub fn log(comptime level: Level, comptime format: []const u8, args: var) void {
2019-06-22 10:00:57 +01:00
fmt.format({}, anyerror, logCallback, "[" ++ @tagName(level) ++ "] " ++ format, args) catch unreachable;
}
2020-01-01 19:12:36 +00:00
///
/// Write a message to the log output stream with the INFO level.
///
/// Arguments:
/// IN comptime format: []const u8 - The message format. Uses the standard format specification options.
/// IN args: var - A struct of the parameters for the format string.
///
pub fn logInfo(comptime format: []const u8, args: var) void {
2019-06-22 10:00:57 +01:00
log(Level.INFO, format, args);
}
2020-01-01 19:12:36 +00:00
///
/// Write a message to the log output stream with the DEBUG level.
///
/// Arguments:
/// IN comptime format: []const u8 - The message format. Uses the standard format specification options.
/// IN args: var - A struct of the parameters for the format string.
///
pub fn logDebug(comptime format: []const u8, args: var) void {
2019-06-22 10:00:57 +01:00
log(Level.DEBUG, format, args);
}
2020-01-01 19:12:36 +00:00
///
/// Write a message to the log output stream with the WARNING level.
///
/// Arguments:
/// IN comptime format: []const u8 - The message format. Uses the standard format specification options.
/// IN args: var - A struct of the parameters for the format string.
///
pub fn logWarning(comptime format: []const u8, args: var) void {
2019-06-22 10:00:57 +01:00
log(Level.WARNING, format, args);
}
2020-01-01 19:12:36 +00:00
///
/// Write a message to the log output stream with the ERROR level.
///
/// Arguments:
/// IN comptime format: []const u8 - The message format. Uses the standard format specification options.
/// IN args: var - A struct of the parameters for the format string.
///
pub fn logError(comptime format: []const u8, args: var) void {
2019-06-22 10:00:57 +01:00
log(Level.ERROR, format, args);
}
2019-10-08 11:20:37 +01:00
pub fn runtimeTests() void {
inline for (@typeInfo(Level).Enum.fields) |field| {
const level = @field(Level, field.name);
2020-01-01 19:12:36 +00:00
log(level, "Test " ++ field.name ++ " level\n", .{});
log(level, "Test " ++ field.name ++ " level with args {}, {}\n", .{ "a", @as(u32, 1) });
2019-10-08 11:20:37 +01:00
const logFn = switch (level) {
.INFO => logInfo,
.DEBUG => logDebug,
.WARNING => logWarning,
.ERROR => logError,
};
2020-01-01 19:12:36 +00:00
logFn("Test " ++ field.name ++ " function\n", .{});
logFn("Test " ++ field.name ++ " function with args {}, {}\n", .{ "a", @as(u32, 1) });
2019-10-08 11:20:37 +01:00
}
}