Use `root.std_options` to override log behaviour (#99)

ziglang/zig#14181 changed the interface for overriding the log function.
Previously, you would define a public `root.log` function. Now, you
define a `root.std_options` namespace that has a `logFn` decl.

Signed-off-by: Aidan Oldershaw <aidan.oldershaw@gmail.com>
wch-ch32v003
Aidan Oldershaw 2 years ago committed by GitHub
parent db78794f01
commit 1c3e04baa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -48,35 +48,17 @@ pub const debug = @import("debug.zig");
pub const mmio = @import("mmio.zig");
// Allow app to override the os API layer
pub const os = if (@hasDecl(app, "os"))
app.os
else
struct {};
// Allow app to override the panic handler
pub const panic = if (@hasDecl(app, "panic"))
app.panic
else
microzig_panic;
// Conditionally export log_level if the app has it defined.
usingnamespace if (@hasDecl(app, "log_level"))
const options_override = if (@hasDecl(app, "std_options")) app.std_options else struct {};
pub const std_options = struct {
pub usingnamespace options_override;
// Conditionally provide a default no-op logFn if app does not have one
// defined. Parts of microzig use the stdlib logging facility and
// compilations will now fail on freestanding systems that use it but do
// not explicitly set `root.std_options.logFn`
pub usingnamespace if (!@hasDecl(options_override, "logFn"))
struct {
pub const log_level = app.log_level;
}
else
struct {};
// Conditionally export log() if the app has it defined.
pub const log = if (@hasDecl(app, "log"))
app.log
else
// log is a no-op by default. Parts of microzig use the stdlib logging
// facility and compilations will now fail on freestanding systems that
// use it but do not explicitly set `root.log`
struct {
fn log(
pub fn logFn(
comptime message_level: std.log.Level,
comptime scope: @Type(.EnumLiteral),
comptime format: []const u8,
@ -87,7 +69,22 @@ else
_ = format;
_ = args;
}
}.log;
}
else
struct {};
};
// Allow app to override the os API layer
pub const os = if (@hasDecl(app, "os"))
app.os
else
struct {};
// Allow app to override the panic handler
pub const panic = if (@hasDecl(app, "panic"))
app.panic
else
microzig_panic;
/// The microzig default panic handler. Will disable interrupts and loop endlessly.
pub fn microzig_panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {

Loading…
Cancel
Save