clean up logging
This commit is contained in:
parent
cafeeaf478
commit
e8df37f447
1 changed files with 50 additions and 38 deletions
90
src/main.zig
90
src/main.zig
|
@ -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" }
|
||||
// Format complete error message in a buffer
|
||||
var buffer: [2048]u8 = undefined;
|
||||
var stream = std.io.fixedBufferStream(&buffer);
|
||||
const writer = stream.writer();
|
||||
|
||||
try writer.print("{s}", .{error_info.message});
|
||||
|
||||
if (error_info.context) |context|
|
||||
try writer.print("\n\tContext: {s}", .{context});
|
||||
|
||||
if (error_info.system_error) |sys_err|
|
||||
try writer.print("\n\tSystem Error: {} ({any})", .{ sys_err, error_info.error_code });
|
||||
|
||||
if (error_info.recovery_suggestion) |suggestion|
|
||||
try writer.print("\n\tSuggestion: {s}", .{suggestion});
|
||||
|
||||
if (error_info.recoverable)
|
||||
try writer.print("\n\tStatus: Recoverable - system will attempt to continue", .{})
|
||||
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" },
|
||||
};
|
||||
try writer.print("\n\tStatus: Fatal - intervention may be required", .{});
|
||||
|
||||
// Print detailed error information
|
||||
std.debug.print("[{}] {s} {s}: {s}\n", .{ timestamp, severity_info.icon, severity_info.level, error_info.message });
|
||||
const message = stream.getWritten();
|
||||
|
||||
// Print additional context if available
|
||||
if (error_info.context) |context| {
|
||||
std.debug.print(" Context: {s}\n", .{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 });
|
||||
}
|
||||
|
||||
// Print recovery suggestion if available
|
||||
if (error_info.recovery_suggestion) |suggestion| {
|
||||
std.debug.print(" Suggestion: {s}\n", .{suggestion});
|
||||
}
|
||||
|
||||
// Print recoverable status
|
||||
if (error_info.recoverable) {
|
||||
std.debug.print(" Status: Recoverable - system will attempt to continue\n", .{});
|
||||
// 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.debug.print(" Status: Fatal - intervention may be required\n", .{});
|
||||
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});
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue