Skip to content

Commit

Permalink
chore: refine log API (#364)
Browse files Browse the repository at this point in the history
* chore: refine log API

* chore: refine log API

* chore: refine log API

* chore: refine log API

* chore: refine log API
  • Loading branch information
halajohn authored Dec 3, 2024
1 parent 95eb145 commit ecd0581
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 26 deletions.
5 changes: 0 additions & 5 deletions core/include/ten_runtime/binding/cpp/detail/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
40 changes: 40 additions & 0 deletions core/include_internal/ten_runtime/ten_env/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
16 changes: 10 additions & 6 deletions core/src/ten_runtime/addon/protocol/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/ten_runtime/app/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions core/src/ten_runtime/binding/go/interface/ten/ten_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions core/src/ten_runtime/binding/python/interface/ten/ten_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions core/src/ten_runtime/ten_env/internal/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}')
Expand Down

0 comments on commit ecd0581

Please sign in to comment.