more cleanup of logging

This commit is contained in:
Emil Lerch 2025-09-10 17:47:13 -07:00
parent 6e7dba19e4
commit b6cfeea62f
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -137,6 +137,8 @@ fn signalHandler(sig: c_int) callconv(.c) void {
}
pub fn main() !void {
const stdout = std.fs.File.stdout();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
@ -181,43 +183,44 @@ pub fn main() !void {
std.debug.print("Initializing STT library...\n", .{});
var session = stt.SttSession.init(allocator, options) catch |err| {
std.debug.print("Failed to initialize STT library: {}\n", .{err});
std.debug.print("Please ensure:\n", .{});
std.debug.print(" - Audio device '{s}' is available\n", .{options.audio_device});
std.debug.print(" - Model directory exists at: {s}\n", .{options.model_path});
std.debug.print(" - You have permission to access the audio device\n", .{});
std.log.err("Failed to initialize STT library: {}", .{err});
std.log.err("Please ensure:", .{});
std.log.err(" - Audio device '{s}' is available", .{options.audio_device});
std.log.err(" - Model directory exists at: {s}", .{options.model_path});
std.log.err(" - You have permission to access the audio device", .{});
return;
};
defer {
std.debug.print("Cleaning up STT session...\n", .{});
_ = stdout.writeAll("Cleaning up STT session...") catch {};
session.deinit();
_ = stdout.writeAll("\n") catch {};
}
std.debug.print("✓ STT library initialized successfully\n", .{});
std.debug.print("Configuration:\n", .{});
std.debug.print(" Model path: {s}\n", .{options.model_path});
std.debug.print(" Audio device: {s}\n", .{options.audio_device});
std.debug.print(" Sample rate: {} Hz\n", .{options.sample_rate});
std.debug.print(" Channels: {} (converted to mono)\n", .{options.channels});
std.debug.print(" Buffer size: {} frames\n", .{options.buffer_size});
std.debug.print("\n", .{});
std.log.info("✓ STT library initialized successfully", .{});
std.log.info("Configuration:", .{});
std.log.info(" Model path: {s}", .{options.model_path});
std.log.info(" Audio device: {s}", .{options.audio_device});
std.log.info(" Sample rate: {} Hz", .{options.sample_rate});
std.log.info(" Channels: {} (converted to mono)", .{options.channels});
std.log.info(" Buffer size: {} frames", .{options.buffer_size});
std.log.info("", .{});
// Start listening for speech with error handling
std.debug.print("Starting speech recognition...\n", .{});
_ = stdout.writeAll("Starting speech recognition...\n") catch {};
session.start_listening() catch |err| {
std.debug.print("Failed to start listening: {}\n", .{err});
std.log.err("Failed to start listening: {}", .{err});
switch (err) {
stt.SttError.AudioDeviceError => {
std.debug.print("Audio device error. Please check:\n", .{});
std.debug.print(" - Device '{s}' exists and is accessible\n", .{options.audio_device});
std.debug.print(" - No other application is using the device\n", .{});
std.debug.print(" - You have permission to access audio devices\n", .{});
std.log.err("Audio device error. Please check:", .{});
std.log.err(" - Device '{s}' exists and is accessible", .{options.audio_device});
std.log.err(" - No other application is using the device", .{});
std.log.err(" - You have permission to access audio devices", .{});
},
stt.SttError.ThreadingError => {
std.debug.print("Threading error. System may be under heavy load.\n", .{});
std.log.err("Threading error. System may be under heavy load.", .{});
},
else => {
std.debug.print("Unexpected error during startup.\n", .{});
std.log.err("Unexpected error during startup.", .{});
},
}
return;
@ -227,10 +230,10 @@ pub fn main() !void {
session.stop_listening();
}
std.debug.print("✓ Speech recognition started successfully\n", .{});
std.debug.print("Listening for speech... (Press Ctrl+C to exit)\n", .{});
std.debug.print("Speak into your microphone to see speech recognition results.\n", .{});
std.debug.print("----------------------------------------\n", .{});
std.log.info("✓ Speech recognition started successfully", .{});
_ = stdout.writeAll("Listening for speech... (Press Ctrl+C to exit)\n") catch {};
_ = stdout.writeAll("Speak into your microphone to see speech recognition results\n") catch {};
_ = stdout.writeAll("------------------------------------------------------------\n") catch {};
// Main loop - wait for Ctrl+C signal
while (!should_exit.load(.acquire)) {
@ -239,36 +242,36 @@ pub fn main() !void {
// Check if session is still listening (in case of errors)
if (!session.is_listening()) {
std.debug.print("Speech recognition stopped unexpectedly.\n", .{});
std.log.err("Speech recognition stopped unexpectedly.", .{});
break;
}
}
std.debug.print("\n----------------------------------------\n", .{});
std.debug.print("Shutdown signal received, stopping...\n", .{});
_ = stdout.writeAll("\n----------------------------------------\n") catch {};
_ = stdout.writeAll("Shutdown signal received, stopping...\n") catch {};
// Get final statistics from demo handler
const stats = demo_handler.getStats();
std.debug.print("\nDemo Session Summary:\n", .{});
std.debug.print(" Speech detections: {}\n", .{stats.speech_count});
std.debug.print(" Fatal errors: {}\n", .{stats.error_count});
std.debug.print(" Recoverable errors: {}\n", .{stats.recoverable_error_count});
std.debug.print(" Total issues: {}\n", .{stats.total_issues});
std.log.info("Demo Session Summary:", .{});
std.log.info(" Speech detections: {}", .{stats.speech_count});
std.log.info(" Fatal errors: {}", .{stats.error_count});
std.log.info(" Recoverable errors: {}", .{stats.recoverable_error_count});
std.log.info(" Total issues: {}", .{stats.total_issues});
if (stats.speech_count > 0) {
std.debug.print("✓ Speech recognition worked successfully!\n", .{});
std.log.info("✓ Speech recognition worked successfully!", .{});
if (stats.recoverable_error_count > 0) {
std.debug.print(" System recovered from {} issues during operation.\n", .{stats.recoverable_error_count});
std.log.info(" System recovered from {} issues during operation.", .{stats.recoverable_error_count});
}
} else if (stats.error_count > 0) {
std.debug.print("✗ Fatal errors occurred during speech recognition.\n", .{});
std.log.info("✗ Fatal errors occurred during speech recognition.", .{});
} else if (stats.recoverable_error_count > 0) {
std.debug.print("⚠ Recoverable issues occurred but system continued operating.\n", .{});
std.log.info("⚠ Recoverable issues occurred but system continued operating.", .{});
} else {
std.debug.print(" No speech was detected during this session.\n", .{});
std.log.info(" No speech was detected during this session.", .{});
}
std.debug.print("Demo completed successfully.\n", .{});
_ = stdout.writeAll("Session completed successfully.\n") catch {};
}
// Test the demo functionality