From 68ec0b76414d00b2590023d646cfb3c28149cba5 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Fri, 2 May 2025 19:28:44 +0800 Subject: [PATCH 1/4] refactor: introduce mandatory log level and enhance logging functionality across components --- core/include/ten_utils/log/log.h | 38 +-------------- .../ten_runtime/engine/engine.h | 5 ++ .../extension_thread/extension_thread.h | 2 + core/include_internal/ten_utils/log/log.h | 48 +++++++++++++++++++ core/src/ten_runtime/app/predefined_graph.c | 8 +++- core/src/ten_runtime/engine/engine.c | 11 +++++ .../extension_context/extension_context.c | 14 ++++-- .../extension_thread/extension_thread.c | 2 + core/src/ten_utils/lib/sys/general/pid.c | 12 ++--- core/src/ten_utils/log/level.c | 2 + 10 files changed, 92 insertions(+), 50 deletions(-) diff --git a/core/include/ten_utils/log/log.h b/core/include/ten_utils/log/log.h index 3218cee63..a888982ab 100644 --- a/core/include/ten_utils/log/log.h +++ b/core/include/ten_utils/log/log.h @@ -50,42 +50,6 @@ __FILE__, __LINE__, __VA_ARGS__); \ } while (0) -#define TEN_LOGV_AUX(log, ...) \ - do { \ - ten_log_log_formatted(log, TEN_LOG_LEVEL_VERBOSE, __func__, __FILE__, \ - __LINE__, __VA_ARGS__); \ - } while (0) - -#define TEN_LOGD_AUX(log, ...) \ - do { \ - ten_log_log_formatted(log, TEN_LOG_LEVEL_DEBUG, __func__, __FILE__, \ - __LINE__, __VA_ARGS__); \ - } while (0) - -#define TEN_LOGI_AUX(log, ...) \ - do { \ - ten_log_log_formatted(log, TEN_LOG_LEVEL_INFO, __func__, __FILE__, \ - __LINE__, __VA_ARGS__); \ - } while (0) - -#define TEN_LOGW_AUX(log, ...) \ - do { \ - ten_log_log_formatted(log, TEN_LOG_LEVEL_WARN, __func__, __FILE__, \ - __LINE__, __VA_ARGS__); \ - } while (0) - -#define TEN_LOGE_AUX(log, ...) \ - do { \ - ten_log_log_formatted(log, TEN_LOG_LEVEL_ERROR, __func__, __FILE__, \ - __LINE__, __VA_ARGS__); \ - } while (0) - -#define TEN_LOGF_AUX(log, ...) \ - do { \ - ten_log_log_formatted(log, TEN_LOG_LEVEL_FATAL, __func__, __FILE__, \ - __LINE__, __VA_ARGS__); \ - } while (0) - typedef struct ten_string_t ten_string_t; typedef struct ten_log_t ten_log_t; @@ -98,6 +62,8 @@ typedef enum TEN_LOG_LEVEL { TEN_LOG_LEVEL_WARN, TEN_LOG_LEVEL_ERROR, TEN_LOG_LEVEL_FATAL, + + TEN_LOG_LEVEL_MANDATORY, } TEN_LOG_LEVEL; typedef enum TEN_LOG_OUTPUT_TYPE { diff --git a/core/include_internal/ten_runtime/engine/engine.h b/core/include_internal/ten_runtime/engine/engine.h index 3c891119d..085126af5 100644 --- a/core/include_internal/ten_runtime/engine/engine.h +++ b/core/include_internal/ten_runtime/engine/engine.h @@ -61,6 +61,8 @@ struct ten_engine_t { // into the engine until it is fully prepared. bool is_ready_to_handle_msg; + ten_string_t graph_name; + // When app creates an engine, it will create a randomized graph ID for the // engine. It _must_ be a UUID4 string. ten_string_t graph_id; @@ -118,6 +120,9 @@ TEN_RUNTIME_PRIVATE_API bool ten_engine_is_ready_to_handle_msg( TEN_RUNTIME_PRIVATE_API const char *ten_engine_get_id(ten_engine_t *self, bool check_thread); +TEN_RUNTIME_PRIVATE_API void ten_engine_set_graph_name(ten_engine_t *self, + const char *name); + TEN_RUNTIME_PRIVATE_API void ten_engine_add_orphan_connection( ten_engine_t *self, ten_connection_t *connection); diff --git a/core/include_internal/ten_runtime/extension_thread/extension_thread.h b/core/include_internal/ten_runtime/extension_thread/extension_thread.h index aa922a071..ec798d743 100644 --- a/core/include_internal/ten_runtime/extension_thread/extension_thread.h +++ b/core/include_internal/ten_runtime/extension_thread/extension_thread.h @@ -46,6 +46,8 @@ typedef struct ten_extension_thread_t { ten_signature_t signature; ten_sanitizer_thread_check_t thread_check; + int64_t tid; + TEN_EXTENSION_THREAD_STATE state; bool is_close_triggered; diff --git a/core/include_internal/ten_utils/log/log.h b/core/include_internal/ten_utils/log/log.h index 3a8358474..bb1e020a7 100644 --- a/core/include_internal/ten_utils/log/log.h +++ b/core/include_internal/ten_utils/log/log.h @@ -16,6 +16,54 @@ #define TEN_LOG_SIGNATURE 0xC0EE0CE92149D61AU +#define TEN_LOGM(...) \ + do { \ + ten_log_log_formatted(&ten_global_log, TEN_LOG_LEVEL_MANDATORY, __func__, \ + __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_LOGV_AUX(log, ...) \ + do { \ + ten_log_log_formatted(log, TEN_LOG_LEVEL_VERBOSE, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_LOGD_AUX(log, ...) \ + do { \ + ten_log_log_formatted(log, TEN_LOG_LEVEL_DEBUG, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_LOGI_AUX(log, ...) \ + do { \ + ten_log_log_formatted(log, TEN_LOG_LEVEL_INFO, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_LOGW_AUX(log, ...) \ + do { \ + ten_log_log_formatted(log, TEN_LOG_LEVEL_WARN, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_LOGE_AUX(log, ...) \ + do { \ + ten_log_log_formatted(log, TEN_LOG_LEVEL_ERROR, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_LOGF_AUX(log, ...) \ + do { \ + ten_log_log_formatted(log, TEN_LOG_LEVEL_FATAL, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_LOGM_AUX(log, ...) \ + do { \ + ten_log_log_formatted(log, TEN_LOG_LEVEL_MANDATORY, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + typedef struct ten_string_t ten_string_t; TEN_UTILS_PRIVATE_API bool ten_log_check_integrity(ten_log_t *self); diff --git a/core/src/ten_runtime/app/predefined_graph.c b/core/src/ten_runtime/app/predefined_graph.c index 7f1a10474..a1b51abe2 100644 --- a/core/src/ten_runtime/app/predefined_graph.c +++ b/core/src/ten_runtime/app/predefined_graph.c @@ -228,13 +228,17 @@ bool ten_app_start_predefined_graph( ten_path_t *out_path = (ten_path_t *)ten_path_table_add_out_path( self->path_table, start_graph_cmd); - TEN_ASSERT(out_path && ten_path_check_integrity(out_path, true), - "Should not happen."); + TEN_ASSERT(out_path, "Should not happen."); + TEN_ASSERT(ten_path_check_integrity(out_path, true), "Should not happen."); } // @} predefined_graph_info->engine = ten_app_create_engine(self, start_graph_cmd); + ten_engine_set_graph_name( + predefined_graph_info->engine, + ten_string_get_raw_str(&predefined_graph_info->name)); + // There is no 'connection' when creating predefined graph, so it's always no // migration in this stage. Send the 'start_graph_cmd' into the newly created // engine directly. diff --git a/core/src/ten_runtime/engine/engine.c b/core/src/ten_runtime/engine/engine.c index 3e065df02..ed775ada9 100644 --- a/core/src/ten_runtime/engine/engine.c +++ b/core/src/ten_runtime/engine/engine.c @@ -95,6 +95,7 @@ static void ten_engine_destroy(ten_engine_t *self) { self->cmd_stop_graph = NULL; } + ten_string_deinit(&self->graph_name); ten_string_deinit(&self->graph_id); ten_path_table_destroy(self->path_table); @@ -125,6 +126,7 @@ static void ten_engine_set_graph_id(ten_engine_t *self, ten_shared_ptr_t *cmd) { !ten_string_is_empty(src_graph_id)) { TEN_LOGD("[%s] Inherit engine's name from previous node", ten_string_get_raw_str(src_graph_id)); + ten_string_init_formatted(&self->graph_id, "%s", ten_string_get_raw_str(src_graph_id)); } else { @@ -216,6 +218,7 @@ ten_engine_t *ten_engine_create(ten_app_t *app, ten_shared_ptr_t *cmd) { self->long_running_mode = ten_cmd_start_graph_get_long_running_mode(cmd); + ten_string_init(&self->graph_name); ten_engine_set_graph_id(self, cmd); ten_engine_init_individual_eventloop_relevant_vars(self, app); @@ -253,6 +256,14 @@ const char *ten_engine_get_id(ten_engine_t *self, bool check_thread) { return ten_string_get_raw_str(&self->graph_id); } +void ten_engine_set_graph_name(ten_engine_t *self, const char *name) { + TEN_ASSERT(self, "Should not happen."); + TEN_ASSERT(ten_engine_check_integrity(self, false), "Should not happen."); + TEN_ASSERT(name, "Should not happen."); + + ten_string_set_from_c_str(&self->graph_name, name); +} + void ten_engine_del_orphan_connection(ten_engine_t *self, ten_connection_t *connection) { TEN_ASSERT(self, "Should not happen."); diff --git a/core/src/ten_runtime/extension_context/extension_context.c b/core/src/ten_runtime/extension_context/extension_context.c index 39b368933..b98d7b556 100644 --- a/core/src/ten_runtime/extension_context/extension_context.c +++ b/core/src/ten_runtime/extension_context/extension_context.c @@ -25,6 +25,7 @@ #include "include_internal/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.h" #include "include_internal/ten_runtime/msg/msg.h" #include "include_internal/ten_runtime/ten_env/ten_env.h" +#include "include_internal/ten_utils/log/log.h" #include "ten_runtime/app/app.h" #include "ten_runtime/common/error_code.h" #include "ten_runtime/ten_env/ten_env.h" @@ -329,6 +330,15 @@ static void ten_extension_context_add_extension_groups_info_from_graph( ten_list_swap(&self->extension_groups_info_from_graph, extension_groups_info); } +static void ten_extension_context_log_graph_resources( + ten_extension_context_t *self) { + TEN_ASSERT(self, "Invalid argument."); + TEN_ASSERT(ten_extension_context_check_integrity(self, true), + "Invalid use of extension_context %p.", self); + + TEN_LOGM("[graph resources] %s, ", ten_engine_get_id(self->engine, true)); +} + static void ten_extension_context_create_extension_group_done( ten_env_t *ten_env, ten_extension_group_t *extension_group) { TEN_ASSERT(extension_group, "Should not happen."); @@ -430,9 +440,7 @@ static void ten_extension_context_create_extension_group_done( ten_extension_group_get_name(extension_group, true)); TEN_ASSERT(extension_group->extension_group_info, "Should not happen."); - TEN_LOGV( - "[%s] graph info: ", ten_engine_get_id(extension_context->engine, true), - ten_extension_group_get_name(extension_group, true)); + ten_extension_context_log_graph_resources(extension_context); ten_extension_context_start(extension_context); } diff --git a/core/src/ten_runtime/extension_thread/extension_thread.c b/core/src/ten_runtime/extension_thread/extension_thread.c index 3787fa2a9..e259370bf 100644 --- a/core/src/ten_runtime/extension_thread/extension_thread.c +++ b/core/src/ten_runtime/extension_thread/extension_thread.c @@ -85,6 +85,7 @@ ten_extension_thread_t *ten_extension_thread_create(void) { ten_signature_set(&self->signature, (ten_signature_t)TEN_EXTENSION_THREAD_SIGNATURE); + self->tid = 0; self->state = TEN_EXTENSION_THREAD_STATE_INIT; self->is_close_triggered = false; @@ -255,6 +256,7 @@ static void *ten_extension_thread_main_actual(ten_extension_thread_t *self) { ten_extension_thread_check_integrity(self, false), "Should not happen."); ten_extension_thread_inherit_thread_ownership(self); + self->tid = ten_thread_get_id(NULL); ten_extension_group_t *extension_group = self->extension_group; TEN_ASSERT(extension_group, "Should not happen."); diff --git a/core/src/ten_utils/lib/sys/general/pid.c b/core/src/ten_utils/lib/sys/general/pid.c index 976773f72..b4f4d3415 100644 --- a/core/src/ten_utils/lib/sys/general/pid.c +++ b/core/src/ten_utils/lib/sys/general/pid.c @@ -11,6 +11,8 @@ #include #include +#include "ten_utils/lib/thread.h" + #if defined(__linux__) #include @@ -51,13 +53,5 @@ void ten_get_pid_tid(int64_t *pid, int64_t *tid) { *pid = getpid(); #endif -#if defined(OS_WINDOWS) - *tid = GetCurrentThreadId(); -#elif defined(OS_LINUX) - *tid = syscall(SYS_gettid); -#elif defined(OS_MACOS) - *tid = (int)pthread_mach_thread_np(pthread_self()); -#else -#error Platform not supported -#endif + *tid = ten_thread_get_id(NULL); } diff --git a/core/src/ten_utils/log/level.c b/core/src/ten_utils/log/level.c index e7415c335..e7dbe2864 100644 --- a/core/src/ten_utils/log/level.c +++ b/core/src/ten_utils/log/level.c @@ -24,6 +24,8 @@ char ten_log_level_char(const TEN_LOG_LEVEL level) { return 'E'; case TEN_LOG_LEVEL_FATAL: return 'F'; + case TEN_LOG_LEVEL_MANDATORY: + return 'M'; default: return '?'; } From 7e734b840ed001bfb2e5a2cfb727748090a0fea3 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Fri, 2 May 2025 20:29:58 +0800 Subject: [PATCH 2/4] fix: refine codes --- .vscode/launch.json | 2 +- .../ten_utils/log/termcolor.h | 1 + .../ten_runtime/extension/internal/close.c | 4 +- .../extension/internal/path_timer.c | 8 +- .../ten_runtime/extension/ten_env/on_xxx.c | 12 +-- .../extension_context/extension_context.c | 15 +++- .../extension_group/ten_env/on_xxx.c | 16 ++-- .../extension_thread/extension_thread.c | 80 ++++++++++++++++--- core/src/ten_utils/log/formatter.c | 3 + 9 files changed, 109 insertions(+), 32 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 7d4ac9250..36877afed 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -137,7 +137,7 @@ "request": "launch", "program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test", "args": [ - "--gtest_filter=BasicTest.ThrowExceptionInExtension" + "--gtest_filter=BasicTest.EmptyExtensionGroup" ], "cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/", "env": { diff --git a/core/include_internal/ten_utils/log/termcolor.h b/core/include_internal/ten_utils/log/termcolor.h index 987a0b18e..c91a8a25d 100644 --- a/core/include_internal/ten_utils/log/termcolor.h +++ b/core/include_internal/ten_utils/log/termcolor.h @@ -16,3 +16,4 @@ #define TEN_LOG_COLOR_MAGENTA "\033[35m" #define TEN_LOG_COLOR_CYAN "\033[36m" #define TEN_LOG_COLOR_WHITE "\033[37m" +#define TEN_LOG_COLOR_GOLD "\033[1;33m" diff --git a/core/src/ten_runtime/extension/internal/close.c b/core/src/ten_runtime/extension/internal/close.c index 921ec4a3d..fdda9873c 100644 --- a/core/src/ten_runtime/extension/internal/close.c +++ b/core/src/ten_runtime/extension/internal/close.c @@ -94,8 +94,8 @@ static void ten_extension_do_close(ten_extension_t *self) { "Should not happen."); ten_extension_thread_t *extension_thread = self->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); ten_extension_on_deinit(self); diff --git a/core/src/ten_runtime/extension/internal/path_timer.c b/core/src/ten_runtime/extension/internal/path_timer.c index 67b014236..7dac33e5b 100644 --- a/core/src/ten_runtime/extension/internal/path_timer.c +++ b/core/src/ten_runtime/extension/internal/path_timer.c @@ -118,8 +118,8 @@ ten_timer_t *ten_extension_create_timer_for_in_path(ten_extension_t *self) { "Should not happen."); ten_extension_thread_t *extension_thread = self->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); ten_timer_t *timer = ten_timer_create( @@ -138,8 +138,8 @@ ten_timer_t *ten_extension_create_timer_for_out_path(ten_extension_t *self) { "Should not happen."); ten_extension_thread_t *extension_thread = self->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); ten_timer_t *timer = ten_timer_create( diff --git a/core/src/ten_runtime/extension/ten_env/on_xxx.c b/core/src/ten_runtime/extension/ten_env/on_xxx.c index 12f339d91..b4d417a49 100644 --- a/core/src/ten_runtime/extension/ten_env/on_xxx.c +++ b/core/src/ten_runtime/extension/ten_env/on_xxx.c @@ -97,8 +97,8 @@ bool ten_extension_on_configure_done(ten_env_t *self) { extension->state = TEN_EXTENSION_STATE_ON_CONFIGURE_DONE; ten_extension_thread_t *extension_thread = extension->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); if (extension_thread->is_close_triggered) { @@ -249,8 +249,8 @@ bool ten_extension_on_init_done(ten_env_t *self) { extension->state = TEN_EXTENSION_STATE_ON_INIT_DONE; ten_extension_thread_t *extension_thread = extension->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); if (extension_thread->is_close_triggered) { @@ -308,8 +308,8 @@ bool ten_extension_on_start_done(ten_env_t *self) { extension->state = TEN_EXTENSION_STATE_ON_START_DONE; ten_extension_thread_t *extension_thread = extension->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); if (extension_thread->is_close_triggered) { diff --git a/core/src/ten_runtime/extension_context/extension_context.c b/core/src/ten_runtime/extension_context/extension_context.c index b98d7b556..32445c553 100644 --- a/core/src/ten_runtime/extension_context/extension_context.c +++ b/core/src/ten_runtime/extension_context/extension_context.c @@ -7,6 +7,7 @@ #include "include_internal/ten_runtime/extension_context/extension_context.h" #include +#include #include #include "include_internal/ten_runtime/addon/addon.h" @@ -336,7 +337,19 @@ static void ten_extension_context_log_graph_resources( TEN_ASSERT(ten_extension_context_check_integrity(self, true), "Invalid use of extension_context %p.", self); - TEN_LOGM("[graph resources] %s, ", ten_engine_get_id(self->engine, true)); + // Get the required information. + const char *app_uri = ten_app_get_uri(self->engine->app); + const char *graph_id = ten_engine_get_id(self->engine, true); + const char *graph_name = ten_string_get_raw_str(&self->engine->graph_name); + + // Log the complete JSON in a single call. + TEN_LOGM( + "[graph resources] {" + "\"app_uri\": \"%s\", " + "\"graph name\": \"%s\", " + "\"graph id\": \"%s\" " + "}", + app_uri, graph_name, graph_id); } static void ten_extension_context_create_extension_group_done( diff --git a/core/src/ten_runtime/extension_group/ten_env/on_xxx.c b/core/src/ten_runtime/extension_group/ten_env/on_xxx.c index b14b0db9f..978f5be3b 100644 --- a/core/src/ten_runtime/extension_group/ten_env/on_xxx.c +++ b/core/src/ten_runtime/extension_group/ten_env/on_xxx.c @@ -78,8 +78,8 @@ void ten_extension_group_on_init_done(ten_env_t *self) { ten_extension_group_get_name(extension_group, true)); ten_extension_thread_t *extension_thread = extension_group->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); int rc = ten_runloop_post_task_tail( @@ -131,8 +131,8 @@ bool ten_extension_group_on_deinit_done(ten_env_t *self) { } ten_extension_thread_t *extension_thread = extension_group->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); // All extensions belong to this extension thread (group) are deleted, notify @@ -160,8 +160,8 @@ void ten_extension_group_on_create_extensions_done(ten_extension_group_t *self, ten_string_get_raw_str(&self->name)); ten_extension_thread_t *extension_thread = self->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); // Remove the extensions that were not successfully created from the list of @@ -212,8 +212,8 @@ void ten_extension_group_on_destroy_extensions_done( ten_string_get_raw_str(&self->name)); ten_extension_thread_t *extension_thread = self->extension_thread; - TEN_ASSERT(extension_thread && - ten_extension_thread_check_integrity(extension_thread, true), + TEN_ASSERT(extension_thread, "Should not happen."); + TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true), "Should not happen."); int rc = ten_runloop_post_task_tail( diff --git a/core/src/ten_runtime/extension_thread/extension_thread.c b/core/src/ten_runtime/extension_thread/extension_thread.c index e259370bf..50967710f 100644 --- a/core/src/ten_runtime/extension_thread/extension_thread.c +++ b/core/src/ten_runtime/extension_thread/extension_thread.c @@ -26,6 +26,7 @@ #include "include_internal/ten_runtime/extension_thread/msg_interface/common.h" #include "include_internal/ten_runtime/msg/msg.h" #include "include_internal/ten_runtime/ten_env/ten_env.h" +#include "include_internal/ten_utils/log/log.h" #include "include_internal/ten_utils/sanitizer/thread_check.h" #include "ten_runtime/extension/extension.h" #include "ten_runtime/ten_env/ten_env.h" @@ -493,6 +494,65 @@ void ten_extension_thread_start_life_cycle_of_all_extensions_task( } } +static void ten_extension_thread_log_graph_resources( + ten_extension_thread_t *self) { + TEN_ASSERT(self, "Invalid argument."); + TEN_ASSERT(ten_extension_thread_check_integrity(self, true), + "Invalid use of extension_thread %p.", self); + + // Get the required information. + const char *app_uri = ten_app_get_uri(self->extension_context->engine->app); + const char *graph_id = + ten_engine_get_id(self->extension_context->engine, false); + const char *graph_name = + ten_string_get_raw_str(&self->extension_context->engine->graph_name); + + // Build extension list entries. + ten_string_t extensions_json; + ten_string_init(&extensions_json); + + bool first_extension = true; + + ten_list_foreach (&self->extensions, iter) { + ten_extension_t *extension = ten_ptr_listnode_get(iter.node); + TEN_ASSERT(extension, "Should not happen."); + TEN_ASSERT(ten_extension_check_integrity(extension, true), + "Invalid use of extension %p.", extension); + + // Add comma for all but the first extension. + if (!first_extension) { + ten_string_append_formatted(&extensions_json, "%s", ", "); + } + first_extension = false; + + // Add extension entry with thread_id. + char entry[256]; + int written = + snprintf(entry, sizeof(entry), "\"%s\": {\"thread_id\": %lld}", + ten_extension_get_name(extension, true), (long long)self->tid); + + if (written < 0 || written >= (int)sizeof(entry)) { + TEN_LOGE("Error formatting extension entry or buffer too small"); + continue; + } + + ten_string_append_formatted(&extensions_json, "%s", entry); + } + + // Log the complete JSON in a single call. + TEN_LOGM( + "[graph resources] {" + "\"app_uri\": \"%s\", " + "\"graph name\": \"%s\", " + "\"graph id\": \"%s\", " + "\"extensions\": {%s}" + "}", + app_uri, graph_name, graph_id, ten_string_get_raw_str(&extensions_json)); + + // Clean up. + ten_string_deinit(&extensions_json); +} + void ten_extension_thread_add_all_created_extensions( ten_extension_thread_t *self) { TEN_ASSERT(self, "Invalid argument."); @@ -501,16 +561,14 @@ void ten_extension_thread_add_all_created_extensions( ten_extension_context_t *extension_context = self->extension_context; TEN_ASSERT(extension_context, "Should not happen."); - TEN_ASSERT( - // TEN_NOLINTNEXTLINE(thread-check) - // thread-check: We are in the extension thread, and throughout the entire - // lifecycle of the extension, the extension_context where the extension - // resides remains unchanged. Even in the closing flow, the - // extension_context is closed later than the extension itself. Therefore, - // using a pointer to the extension_context within the extension thread is - // thread-safe. - ten_extension_context_check_integrity(extension_context, false), - "Should not happen."); + // TEN_NOLINTNEXTLINE(thread-check) + // thread-check: We are in the extension thread, and throughout the entire + // lifecycle of the extension, the extension_context where the extension + // resides remains unchanged. Even in the closing flow, the extension_context + // is closed later than the extension itself. Therefore, using a pointer to + // the extension_context within the extension thread is thread-safe. + TEN_ASSERT(ten_extension_context_check_integrity(extension_context, false), + "Should not happen."); ten_list_foreach (&self->extensions, iter) { ten_extension_t *extension = ten_ptr_listnode_get(iter.node); @@ -525,6 +583,8 @@ void ten_extension_thread_add_all_created_extensions( ten_extension_thread_add_extension(self, extension); } + ten_extension_thread_log_graph_resources(self); + // Notify the engine to handle those newly created extensions. ten_engine_t *engine = extension_context->engine; diff --git a/core/src/ten_utils/log/formatter.c b/core/src/ten_utils/log/formatter.c index 1f80f01ea..39748c187 100644 --- a/core/src/ten_utils/log/formatter.c +++ b/core/src/ten_utils/log/formatter.c @@ -113,6 +113,9 @@ void ten_log_colored_formatter(ten_string_t *buf, TEN_LOG_LEVEL level, // Determine color based on log level. const char *level_color = NULL; switch (level) { + case TEN_LOG_LEVEL_MANDATORY: + level_color = TEN_LOG_COLOR_GOLD; + break; case TEN_LOG_LEVEL_FATAL: case TEN_LOG_LEVEL_ERROR: level_color = TEN_LOG_COLOR_RED; From ce31a85127d599bc3876cd1caf1213f43153ae10 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Fri, 2 May 2025 20:31:57 +0800 Subject: [PATCH 3/4] fix: refine codes --- core/src/ten_manager/src/lib.rs | 1 + core/src/ten_manager/src/log/mod.rs | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 core/src/ten_manager/src/log/mod.rs diff --git a/core/src/ten_manager/src/lib.rs b/core/src/ten_manager/src/lib.rs index a3bc47ce3..ca576c46d 100644 --- a/core/src/ten_manager/src/lib.rs +++ b/core/src/ten_manager/src/lib.rs @@ -33,6 +33,7 @@ pub mod fs; pub mod graph; pub mod http; mod install; +pub mod log; mod manifest_lock; pub mod output; mod package_file; diff --git a/core/src/ten_manager/src/log/mod.rs b/core/src/ten_manager/src/log/mod.rs new file mode 100644 index 000000000..375113a11 --- /dev/null +++ b/core/src/ten_manager/src/log/mod.rs @@ -0,0 +1,6 @@ +// +// Copyright © 2025 Agora +// This file is part of TEN Framework, an open source project. +// Licensed under the Apache License, Version 2.0, with certain conditions. +// Refer to the "LICENSE" file in the root directory for more information. +// From 2c89013e7704d63db1eca13b8e33671adfb97cfe Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Fri, 2 May 2025 21:07:18 +0800 Subject: [PATCH 4/4] fix: refine codes --- core/src/ten_manager/src/log/mod.rs | 20 +++++++++++++++++++ .../src/json_schema/data/property.schema.json | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/src/ten_manager/src/log/mod.rs b/core/src/ten_manager/src/log/mod.rs index 375113a11..27b20de27 100644 --- a/core/src/ten_manager/src/log/mod.rs +++ b/core/src/ten_manager/src/log/mod.rs @@ -4,3 +4,23 @@ // Licensed under the Apache License, Version 2.0, with certain conditions. // Refer to the "LICENSE" file in the root directory for more information. // +use anyhow::Result; +use std::collections::HashMap; + +pub struct ExtensionInfo { + pub thread_id: u64, +} + +pub struct GraphResourcesLog { + pub app_uri: Option, + pub graph_id: String, + pub graph_name: String, + pub extensions: HashMap, +} + +pub fn parse_graph_resources_log( + log_message: &str, + graph_resources_log: &mut GraphResourcesLog, +) -> Result<()> { + Ok(()) +} diff --git a/core/src/ten_rust/src/json_schema/data/property.schema.json b/core/src/ten_rust/src/json_schema/data/property.schema.json index 7435f2a5a..38ff0ab01 100644 --- a/core/src/ten_rust/src/json_schema/data/property.schema.json +++ b/core/src/ten_rust/src/json_schema/data/property.schema.json @@ -13,9 +13,10 @@ 6 ] }, - "non_localhost_uri": { + "non_empty_non_localhost_uri": { "type": "string", "format": "uri", + "minLength": 1, "not": { "const": "localhost" } @@ -501,7 +502,7 @@ "additionalProperties": false, "properties": { "uri": { - "$ref": "#/$defs/non_localhost_uri" + "$ref": "#/$defs/non_empty_non_localhost_uri" }, // @{ // TODO(Wei): The log_level and log_file below are for versions of ten