|
1 | 1 | //! LVGL Functions specific to Apache NuttX RTOS
|
| 2 | + |
| 3 | +/// Import the Zig Standard Library |
| 4 | +const std = @import("std"); |
| 5 | +const builtin = @import("builtin"); |
| 6 | + |
| 7 | +/////////////////////////////////////////////////////////////////////////////// |
| 8 | +// Panic Handler |
| 9 | + |
| 10 | +/// Called by Zig when it hits a Panic. We print the Panic Message, Stack Trace and halt. See |
| 11 | +/// https://andrewkelley.me/post/zig-stack-traces-kernel-panic-bare-bones-os.html |
| 12 | +/// https://github.com/ziglang/zig/blob/master/lib/std/builtin.zig#L763-L847 |
| 13 | +pub fn panic(message: []const u8, _stack_trace: ?*std.builtin.StackTrace) noreturn { |
| 14 | + // Print the Panic Message |
| 15 | + _ = _stack_trace; |
| 16 | + _ = puts("\n!ZIG PANIC!"); |
| 17 | + _ = puts(@ptrCast([*c]const u8, message)); |
| 18 | + |
| 19 | + // Print the Stack Trace |
| 20 | + _ = puts("Stack Trace:"); |
| 21 | + var it = std.debug.StackIterator.init(@returnAddress(), null); |
| 22 | + while (it.next()) |return_address| { |
| 23 | + _ = printf("%p\n", return_address); |
| 24 | + } |
| 25 | + |
| 26 | + // Halt |
| 27 | + while (true) {} |
| 28 | +} |
| 29 | + |
| 30 | +/////////////////////////////////////////////////////////////////////////////// |
| 31 | +// Logging |
| 32 | + |
| 33 | +/// Called by Zig for `std.log.debug`, `std.log.info`, `std.log.err`, ... |
| 34 | +/// https://gist.github.com/leecannon/d6f5d7e5af5881c466161270347ce84d |
| 35 | +pub fn log( |
| 36 | + comptime _message_level: std.log.Level, |
| 37 | + comptime _scope: @Type(.EnumLiteral), |
| 38 | + comptime format: []const u8, |
| 39 | + args: anytype, |
| 40 | +) void { |
| 41 | + _ = _message_level; |
| 42 | + _ = _scope; |
| 43 | + |
| 44 | + // Format the message |
| 45 | + var buf: [100]u8 = undefined; // Limit to 100 chars |
| 46 | + var slice = std.fmt.bufPrint(&buf, format, args) catch { |
| 47 | + _ = puts("*** log error: buf too small"); |
| 48 | + return; |
| 49 | + }; |
| 50 | + |
| 51 | + // Terminate the formatted message with a null |
| 52 | + var buf2: [buf.len + 1:0]u8 = undefined; |
| 53 | + std.mem.copy(u8, buf2[0..slice.len], slice[0..slice.len]); |
| 54 | + buf2[slice.len] = 0; |
| 55 | + |
| 56 | + // Print the formatted message |
| 57 | + _ = puts(&buf2); |
| 58 | +} |
| 59 | + |
| 60 | +/////////////////////////////////////////////////////////////////////////////// |
| 61 | +// Imported Functions and Variables |
| 62 | + |
| 63 | +/// For safety, we import these functions ourselves to enforce Null-Terminated Strings. |
| 64 | +/// We changed `[*c]const u8` to `[*:0]const u8` |
| 65 | +extern fn printf(format: [*:0]const u8, ...) c_int; |
| 66 | +extern fn puts(str: [*:0]const u8) c_int; |
| 67 | + |
| 68 | +/// Aliases for Zig Standard Library |
| 69 | +const assert = std.debug.assert; |
| 70 | +const debug = std.log.debug; |
0 commit comments