2020-04-12 22:26:34 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const fmt = std.fmt;
|
2020-07-23 20:47:56 +01:00
|
|
|
const build_options = @import("build_options");
|
2020-07-18 22:46:24 +01:00
|
|
|
const Serial = @import("serial.zig").Serial;
|
|
|
|
const scheduler = @import("scheduler.zig");
|
2020-04-12 22:26:34 +01:00
|
|
|
|
|
|
|
/// The errors that can occur when logging
|
|
|
|
const LoggingError = error{};
|
|
|
|
|
|
|
|
/// The OutStream for the format function
|
|
|
|
const OutStream = std.io.OutStream(void, LoggingError, logCallback);
|
2019-06-22 10:00:57 +01:00
|
|
|
|
2020-07-23 20:47:56 +01:00
|
|
|
/// The serial object where the logs will be written to. This will be a COM serial port.
|
2020-06-04 14:39:37 +01:00
|
|
|
var serial: Serial = undefined;
|
|
|
|
|
2020-06-23 12:43:52 +01:00
|
|
|
///
|
|
|
|
/// The call back function for the std library format function.
|
|
|
|
///
|
|
|
|
/// Arguments:
|
|
|
|
/// context: void - The context of the printing. There isn't a need for a context for this
|
|
|
|
/// so is void.
|
|
|
|
/// str: []const u8 - The string to print to the serial terminal.
|
|
|
|
///
|
|
|
|
/// Return: usize
|
|
|
|
/// The number of bytes written. This will always be the length of the string to print.
|
|
|
|
///
|
|
|
|
/// Error: LoggingError
|
|
|
|
/// {} - No error as LoggingError is empty.
|
|
|
|
///
|
2020-04-12 22:26:34 +01:00
|
|
|
fn logCallback(context: void, str: []const u8) LoggingError!usize {
|
2020-06-04 14:39:37 +01:00
|
|
|
serial.writeBytes(str);
|
2020-04-12 22:26:34 +01:00
|
|
|
return str.len;
|
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:
|
2020-07-23 20:47:56 +01:00
|
|
|
/// IN comptime level: std.log.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: anytype - A struct of the parameters for the format string.
|
2020-01-01 19:12:36 +00:00
|
|
|
///
|
2020-07-23 20:47:56 +01:00
|
|
|
pub fn log(comptime level: std.log.Level, comptime format: []const u8, args: anytype) void {
|
2020-07-18 22:46:24 +01:00
|
|
|
scheduler.taskSwitching(false);
|
2020-04-12 22:26:34 +01:00
|
|
|
fmt.format(OutStream{ .context = {} }, "[" ++ @tagName(level) ++ "] " ++ format, args) catch unreachable;
|
2020-07-18 22:46:24 +01:00
|
|
|
scheduler.taskSwitching(true);
|
2019-06-22 10:00:57 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 14:39:37 +01:00
|
|
|
///
|
|
|
|
/// Initialise the logging stream using the given Serial instance.
|
|
|
|
///
|
|
|
|
/// Arguments:
|
|
|
|
/// IN ser: Serial - The serial instance to use when logging
|
|
|
|
///
|
|
|
|
pub fn init(ser: Serial) void {
|
|
|
|
serial = ser;
|
|
|
|
|
2020-06-23 12:43:52 +01:00
|
|
|
switch (build_options.test_mode) {
|
|
|
|
.Initialisation => runtimeTests(),
|
|
|
|
else => {},
|
|
|
|
}
|
2020-06-04 14:39:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 12:43:52 +01:00
|
|
|
///
|
|
|
|
/// The logging runtime tests that will test all logging levels.
|
|
|
|
///
|
2020-07-18 22:46:24 +01:00
|
|
|
fn runtimeTests() void {
|
2020-07-23 20:47:56 +01:00
|
|
|
inline for (@typeInfo(std.log.Level).Enum.fields) |field| {
|
|
|
|
const level = @field(std.log.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
|
|
|
}
|
|
|
|
}
|