Skip to content

Commit 7e734b8

Browse files
committed
fix: refine codes
1 parent 68ec0b7 commit 7e734b8

File tree

9 files changed

+109
-32
lines changed

9 files changed

+109
-32
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"request": "launch",
138138
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
139139
"args": [
140-
"--gtest_filter=BasicTest.ThrowExceptionInExtension"
140+
"--gtest_filter=BasicTest.EmptyExtensionGroup"
141141
],
142142
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
143143
"env": {

core/include_internal/ten_utils/log/termcolor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
#define TEN_LOG_COLOR_MAGENTA "\033[35m"
1717
#define TEN_LOG_COLOR_CYAN "\033[36m"
1818
#define TEN_LOG_COLOR_WHITE "\033[37m"
19+
#define TEN_LOG_COLOR_GOLD "\033[1;33m"

core/src/ten_runtime/extension/internal/close.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ static void ten_extension_do_close(ten_extension_t *self) {
9494
"Should not happen.");
9595

9696
ten_extension_thread_t *extension_thread = self->extension_thread;
97-
TEN_ASSERT(extension_thread &&
98-
ten_extension_thread_check_integrity(extension_thread, true),
97+
TEN_ASSERT(extension_thread, "Should not happen.");
98+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
9999
"Should not happen.");
100100

101101
ten_extension_on_deinit(self);

core/src/ten_runtime/extension/internal/path_timer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ ten_timer_t *ten_extension_create_timer_for_in_path(ten_extension_t *self) {
118118
"Should not happen.");
119119

120120
ten_extension_thread_t *extension_thread = self->extension_thread;
121-
TEN_ASSERT(extension_thread &&
122-
ten_extension_thread_check_integrity(extension_thread, true),
121+
TEN_ASSERT(extension_thread, "Should not happen.");
122+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
123123
"Should not happen.");
124124

125125
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) {
138138
"Should not happen.");
139139

140140
ten_extension_thread_t *extension_thread = self->extension_thread;
141-
TEN_ASSERT(extension_thread &&
142-
ten_extension_thread_check_integrity(extension_thread, true),
141+
TEN_ASSERT(extension_thread, "Should not happen.");
142+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
143143
"Should not happen.");
144144

145145
ten_timer_t *timer = ten_timer_create(

core/src/ten_runtime/extension/ten_env/on_xxx.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ bool ten_extension_on_configure_done(ten_env_t *self) {
9797
extension->state = TEN_EXTENSION_STATE_ON_CONFIGURE_DONE;
9898

9999
ten_extension_thread_t *extension_thread = extension->extension_thread;
100-
TEN_ASSERT(extension_thread &&
101-
ten_extension_thread_check_integrity(extension_thread, true),
100+
TEN_ASSERT(extension_thread, "Should not happen.");
101+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
102102
"Should not happen.");
103103

104104
if (extension_thread->is_close_triggered) {
@@ -249,8 +249,8 @@ bool ten_extension_on_init_done(ten_env_t *self) {
249249
extension->state = TEN_EXTENSION_STATE_ON_INIT_DONE;
250250

251251
ten_extension_thread_t *extension_thread = extension->extension_thread;
252-
TEN_ASSERT(extension_thread &&
253-
ten_extension_thread_check_integrity(extension_thread, true),
252+
TEN_ASSERT(extension_thread, "Should not happen.");
253+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
254254
"Should not happen.");
255255

256256
if (extension_thread->is_close_triggered) {
@@ -308,8 +308,8 @@ bool ten_extension_on_start_done(ten_env_t *self) {
308308
extension->state = TEN_EXTENSION_STATE_ON_START_DONE;
309309

310310
ten_extension_thread_t *extension_thread = extension->extension_thread;
311-
TEN_ASSERT(extension_thread &&
312-
ten_extension_thread_check_integrity(extension_thread, true),
311+
TEN_ASSERT(extension_thread, "Should not happen.");
312+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
313313
"Should not happen.");
314314

315315
if (extension_thread->is_close_triggered) {

core/src/ten_runtime/extension_context/extension_context.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "include_internal/ten_runtime/extension_context/extension_context.h"
88

99
#include <stddef.h>
10+
#include <stdio.h>
1011
#include <stdlib.h>
1112

1213
#include "include_internal/ten_runtime/addon/addon.h"
@@ -336,7 +337,19 @@ static void ten_extension_context_log_graph_resources(
336337
TEN_ASSERT(ten_extension_context_check_integrity(self, true),
337338
"Invalid use of extension_context %p.", self);
338339

339-
TEN_LOGM("[graph resources] %s, ", ten_engine_get_id(self->engine, true));
340+
// Get the required information.
341+
const char *app_uri = ten_app_get_uri(self->engine->app);
342+
const char *graph_id = ten_engine_get_id(self->engine, true);
343+
const char *graph_name = ten_string_get_raw_str(&self->engine->graph_name);
344+
345+
// Log the complete JSON in a single call.
346+
TEN_LOGM(
347+
"[graph resources] {"
348+
"\"app_uri\": \"%s\", "
349+
"\"graph name\": \"%s\", "
350+
"\"graph id\": \"%s\" "
351+
"}",
352+
app_uri, graph_name, graph_id);
340353
}
341354

342355
static void ten_extension_context_create_extension_group_done(

core/src/ten_runtime/extension_group/ten_env/on_xxx.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ void ten_extension_group_on_init_done(ten_env_t *self) {
7878
ten_extension_group_get_name(extension_group, true));
7979

8080
ten_extension_thread_t *extension_thread = extension_group->extension_thread;
81-
TEN_ASSERT(extension_thread &&
82-
ten_extension_thread_check_integrity(extension_thread, true),
81+
TEN_ASSERT(extension_thread, "Should not happen.");
82+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
8383
"Should not happen.");
8484

8585
int rc = ten_runloop_post_task_tail(
@@ -131,8 +131,8 @@ bool ten_extension_group_on_deinit_done(ten_env_t *self) {
131131
}
132132

133133
ten_extension_thread_t *extension_thread = extension_group->extension_thread;
134-
TEN_ASSERT(extension_thread &&
135-
ten_extension_thread_check_integrity(extension_thread, true),
134+
TEN_ASSERT(extension_thread, "Should not happen.");
135+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
136136
"Should not happen.");
137137

138138
// 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,
160160
ten_string_get_raw_str(&self->name));
161161

162162
ten_extension_thread_t *extension_thread = self->extension_thread;
163-
TEN_ASSERT(extension_thread &&
164-
ten_extension_thread_check_integrity(extension_thread, true),
163+
TEN_ASSERT(extension_thread, "Should not happen.");
164+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
165165
"Should not happen.");
166166

167167
// Remove the extensions that were not successfully created from the list of
@@ -212,8 +212,8 @@ void ten_extension_group_on_destroy_extensions_done(
212212
ten_string_get_raw_str(&self->name));
213213

214214
ten_extension_thread_t *extension_thread = self->extension_thread;
215-
TEN_ASSERT(extension_thread &&
216-
ten_extension_thread_check_integrity(extension_thread, true),
215+
TEN_ASSERT(extension_thread, "Should not happen.");
216+
TEN_ASSERT(ten_extension_thread_check_integrity(extension_thread, true),
217217
"Should not happen.");
218218

219219
int rc = ten_runloop_post_task_tail(

core/src/ten_runtime/extension_thread/extension_thread.c

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "include_internal/ten_runtime/extension_thread/msg_interface/common.h"
2727
#include "include_internal/ten_runtime/msg/msg.h"
2828
#include "include_internal/ten_runtime/ten_env/ten_env.h"
29+
#include "include_internal/ten_utils/log/log.h"
2930
#include "include_internal/ten_utils/sanitizer/thread_check.h"
3031
#include "ten_runtime/extension/extension.h"
3132
#include "ten_runtime/ten_env/ten_env.h"
@@ -493,6 +494,65 @@ void ten_extension_thread_start_life_cycle_of_all_extensions_task(
493494
}
494495
}
495496

497+
static void ten_extension_thread_log_graph_resources(
498+
ten_extension_thread_t *self) {
499+
TEN_ASSERT(self, "Invalid argument.");
500+
TEN_ASSERT(ten_extension_thread_check_integrity(self, true),
501+
"Invalid use of extension_thread %p.", self);
502+
503+
// Get the required information.
504+
const char *app_uri = ten_app_get_uri(self->extension_context->engine->app);
505+
const char *graph_id =
506+
ten_engine_get_id(self->extension_context->engine, false);
507+
const char *graph_name =
508+
ten_string_get_raw_str(&self->extension_context->engine->graph_name);
509+
510+
// Build extension list entries.
511+
ten_string_t extensions_json;
512+
ten_string_init(&extensions_json);
513+
514+
bool first_extension = true;
515+
516+
ten_list_foreach (&self->extensions, iter) {
517+
ten_extension_t *extension = ten_ptr_listnode_get(iter.node);
518+
TEN_ASSERT(extension, "Should not happen.");
519+
TEN_ASSERT(ten_extension_check_integrity(extension, true),
520+
"Invalid use of extension %p.", extension);
521+
522+
// Add comma for all but the first extension.
523+
if (!first_extension) {
524+
ten_string_append_formatted(&extensions_json, "%s", ", ");
525+
}
526+
first_extension = false;
527+
528+
// Add extension entry with thread_id.
529+
char entry[256];
530+
int written =
531+
snprintf(entry, sizeof(entry), "\"%s\": {\"thread_id\": %lld}",
532+
ten_extension_get_name(extension, true), (long long)self->tid);
533+
534+
if (written < 0 || written >= (int)sizeof(entry)) {
535+
TEN_LOGE("Error formatting extension entry or buffer too small");
536+
continue;
537+
}
538+
539+
ten_string_append_formatted(&extensions_json, "%s", entry);
540+
}
541+
542+
// Log the complete JSON in a single call.
543+
TEN_LOGM(
544+
"[graph resources] {"
545+
"\"app_uri\": \"%s\", "
546+
"\"graph name\": \"%s\", "
547+
"\"graph id\": \"%s\", "
548+
"\"extensions\": {%s}"
549+
"}",
550+
app_uri, graph_name, graph_id, ten_string_get_raw_str(&extensions_json));
551+
552+
// Clean up.
553+
ten_string_deinit(&extensions_json);
554+
}
555+
496556
void ten_extension_thread_add_all_created_extensions(
497557
ten_extension_thread_t *self) {
498558
TEN_ASSERT(self, "Invalid argument.");
@@ -501,16 +561,14 @@ void ten_extension_thread_add_all_created_extensions(
501561

502562
ten_extension_context_t *extension_context = self->extension_context;
503563
TEN_ASSERT(extension_context, "Should not happen.");
504-
TEN_ASSERT(
505-
// TEN_NOLINTNEXTLINE(thread-check)
506-
// thread-check: We are in the extension thread, and throughout the entire
507-
// lifecycle of the extension, the extension_context where the extension
508-
// resides remains unchanged. Even in the closing flow, the
509-
// extension_context is closed later than the extension itself. Therefore,
510-
// using a pointer to the extension_context within the extension thread is
511-
// thread-safe.
512-
ten_extension_context_check_integrity(extension_context, false),
513-
"Should not happen.");
564+
// TEN_NOLINTNEXTLINE(thread-check)
565+
// thread-check: We are in the extension thread, and throughout the entire
566+
// lifecycle of the extension, the extension_context where the extension
567+
// resides remains unchanged. Even in the closing flow, the extension_context
568+
// is closed later than the extension itself. Therefore, using a pointer to
569+
// the extension_context within the extension thread is thread-safe.
570+
TEN_ASSERT(ten_extension_context_check_integrity(extension_context, false),
571+
"Should not happen.");
514572

515573
ten_list_foreach (&self->extensions, iter) {
516574
ten_extension_t *extension = ten_ptr_listnode_get(iter.node);
@@ -525,6 +583,8 @@ void ten_extension_thread_add_all_created_extensions(
525583
ten_extension_thread_add_extension(self, extension);
526584
}
527585

586+
ten_extension_thread_log_graph_resources(self);
587+
528588
// Notify the engine to handle those newly created extensions.
529589

530590
ten_engine_t *engine = extension_context->engine;

core/src/ten_utils/log/formatter.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ void ten_log_colored_formatter(ten_string_t *buf, TEN_LOG_LEVEL level,
113113
// Determine color based on log level.
114114
const char *level_color = NULL;
115115
switch (level) {
116+
case TEN_LOG_LEVEL_MANDATORY:
117+
level_color = TEN_LOG_COLOR_GOLD;
118+
break;
116119
case TEN_LOG_LEVEL_FATAL:
117120
case TEN_LOG_LEVEL_ERROR:
118121
level_color = TEN_LOG_COLOR_RED;

0 commit comments

Comments
 (0)