clean up logging
This commit is contained in:
parent
cafeeaf478
commit
e8df37f447
1 changed files with 50 additions and 38 deletions
88
src/main.zig
88
src/main.zig
|
@ -45,6 +45,11 @@ const DemoHandler = struct {
|
||||||
fn onDetailedError(ctx: *anyopaque, error_info: stt.SttErrorInfo) void {
|
fn onDetailedError(ctx: *anyopaque, error_info: stt.SttErrorInfo) void {
|
||||||
const self: *DemoHandler = @ptrCast(@alignCast(ctx));
|
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
|
// Categorize the error for statistics
|
||||||
if (error_info.recoverable) {
|
if (error_info.recoverable) {
|
||||||
self.recoverable_error_count += 1;
|
self.recoverable_error_count += 1;
|
||||||
|
@ -52,50 +57,57 @@ const DemoHandler = struct {
|
||||||
self.error_count += 1;
|
self.error_count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format timestamp
|
if (builtin.is_test) return; // Suppress output during tests
|
||||||
const timestamp = std.time.timestamp();
|
|
||||||
|
|
||||||
// Determine error severity and icon
|
// Format complete error message in a buffer
|
||||||
const SeverityInfo = struct { icon: []const u8, level: []const u8 };
|
var buffer: [2048]u8 = undefined;
|
||||||
const severity_info: SeverityInfo = switch (error_info.error_code) {
|
var stream = std.io.fixedBufferStream(&buffer);
|
||||||
stt.SttError.InternalError => if (error_info.recoverable)
|
const writer = stream.writer();
|
||||||
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" },
|
|
||||||
};
|
|
||||||
|
|
||||||
// Print detailed error information
|
try writer.print("{s}", .{error_info.message});
|
||||||
std.debug.print("[{}] {s} {s}: {s}\n", .{ timestamp, severity_info.icon, severity_info.level, error_info.message });
|
|
||||||
|
|
||||||
// Print additional context if available
|
if (error_info.context) |context|
|
||||||
if (error_info.context) |context| {
|
try writer.print("\n\tContext: {s}", .{context});
|
||||||
std.debug.print(" Context: {s}\n", .{context});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print system error if available
|
if (error_info.system_error) |sys_err|
|
||||||
if (error_info.system_error) |sys_err| {
|
try writer.print("\n\tSystem Error: {} ({any})", .{ sys_err, error_info.error_code });
|
||||||
std.debug.print(" System Error: {} ({any})\n", .{ sys_err, error_info.error_code });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print recovery suggestion if available
|
if (error_info.recovery_suggestion) |suggestion|
|
||||||
if (error_info.recovery_suggestion) |suggestion| {
|
try writer.print("\n\tSuggestion: {s}", .{suggestion});
|
||||||
std.debug.print(" Suggestion: {s}\n", .{suggestion});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print recoverable status
|
if (error_info.recoverable)
|
||||||
if (error_info.recoverable) {
|
try writer.print("\n\tStatus: Recoverable - system will attempt to continue", .{})
|
||||||
std.debug.print(" Status: Recoverable - system will attempt to continue\n", .{});
|
else
|
||||||
} else {
|
try writer.print("\n\tStatus: Fatal - intervention may be required", .{});
|
||||||
std.debug.print(" Status: Fatal - intervention may be required\n", .{});
|
|
||||||
|
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});
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue