Skip to content

Commit

Permalink
logger: print in comptime or runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Aug 26, 2024
1 parent a313db9 commit 896e5fd
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion imports/logger.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ pub const default_level: sys.esp_log_level_t = switch (@import("builtin").mode)
.ReleaseFast, .ReleaseSmall => .ESP_LOG_ERROR,
};
pub fn ESP_LOG(allocator: std.mem.Allocator, comptime tag: [*:0]const u8, comptime fmt: []const u8, args: anytype) void {
const buffer = std.fmt.allocPrintZ(allocator, fmt, args) catch |err| @panic(@errorName(err));
const buffer = if (isComptime(args))
std.fmt.comptimePrint(fmt, args)
else
std.fmt.allocPrintZ(allocator, fmt, args) catch |err| @panic(@errorName(err));
sys.esp_log_write(default_level, tag, buffer, sys.esp_log_timestamp(), tag);
}
pub const default_color = switch (default_level) {
Expand Down Expand Up @@ -57,3 +60,7 @@ pub const LOG_COLOR_E = LOG_COLOR(LOG_COLOR_RED);
pub const LOG_COLOR_W = LOG_COLOR(LOG_COLOR_BROWN);
pub const LOG_COLOR_I = LOG_COLOR(LOG_COLOR_GREEN);
pub const default_log_scope = .espressif;

inline fn isComptime(val: anytype) bool {
return @typeInfo(@TypeOf(.{val})).Struct.fields[0].is_comptime;
}

0 comments on commit 896e5fd

Please sign in to comment.