diff --git a/demo/c/picovoice_demo_file.c b/demo/c/picovoice_demo_file.c index b35a75ff2..319875300 100644 --- a/demo/c/picovoice_demo_file.c +++ b/demo/c/picovoice_demo_file.c @@ -130,6 +130,12 @@ void print_usage(const char *program_name) { program_name); } +void print_error_message(char **message_stack, int32_t message_stack_depth) { + for (int32_t i = 0; i < message_stack_depth; i++) { + fprintf(stderr, " [%d] %s\n", i, message_stack[i]); + } +} + int picovoice_main(int argc, char *argv[]) { const char *library_path = NULL; @@ -261,6 +267,22 @@ int picovoice_main(int argc, char *argv[]) { exit(1); } + pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(picovoice_library, "pv_get_error_stack"); + if (!pv_get_error_stack_func) { + print_dl_error("failed to load 'pv_get_error_stack_func'"); + exit(1); + } + + void (*pv_free_error_stack_func)(char **) = load_symbol(picovoice_library, "pv_free_error_stack"); + if (!pv_free_error_stack_func) { + print_dl_error("failed to load 'pv_free_error_stack_func'"); + exit(1); + } + + char **message_stack = NULL; + int32_t message_stack_depth = 0; + pv_status_t error_status = PV_STATUS_RUNTIME_ERROR; + drwav f; #if defined(_WIN32) || defined(_WIN64) @@ -318,6 +340,19 @@ int picovoice_main(int argc, char *argv[]) { &handle); if (status != PV_STATUS_SUCCESS) { fprintf(stderr, "'pv_picovoice_init' failed with '%s'\n", pv_status_to_string_func(status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Rhino error state with '%s'\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + } + + pv_free_error_stack_func(message_stack); exit(1); } @@ -333,7 +368,20 @@ int picovoice_main(int argc, char *argv[]) { status = pv_picovoice_process_func(handle, pcm); if (status != PV_STATUS_SUCCESS) { - fprintf(stderr, "'pv_picovoice_process' failed with '%s'\n", pv_status_to_string_func(status)); + fprintf(stderr, "'pv_picovoice_process' failed with '%s'", pv_status_to_string_func(status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Rhino error state with '%s'\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + } + + pv_free_error_stack_func(message_stack); exit(1); } diff --git a/demo/c/picovoice_demo_mic.c b/demo/c/picovoice_demo_mic.c index dcbd06cbb..f2c7bd0c0 100644 --- a/demo/c/picovoice_demo_mic.c +++ b/demo/c/picovoice_demo_mic.c @@ -156,6 +156,12 @@ static void inference_callback(pv_inference_t *inference) { pv_inference_delete_func(inference); } +void print_error_message(char **message_stack, int32_t message_stack_depth) { + for (int32_t i = 0; i < message_stack_depth; i++) { + fprintf(stderr, " [%d] %s\n", i, message_stack[i]); + } +} + int picovoice_main(int argc, char *argv[]) { signal(SIGINT, interrupt_handler); @@ -291,6 +297,22 @@ int picovoice_main(int argc, char *argv[]) { exit(1); } + pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(picovoice_library, "pv_get_error_stack"); + if (!pv_get_error_stack_func) { + print_dl_error("failed to load 'pv_get_error_stack_func'"); + exit(1); + } + + void (*pv_free_error_stack_func)(char **) = load_symbol(picovoice_library, "pv_free_error_stack"); + if (!pv_free_error_stack_func) { + print_dl_error("failed to load 'pv_free_error_stack_func'"); + exit(1); + } + + char **message_stack = NULL; + int32_t message_stack_depth = 0; + pv_status_t error_status = PV_STATUS_RUNTIME_ERROR; + fprintf(stdout, "%s\n", access_key); fprintf(stdout, "%s\n", library_path); fprintf(stdout, "%s\n", keyword_path); @@ -312,7 +334,20 @@ int picovoice_main(int argc, char *argv[]) { inference_callback, &picovoice); if (status != PV_STATUS_SUCCESS) { - fprintf(stderr, "'pv_picovoice_init' failed with '%s'\n", pv_status_to_string_func(status)); + fprintf(stderr, "'pv_picovoice_init' failed with '%s'", pv_status_to_string_func(status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Rhino error state with '%s'\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + } + + pv_free_error_stack_func(message_stack); exit(1); } @@ -354,7 +389,20 @@ int picovoice_main(int argc, char *argv[]) { status = pv_picovoice_process_func(picovoice, pcm); if (status != PV_STATUS_SUCCESS) { - fprintf(stderr, "'pv_picovoice_process' failed with '%s'\n", pv_status_to_string_func(status)); + fprintf(stderr, "'pv_picovoice_process' failed with '%s'", pv_status_to_string_func(status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Rhino error state with '%s'\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + } + + pv_free_error_stack_func(message_stack); exit(1); } }