diff --git a/core/include/ten_runtime/binding/cpp/detail/ten_env.h b/core/include/ten_runtime/binding/cpp/detail/ten_env.h index 5ae55365e0..3113e45609 100644 --- a/core/include/ten_runtime/binding/cpp/detail/ten_env.h +++ b/core/include/ten_runtime/binding/cpp/detail/ten_env.h @@ -774,11 +774,6 @@ class ten_env_t { (ten_env).log(TEN_LOG_LEVEL_FATAL, __func__, __FILE__, __LINE__, (msg)); \ } while (0) -#define TEN_ENV_LOG(ten_env, level, msg) \ - do { \ - (ten_env).log((level), __func__, __FILE__, __LINE__, (msg)); \ - } while (0) - void log(TEN_LOG_LEVEL level, const char *func_name, const char *file_name, size_t line_no, const char *msg) { TEN_ASSERT(c_ten_env, "Should not happen."); diff --git a/core/include_internal/ten_runtime/ten_env/log.h b/core/include_internal/ten_runtime/ten_env/log.h index 628b617fed..79a2e9c069 100644 --- a/core/include_internal/ten_runtime/ten_env/log.h +++ b/core/include_internal/ten_runtime/ten_env/log.h @@ -10,6 +10,42 @@ #include "ten_utils/log/log.h" +#define TEN_ENV_LOG_VERBOSE_INTERNAL(ten_env, ...) \ + do { \ + ten_env_log_formatted(ten_env, TEN_LOG_LEVEL_VERBOSE, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_ENV_LOG_DEBUG_INTERNAL(ten_env, ...) \ + do { \ + ten_env_log_formatted(ten_env, TEN_LOG_LEVEL_DEBUG, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_ENV_LOG_INFO_INTERNAL(ten_env, ...) \ + do { \ + ten_env_log_formatted(ten_env, TEN_LOG_LEVEL_INFO, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_ENV_LOG_WARN_INTERNAL(ten_env, ...) \ + do { \ + ten_env_log_formatted(ten_env, TEN_LOG_LEVEL_WARN, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_ENV_LOG_ERROR_INTERNAL(ten_env, ...) \ + do { \ + ten_env_log_formatted(ten_env, TEN_LOG_LEVEL_ERROR, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + +#define TEN_ENV_LOG_FATAL_INTERNAL(ten_env, ...) \ + do { \ + ten_env_log_formatted(ten_env, TEN_LOG_LEVEL_FATAL, __func__, __FILE__, \ + __LINE__, __VA_ARGS__); \ + } while (0) + typedef struct ten_env_t ten_env_t; TEN_RUNTIME_API void ten_env_log_with_size_formatted_without_check_thread( @@ -22,6 +58,10 @@ TEN_RUNTIME_API void ten_env_log_with_size_formatted( size_t func_name_len, const char *file_name, size_t file_name_len, size_t line_no, const char *fmt, ...); +TEN_RUNTIME_PRIVATE_API void ten_env_log_formatted( + ten_env_t *self, TEN_LOG_LEVEL level, const char *func_name, + const char *file_name, size_t line_no, const char *fmt, ...); + TEN_RUNTIME_API void ten_env_log_without_check_thread( ten_env_t *self, TEN_LOG_LEVEL level, const char *func_name, const char *file_name, size_t line_no, const char *msg); diff --git a/core/src/ten_runtime/addon/protocol/protocol.c b/core/src/ten_runtime/addon/protocol/protocol.c index ced0de3eda..b431ab42c0 100644 --- a/core/src/ten_runtime/addon/protocol/protocol.c +++ b/core/src/ten_runtime/addon/protocol/protocol.c @@ -10,6 +10,7 @@ #include "include_internal/ten_runtime/addon/common/store.h" #include "include_internal/ten_runtime/common/constant_str.h" #include "include_internal/ten_runtime/protocol/protocol.h" +#include "include_internal/ten_runtime/ten_env/log.h" #include "include_internal/ten_runtime/ten_env/ten_env.h" #include "ten_utils/container/list.h" #include "ten_utils/container/list_node.h" @@ -183,7 +184,8 @@ bool ten_addon_create_protocol_with_uri( TEN_ENV_ATTACH_TO attach_to = ten_env_get_attach_to(ten_env); if (attach_to != TEN_ENV_ATTACH_TO_APP && attach_to != TEN_ENV_ATTACH_TO_ENGINE) { - TEN_LOGE("Invalid ten_env attach_to: %d", attach_to); + TEN_ENV_LOG_ERROR_INTERNAL(ten_env, "Invalid ten_env attach_to: %d", + attach_to); if (err) { ten_error_set(err, TEN_ERRNO_INVALID_ARGUMENT, "Invalid ten_env."); } @@ -194,8 +196,9 @@ bool ten_addon_create_protocol_with_uri( ten_addon_host_t *addon_host = ten_addon_protocol_find(ten_string_get_raw_str(protocol_str)); if (!addon_host) { - TEN_LOGE("Can not handle protocol '%s' because no addon installed for it", - uri); + TEN_ENV_LOG_ERROR_INTERNAL( + ten_env, + "Can not handle protocol '%s' because no addon installed for it", uri); ten_string_destroy(protocol_str); if (err) { @@ -205,8 +208,8 @@ bool ten_addon_create_protocol_with_uri( return false; } - TEN_LOGD("Loading protocol addon: %s", - ten_string_get_raw_str(&addon_host->name)); + TEN_ENV_LOG_ERROR_INTERNAL(ten_env, "Loading protocol addon: %s", + ten_string_get_raw_str(&addon_host->name)); ten_string_destroy(protocol_str); @@ -220,7 +223,8 @@ bool ten_addon_create_protocol_with_uri( proxy_on_addon_protocol_created, info); if (!rc) { - TEN_LOGE("Failed to create protocol for %s", uri); + TEN_ENV_LOG_ERROR_INTERNAL(ten_env, "Failed to create protocol for %s", + uri); ten_addon_create_protocol_info_destroy(info); if (err) { diff --git a/core/src/ten_runtime/app/ten_env/on_xxx.c b/core/src/ten_runtime/app/ten_env/on_xxx.c index 453e3bcf6b..55016167ec 100644 --- a/core/src/ten_runtime/app/ten_env/on_xxx.c +++ b/core/src/ten_runtime/app/ten_env/on_xxx.c @@ -24,6 +24,7 @@ #include "include_internal/ten_runtime/protocol/close.h" #include "include_internal/ten_runtime/protocol/protocol.h" #include "include_internal/ten_runtime/schema_store/store.h" +#include "include_internal/ten_runtime/ten_env/log.h" #include "include_internal/ten_runtime/ten_env/metadata_cb.h" #include "include_internal/ten_runtime/ten_env/ten_env.h" #include "ten_runtime/app/app.h" @@ -317,7 +318,7 @@ void ten_app_on_deinit_done(ten_env_t *ten_env) { self->state = TEN_APP_STATE_CLOSED; ten_mutex_unlock(self->state_lock); - TEN_LOGD("app::on_deinit_done()."); + TEN_ENV_LOG_DEBUG_INTERNAL(ten_env, "app on_deinit_done()."); ten_env_close(self->ten_env); ten_runloop_stop(self->loop); diff --git a/core/src/ten_runtime/binding/go/interface/ten/ten_env.go b/core/src/ten_runtime/binding/go/interface/ten/ten_env.go index 4ca3529294..b93f1e72a8 100644 --- a/core/src/ten_runtime/binding/go/interface/ten/ten_env.go +++ b/core/src/ten_runtime/binding/go/interface/ten/ten_env.go @@ -52,7 +52,6 @@ type TenEnv interface { LogWarn(msg string) LogError(msg string) LogFatal(msg string) - Log(level LogLevel, msg string) // Private functions. postSyncJob(payload job) any @@ -389,10 +388,6 @@ func (p *tenEnv) LogFatal(msg string) { p.logInternal(LogLevelFatal, msg, 2) } -func (p *tenEnv) Log(level LogLevel, msg string) { - p.logInternal(level, msg, 1) -} - func (p *tenEnv) logInternal(level LogLevel, msg string, skip int) { // Get caller info. pc, fileName, lineNo, ok := runtime.Caller(skip) diff --git a/core/src/ten_runtime/binding/python/interface/ten/ten_env.py b/core/src/ten_runtime/binding/python/interface/ten/ten_env.py index 7f0a3fa22f..ca014687bd 100644 --- a/core/src/ten_runtime/binding/python/interface/ten/ten_env.py +++ b/core/src/ten_runtime/binding/python/interface/ten/ten_env.py @@ -163,9 +163,6 @@ def log_error(self, msg: str) -> None: def log_fatal(self, msg: str) -> None: self._log_internal(LogLevel.FATAL, msg, 2) - def log(self, level: LogLevel, msg: str) -> None: - self._log_internal(level, msg, 1) - def _log_internal(self, level: LogLevel, msg: str, skip: int) -> None: # Get the current frame. frame = inspect.currentframe() diff --git a/core/src/ten_runtime/ten_env/internal/log.c b/core/src/ten_runtime/ten_env/internal/log.c index b00897fd4a..b4cb75e5e8 100644 --- a/core/src/ten_runtime/ten_env/internal/log.c +++ b/core/src/ten_runtime/ten_env/internal/log.c @@ -102,3 +102,16 @@ void ten_env_log_with_size_formatted(ten_env_t *self, TEN_LOG_LEVEL level, va_end(ap); } + +void ten_env_log_formatted(ten_env_t *self, TEN_LOG_LEVEL level, + const char *func_name, const char *file_name, + size_t line_no, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + + ten_env_log_with_size_formatted(self, level, func_name, strlen(func_name), + file_name, strlen(file_name), line_no, fmt, + ap); + + va_end(ap); +} diff --git a/tests/ten_runtime/integration/python/no_dest_python/no_dest_python_app/ten_packages/extension/default_extension_python/extension.py b/tests/ten_runtime/integration/python/no_dest_python/no_dest_python_app/ten_packages/extension/default_extension_python/extension.py index bc364755e9..ffb7b04e2c 100644 --- a/tests/ten_runtime/integration/python/no_dest_python/no_dest_python_app/ten_packages/extension/default_extension_python/extension.py +++ b/tests/ten_runtime/integration/python/no_dest_python/no_dest_python_app/ten_packages/extension/default_extension_python/extension.py @@ -25,9 +25,7 @@ def __init__(self, name: str) -> None: self.name = name def on_configure(self, ten_env: TenEnv) -> None: - ten_env.log( - LogLevel.DEBUG, f"DefaultExtension on_init, name: {self.name}" - ) + ten_env.log_debug(f"DefaultExtension on_init, name: {self.name}") assert self.name == "default_extension_python" ten_env.init_property_from_json('{"testKey": "testValue"}') diff --git a/tests/ten_runtime/integration/python/send_cmd_python/send_cmd_python_app/ten_packages/extension/default_extension_python/extension.py b/tests/ten_runtime/integration/python/send_cmd_python/send_cmd_python_app/ten_packages/extension/default_extension_python/extension.py index aa7e796298..db659df3ef 100644 --- a/tests/ten_runtime/integration/python/send_cmd_python/send_cmd_python_app/ten_packages/extension/default_extension_python/extension.py +++ b/tests/ten_runtime/integration/python/send_cmd_python/send_cmd_python_app/ten_packages/extension/default_extension_python/extension.py @@ -22,9 +22,7 @@ def __init__(self, name: str) -> None: self.name = name def on_configure(self, ten_env: TenEnv) -> None: - ten_env.log( - LogLevel.DEBUG, f"DefaultExtension on_init, name: {self.name}" - ) + ten_env.log_debug(f"DefaultExtension on_init, name: {self.name}") assert self.name == "default_extension_python" ten_env.init_property_from_json('{"testKey": "testValue"}')