Skip to content

refactor: introduce mandatory log level and enhance logging functionality across components #776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
38 changes: 2 additions & 36 deletions core/include/ten_utils/log/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions core/include_internal/ten_runtime/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
48 changes: 48 additions & 0 deletions core/include_internal/ten_utils/log/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions core/include_internal/ten_utils/log/termcolor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions core/src/ten_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions core/src/ten_manager/src/log/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// 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.
//
use anyhow::Result;
use std::collections::HashMap;

pub struct ExtensionInfo {
pub thread_id: u64,
}

pub struct GraphResourcesLog {
pub app_uri: Option<String>,
pub graph_id: String,
pub graph_name: String,
pub extensions: HashMap<String, ExtensionInfo>,
}

pub fn parse_graph_resources_log(
log_message: &str,
graph_resources_log: &mut GraphResourcesLog,
) -> Result<()> {
Ok(())
}
8 changes: 6 additions & 2 deletions core/src/ten_runtime/app/predefined_graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions core/src/ten_runtime/engine/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.");
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_runtime/extension/internal/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions core/src/ten_runtime/extension/internal/path_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions core/src/ten_runtime/extension/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 24 additions & 3 deletions core/src/ten_runtime/extension_context/extension_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "include_internal/ten_runtime/extension_context/extension_context.h"

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#include "include_internal/ten_runtime/addon/addon.h"
Expand All @@ -25,6 +26,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"
Expand Down Expand Up @@ -329,6 +331,27 @@ 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);

// 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(
ten_env_t *ten_env, ten_extension_group_t *extension_group) {
TEN_ASSERT(extension_group, "Should not happen.");
Expand Down Expand Up @@ -430,9 +453,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);
}
Expand Down
Loading
Loading