optional process to exec. Spawn without wait, not sure how legit

This commit is contained in:
Emil Lerch 2025-09-11 09:32:44 -07:00
parent 3a8138c901
commit d443a36f7a
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -9,6 +9,7 @@ var should_exit = std.atomic.Value(bool).init(false);
/// Demo implementation of speech event handler with comprehensive error handling
const SpeechHandler = struct {
allocator: std.mem.Allocator,
speech_count: u32 = 0,
error_count: u32 = 0,
warning_count: u32 = 0,
@ -24,13 +25,19 @@ const SpeechHandler = struct {
// Print with timestamp for better experience
const timestamp = std.time.timestamp();
std.debug.print("[{}] Speech #{}: {s}\n", .{ timestamp, self.speech_count, text });
std.log.debug("[{}] Speech {}->{?s}: {s}", .{
timestamp,
self.speech_count,
self.exec_program,
text,
});
// Execute program if specified
if (self.exec_program) |program| {
var child = std.process.Child.init(&[_][]const u8{ program, text }, std.heap.page_allocator);
_ = child.spawn() catch |err| {
var child = std.process.Child.init(&[_][]const u8{ program, text }, self.allocator);
child.spawn() catch |err| {
std.log.err("Failed to execute program '{s}': {}", .{ program, err });
return;
};
}
}
@ -179,7 +186,10 @@ pub fn main() !void {
}
// Create handler with statistics tracking
var handler = SpeechHandler{ .exec_program = exec_program };
var handler = SpeechHandler{
.allocator = allocator,
.exec_program = exec_program,
};
const speech_handler = stt.SpeechEventHandler{
.onSpeechFn = SpeechHandler.onSpeech,
.onErrorFn = SpeechHandler.onError,
@ -244,6 +254,7 @@ pub fn main() !void {
_ = stdout.writeAll("\n") catch {};
}
std.log.info("Program to execute on speech detection: {?s}", .{exec_program});
std.log.info("STT library initialized successfully with configuration:", .{});
std.log.info(" Model path: {s}", .{options.model_path});
std.log.info(" Audio device: {s}", .{options.audio_device});
@ -316,7 +327,7 @@ pub fn main() !void {
test "handler functionality" {
const testing = std.testing;
var handler = SpeechHandler{};
var handler = SpeechHandler{ .allocator = std.testing.allocator };
const speech_handler = stt.SpeechEventHandler{
.onSpeechFn = SpeechHandler.onSpeech,
.onErrorFn = SpeechHandler.onError,