2020-06-04 14:39:37 +01:00
|
|
|
const build_options = @import("build_options");
|
2020-04-12 22:26:34 +01:00
|
|
|
const std = @import("std");
|
2020-06-04 14:39:37 +01:00
|
|
|
const Serial = @import("serial.zig").Serial;
|
2020-04-12 22:26:34 +01:00
|
|
|
const fmt = std.fmt;
|
|
|
|
|
|
|
|
/// 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-06-23 12:43:52 +01:00
|
|
|
/// The different levels of logging that can be outputted.
|
2019-06-22 10:00:57 +01:00
|
|
|
pub const Level = enum {
|
|
|
|
INFO,
|
|
|
|
DEBUG,
|
|
|
|
WARNING,
|
2019-09-08 20:48:23 +01:00
|
|
|
ERROR,
|
2019-06-22 10:00:57 +01:00
|
|
|
};
|
|
|
|
|
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-06-23 12:43:52 +01:00
|
|
|
/// 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.
|
2020-01-01 19:12:36 +00:00
|
|
|
/// 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 {
|
2020-04-12 22:26:34 +01:00
|
|
|
fmt.format(OutStream{ .context = {} }, "[" ++ @tagName(level) ++ "] " ++ format, args) catch unreachable;
|
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 the INFO level.
|
|
|
|
///
|
|
|
|
/// Arguments:
|
2020-06-23 12:43:52 +01:00
|
|
|
/// IN comptime format: []const u8 - The message format. Uses the standard format specification
|
|
|
|
/// options.
|
2020-01-01 19:12:36 +00:00
|
|
|
/// 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:
|
2020-06-23 12:43:52 +01:00
|
|
|
/// IN comptime format: []const u8 - The message format. Uses the standard format specification
|
|
|
|
/// options.
|
2020-01-01 19:12:36 +00:00
|
|
|
/// 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);
|
|
|
|
}
|
2019-09-08 20:48:23 +01:00
|
|
|
|
2020-01-01 19:12:36 +00:00
|
|
|
///
|
|
|
|
/// Write a message to the log output stream with the WARNING level.
|
|
|
|
///
|
|
|
|
/// Arguments:
|
2020-06-23 12:43:52 +01:00
|
|
|
/// IN comptime format: []const u8 - The message format. Uses the standard format specification
|
|
|
|
/// options.
|
2020-01-01 19:12:36 +00:00
|
|
|
/// 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);
|
|
|
|
}
|
2019-09-08 20:48:23 +01:00
|
|
|
|
2020-01-01 19:12:36 +00:00
|
|
|
///
|
|
|
|
/// Write a message to the log output stream with the ERROR level.
|
|
|
|
///
|
|
|
|
/// Arguments:
|
2020-06-23 12:43:52 +01:00
|
|
|
/// IN comptime format: []const u8 - The message format. Uses the standard format specification
|
|
|
|
/// options.
|
2020-01-01 19:12:36 +00:00
|
|
|
/// 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
|
|
|
|
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.
|
|
|
|
///
|
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
|
|
|
}
|
|
|
|
}
|