Skip to content

Commit

Permalink
Add logging and panic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lupyuen committed Jul 1, 2022
1 parent cd38781 commit ec4d58e
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions lvgltest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub export fn lvgltest_main(
_argc: c_int,
_argv: [*]const [*]const u8
) c_int {
debug("Zig LVGL Test", .{});
_ = _argc;
_ = _argv;
var disp_drv: ?*c.lv_disp_drv_t = c.get_disp_drv();
Expand Down Expand Up @@ -88,3 +89,76 @@ pub fn monitor_cb(arg_disp_drv: ?*c.lv_disp_drv_t, arg_time_1: u32, arg_px: u32)
var px = arg_px;
_ = px;
}

///////////////////////////////////////////////////////////////////////////////
// Panic Handler

/// Called by Zig when it hits a Panic. We print the Panic Message, Stack Trace and halt. See
/// https://andrewkelley.me/post/zig-stack-traces-kernel-panic-bare-bones-os.html
/// https://github.com/ziglang/zig/blob/master/lib/std/builtin.zig#L763-L847
pub fn panic(
message: []const u8,
_stack_trace: ?*std.builtin.StackTrace
) noreturn {
// Print the Panic Message
_ = _stack_trace;
_ = puts("\n!ZIG PANIC!");
_ = puts(@ptrCast([*c]const u8, message));

// Print the Stack Trace
_ = puts("Stack Trace:");
var it = std.debug.StackIterator.init(@returnAddress(), null);
while (it.next()) |return_address| {
_ = printf("%p\n", return_address);
}

// Halt
while(true) {}
}

///////////////////////////////////////////////////////////////////////////////
// Logging

/// Called by Zig for `std.log.debug`, `std.log.info`, `std.log.err`, ...
/// https://gist.github.com/leecannon/d6f5d7e5af5881c466161270347ce84d
pub fn log(
comptime _message_level: std.log.Level,
comptime _scope: @Type(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
_ = _message_level;
_ = _scope;

// Format the message
var buf: [100]u8 = undefined; // Limit to 100 chars
var slice = std.fmt.bufPrint(&buf, format, args)
catch { _ = puts("*** log error: buf too small"); return; };

// Terminate the formatted message with a null
var buf2: [buf.len + 1 : 0]u8 = undefined;
std.mem.copy(
u8,
buf2[0..slice.len],
slice[0..slice.len]
);
buf2[slice.len] = 0;

// Print the formatted message
_ = puts(&buf2);
}

///////////////////////////////////////////////////////////////////////////////
// Imported Functions and Variables

/// For safety, we import these functions ourselves to enforce Null-Terminated Strings.
/// We changed `[*c]const u8` to `[*:0]const u8`
extern fn printf(format: [*:0]const u8, ...) c_int;
extern fn puts(str: [*:0]const u8) c_int;

/// LoRaWAN Event Queue
extern var event_queue: c.struct_ble_npl_eventq;

/// Aliases for Zig Standard Library
const assert = std.debug.assert;
const debug = std.log.debug;

0 comments on commit ec4d58e

Please sign in to comment.