clean up logging

This commit is contained in:
Emil Lerch 2025-09-10 17:29:23 -07:00
parent cafeeaf478
commit e8df37f447
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -45,6 +45,11 @@ const DemoHandler = struct {
fn onDetailedError(ctx: *anyopaque, error_info: stt.SttErrorInfo) void {
const self: *DemoHandler = @ptrCast(@alignCast(ctx));
logDetail(self, error_info) catch |e|
std.log.err("Error writing error {}. Original message: {s}", .{ e, error_info.message });
}
fn logDetail(self: *DemoHandler, error_info: stt.SttErrorInfo) !void {
// Categorize the error for statistics
if (error_info.recoverable) {
self.recoverable_error_count += 1;
@ -52,50 +57,57 @@ const DemoHandler = struct {
self.error_count += 1;
}
// Format timestamp
const timestamp = std.time.timestamp();
if (builtin.is_test) return; // Suppress output during tests
// Determine error severity and icon
const SeverityInfo = struct { icon: []const u8, level: []const u8 };
const severity_info: SeverityInfo = switch (error_info.error_code) {
stt.SttError.InternalError => if (error_info.recoverable)
SeverityInfo{ .icon = "", .level = "INFO" }
else
SeverityInfo{ .icon = "", .level = "WARN" },
stt.SttError.OutOfMemory, stt.SttError.ModelLoadError, stt.SttError.InitializationFailed => SeverityInfo{ .icon = "", .level = "ERROR" },
stt.SttError.AudioDeviceError, stt.SttError.AudioDeviceBusy, stt.SttError.AudioDeviceNotFound => if (error_info.recoverable)
SeverityInfo{ .icon = "", .level = "WARN" }
else
SeverityInfo{ .icon = "", .level = "ERROR" },
else => if (error_info.recoverable)
SeverityInfo{ .icon = "", .level = "WARN" }
else
SeverityInfo{ .icon = "", .level = "ERROR" },
};
// Format complete error message in a buffer
var buffer: [2048]u8 = undefined;
var stream = std.io.fixedBufferStream(&buffer);
const writer = stream.writer();
// Print detailed error information
std.debug.print("[{}] {s} {s}: {s}\n", .{ timestamp, severity_info.icon, severity_info.level, error_info.message });
try writer.print("{s}", .{error_info.message});
// Print additional context if available
if (error_info.context) |context| {
std.debug.print(" Context: {s}\n", .{context});
}
if (error_info.context) |context|
try writer.print("\n\tContext: {s}", .{context});
// Print system error if available
if (error_info.system_error) |sys_err| {
std.debug.print(" System Error: {} ({any})\n", .{ sys_err, error_info.error_code });
}
if (error_info.system_error) |sys_err|
try writer.print("\n\tSystem Error: {} ({any})", .{ sys_err, error_info.error_code });
// Print recovery suggestion if available
if (error_info.recovery_suggestion) |suggestion| {
std.debug.print(" Suggestion: {s}\n", .{suggestion});
}
if (error_info.recovery_suggestion) |suggestion|
try writer.print("\n\tSuggestion: {s}", .{suggestion});
// Print recoverable status
if (error_info.recoverable) {
std.debug.print(" Status: Recoverable - system will attempt to continue\n", .{});
} else {
std.debug.print(" Status: Fatal - intervention may be required\n", .{});
if (error_info.recoverable)
try writer.print("\n\tStatus: Recoverable - system will attempt to continue", .{})
else
try writer.print("\n\tStatus: Fatal - intervention may be required", .{});
const message = stream.getWritten();
// Determine and call appropriate log function once
switch (error_info.error_code) {
stt.SttError.InternalError => if (error_info.recoverable) {
std.log.info("{s}", .{message});
} else {
std.log.warn("{s}", .{message});
},
stt.SttError.OutOfMemory,
stt.SttError.ModelLoadError,
stt.SttError.InitializationFailed,
=> {
std.log.err("{s}", .{message});
},
stt.SttError.AudioDeviceError,
stt.SttError.AudioDeviceBusy,
stt.SttError.AudioDeviceNotFound,
=> if (error_info.recoverable) {
std.log.warn("{s}", .{message});
} else {
std.log.err("{s}", .{message});
},
else => if (error_info.recoverable) {
std.log.warn("{s}", .{message});
} else {
std.log.err("{s}", .{message});
},
}
}