Skip to content
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

feat: new log mechanism #18

Merged
merged 14 commits into from
Sep 17, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ IndentAccessModifiers: false
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: BeforeHash
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 2
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_utils_unit_test",
"cwd": "${workspaceFolder}/out/linux/x64",
"args": [
"--gtest_filter=SchemaTest.ValidStringType"
"--gtest_filter=ThreadTest.negative"
],
"env": {
"LD_LIBRARY_PATH": "${workspaceFolder}/out/linux/x64/tests/standalone/",
Expand Down
3 changes: 1 addition & 2 deletions core/include/ten_runtime/binding/cpp/internal/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
#include <cstddef>
#include <string>

#include "ten_utils/macro/check.h"
#include "ten_runtime/app/app.h"
#include "ten_runtime/binding/common.h"
#include "ten_runtime/binding/cpp/internal/common.h"
#include "ten_runtime/binding/cpp/internal/extension_group.h"
#include "ten_runtime/ten.h"
#include "ten_utils/log/log.h"
#include "ten_utils/macro/check.h"

using ten_app_t = struct ten_app_t;

Expand Down
3 changes: 1 addition & 2 deletions core/include/ten_runtime/binding/cpp/internal/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <utility>

#include "ten_utils/macro/check.h"
#include "ten_runtime/binding/common.h"
#include "ten_runtime/binding/cpp/internal/common.h"
#include "ten_runtime/binding/cpp/internal/msg/audio_frame.h"
Expand All @@ -26,8 +27,6 @@
#include "ten_runtime/ten_env/ten_env.h"
#include "ten_utils/lib/json.h"
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/log/log.h"
#include "ten_utils/macro/check.h"

using ten_json_t = ::ten_json_t;
using ten_env_t = struct ten_env_t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

#include <cstddef>

#include "ten_utils/macro/check.h"
#include "ten_runtime/binding/common.h"
#include "ten_runtime/binding/cpp/internal/ten_env.h"
#include "ten_runtime/extension_group/extension_group.h"
#include "ten_utils/container/list.h"
#include "ten_utils/macro/check.h"

// NOLINTNEXTLINE(bugprone-forward-declaration-namespace)
using ten_extension_group_t = struct ten_extension_group_t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

#include <memory>

#include "ten_utils/macro/check.h"
#include "ten_runtime/binding/cpp/internal/msg/msg.h"
#include "ten_runtime/msg/cmd/cmd.h"
#include "ten_runtime/msg/msg.h"
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/macro/check.h"

namespace ten {

Expand Down
2 changes: 1 addition & 1 deletion core/include/ten_runtime/binding/cpp/internal/msg/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

#include <string>

#include "ten_utils/macro/check.h"
#include "ten_runtime/common/errno.h"
#include "ten_runtime/msg/msg.h"
#include "ten_utils/lang/cpp/lib/error.h"
#include "ten_utils/lang/cpp/lib/value.h"
#include "ten_utils/lib/buf.h"
#include "ten_utils/lib/json.h"
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/value/value.h"
#include "ten_utils/value/value_get.h"
#include "ten_utils/value/value_json.h"
Expand Down
43 changes: 43 additions & 0 deletions core/include/ten_runtime/binding/cpp/internal/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include "ten_utils/lang/cpp/lib/value.h"
#include "ten_utils/lib/buf.h"
#include "ten_utils/lib/error.h"
#include "ten_utils/log/log.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"
#include "ten_utils/value/value.h"
#include "ten_utils/value/value_json.h"

Expand Down Expand Up @@ -1031,6 +1033,47 @@ class ten_env_t {

void *get_attached_target() { return get_attached_target(nullptr); }

#define TEN_ENV_LOG_VERBOSE(ten_env, msg) \
do { \
(ten_env).log(TEN_LOG_LEVEL_VERBOSE, __func__, __FILE__, __LINE__, (msg)); \
} while (0)

#define TEN_ENV_LOG_DEBUG(ten_env, msg) \
do { \
(ten_env).log(TEN_LOG_LEVEL_DEBUG, __func__, __FILE__, __LINE__, (msg)); \
} while (0)

#define TEN_ENV_LOG_INFO(ten_env, msg) \
do { \
(ten_env).log(TEN_LOG_LEVEL_INFO, __func__, __FILE__, __LINE__, (msg)); \
} while (0)

#define TEN_ENV_LOG_WARN(ten_env, msg) \
do { \
(ten_env).log(TEN_LOG_LEVEL_WARN, __func__, __FILE__, __LINE__, (msg)); \
} while (0)

#define TEN_ENV_LOG_ERROR(ten_env, msg) \
do { \
(ten_env).log(TEN_LOG_LEVEL_ERROR, __func__, __FILE__, __LINE__, (msg)); \
} while (0)

#define TEN_ENV_LOG_FATAL(ten_env, msg) \
do { \
(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.");
ten_env_log(c_ten_env, level, func_name, file_name, line_no, msg);
}

private:
friend class ten_env_proxy_t;
friend class ten_env_mock_t;
Expand Down
16 changes: 16 additions & 0 deletions core/include/ten_runtime/ten_env/internal/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// This file is part of the TEN Framework project.
// See https://github.com/TEN-framework/ten_framework/LICENSE for license
// information.
//
#pragma once

#include "ten_runtime/ten_config.h"

#include "ten_utils/log/log.h"

typedef struct ten_env_t ten_env_t;

TEN_RUNTIME_API void ten_env_log(ten_env_t *self, TEN_LOG_LEVEL level,
const char *func_name, const char *file_name,
size_t line_no, const char *msg);
1 change: 1 addition & 0 deletions core/include/ten_runtime/ten_env/ten_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ten_runtime/ten_config.h"

#include "ten_runtime/common/errno.h" // IWYU pragma: keep
#include "ten_runtime/ten_env/internal/log.h" // IWYU pragma: keep
#include "ten_runtime/ten_env/internal/metadata.h" // IWYU pragma: keep
#include "ten_runtime/ten_env/internal/on_xxx_done.h" // IWYU pragma: keep
#include "ten_runtime/ten_env/internal/return.h" // IWYU pragma: keep
Expand Down
123 changes: 0 additions & 123 deletions core/include/ten_utils/backtrace/backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,131 +17,8 @@
extern "C" {
#endif

typedef struct ten_backtrace_t ten_backtrace_t;

/**
* @brief The type of the callback to be called when one backtrace frame is
* dumping.
*
* @param ten_backtrace
* @param pc The program counter.
* @param filename The name of the file containing @a pc, or NULL if not
* available.
* @param LINENO The line number in @a filename containing @a pc, or 0 if not
* available.
* @param function The name of the function containing @a pc, or NULL if
* not available.
*
* @return 0 to continuing tracing.
*
* @note The @a filename and @a function buffers may become invalid after this
* function returns.
*/
typedef int (*ten_backtrace_dump_file_line_func_t)(
ten_backtrace_t *ten_backtrace, uintptr_t pc, const char *filename,
int lineno, const char *function, void *data);

/**
* @brief The type of the callback to be called when one symbol information is
* dumping.
*
* @param ten_backtrace
* @param pc The program counter.
* @param sym_name The name of the symbol for the corresponding code.
* @param sym_val The value of the symbol.
* @param sym_size The size of the symbol.
*
* @note @a symname will be NULL if no error occurred but the symbol could not
* be found.
*/
typedef void (*ten_backtrace_dump_syminfo_func_t)(
ten_backtrace_t *ten_backtrace, uintptr_t pc, const char *sym_name,
uintptr_t sym_val, uintptr_t sym_size, void *data);

/**
* @brief The type of the error callback to be called when backtrace module
* encounters errors. This function, if not NULL, will be called for certain
* error cases.
*
* @param ten_backtrace
* @param msg An error message.
* @param errnum if greater than 0, holds an errno value.
*
* @note The @a msg buffer may become invalid after this function returns.
* @note As a special case, the @a errnum argument will be passed as -1 if no
* debug info can be found for the executable, or if the debug info exists but
* has an unsupported version, but the function requires debug info (e.g.,
* ten_backtrace_dump). The @a msg in this case will be something along the
* lines of "no debug info".
* @note Similarly, @a errnum will be passed as -1 if there is no symbol table,
* but the function requires a symbol table (e.g., backtrace_syminfo). This may
* be used as a signal that some other approach should be tried.
*/
typedef void (*ten_backtrace_error_func_t)(ten_backtrace_t *self,
const char *msg, int errnum,
void *data);

/**
* @brief Given @a pc, a program counter in the current program, call the
* @a dump_file_line_cb function with filename, line number, and function name
* information. This will normally call the callback function exactly once.
* However, if the @a pc happens to describe an inlined call, and the debugging
* information contains the necessary information, then this may call the
* callback function multiple times. This will make at least one call to either
* @a dump_file_line_cb or @a error_cb.
*
* @return The first non-zero value returned by @a dump_file_line_cb or @a
* error_cb, or 0.
*/
TEN_UTILS_API int ten_backtrace_get_file_line_info(
ten_backtrace_t *self, uintptr_t pc,
ten_backtrace_dump_file_line_func_t dump_file_line_cb,
ten_backtrace_error_func_t error_cb, void *data);

/**
* @brief Given @a pc, an address or program counter in the current program,
* call the callback information with the symbol name and value describing the
* function or variable in which @a pc may be found.
* This will call either @a dump_syminfo_cb or @a error_cb exactly once.
*
* @return 1 on success, 0 on failure.
*
* @note This function requires the symbol table but does not require the debug
* info. Note that if the symbol table is present but @a pc could not be found
* in the table, @a dump_syminfo_cb will be called with a NULL @a sym_name
* argument. Returns 1 on success, 0 on error.
*/
TEN_UTILS_API int ten_backtrace_get_syminfo(
ten_backtrace_t *self, uintptr_t pc,
ten_backtrace_dump_syminfo_func_t dump_syminfo_cb,
ten_backtrace_error_func_t error_cb, void *data);

TEN_UTILS_API void ten_backtrace_create_global(void);

TEN_UTILS_API ten_backtrace_t *ten_backtrace_create(void);

TEN_UTILS_API void ten_backtrace_destroy_global(void);

TEN_UTILS_API void ten_backtrace_destroy(ten_backtrace_t *self);

TEN_UTILS_API void ten_backtrace_dump_global(size_t skip);

/**
* @brief Get a full stack backtrace.
*
* @param skip The number of frames to skip. Passing 0 will start the trace with
* the function calling ten_backtrace_dump.
*
* @note If any call to 'dump' callback returns a non-zero value, the stack
* backtrace stops, and backtrace returns that value; this may be used to
* limit the number of stack frames desired.
* @note If all calls to 'dump' callback return 0, backtrace returns 0. The
* ten_backtrace_dump function will make at least one call to either
* 'dump' callback or 'error' callback.
* @note This function requires debug info for the executable.
*/
TEN_UTILS_API void ten_backtrace_dump(ten_backtrace_t *self, size_t skip);

#ifdef __cplusplus
} /* End extern "C". */
#endif
2 changes: 1 addition & 1 deletion core/include/ten_utils/io/runloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEN_UTILS_API void ten_runloop_async_destroy(ten_runloop_async_t *async);
* @brief Notify the signal.
* @param async The signal.
* @return 0 if success, -1 otherwise.
* @note The signal callback will be called in the thread of |loop| that inited
* @note The signal callback will be called in the thread of |loop| that initted
*/
TEN_UTILS_API int ten_runloop_async_notify(ten_runloop_async_t *async);

Expand Down
2 changes: 1 addition & 1 deletion core/include/ten_utils/lang/cpp/lib/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
#include <vector>

#include "buf.h"
#include "ten_utils/macro/check.h"
#include "ten_runtime/common/errno.h"
#include "ten_utils/lib/alloc.h"
#include "ten_utils/lib/buf.h"
#include "ten_utils/lib/error.h"
#include "ten_utils/lib/json.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/value/value.h"
#include "ten_utils/value/value_get.h"
#include "ten_utils/value/value_is.h"
Expand Down
4 changes: 2 additions & 2 deletions core/include/ten_utils/lib/ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ typedef struct ten_ref_t {
ten_atomic_t ref_cnt;

// The object which is managed by this 'ten_ref_t'. This field should _not_ be
// modified after 'ten_ref_t' has been inited, therefore, we don't need to
// modified after 'ten_ref_t' has been initted, therefore, we don't need to
// care about its thread safety.
void *supervisee;

// This function will be called when the end-of-life of 'supervisee' is
// reached. This field should _not_ be modified after 'ten_ref_t' has been
// inited, therefore, we don't need to care about its thread safety.
// initted, therefore, we don't need to care about its thread safety.
ten_ref_on_end_of_life_func_t on_end_of_life;
} ten_ref_t;

Expand Down
8 changes: 4 additions & 4 deletions core/include/ten_utils/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include <stdbool.h>
#include <string.h>

#include "ten_utils/macro/check.h"
#include "ten_utils/lib/buf.h"
#include "ten_utils/lib/signature.h"
#include "ten_utils/macro/check.h"

#define TEN_STRING_SIGNATURE 0x178445C0402E320DU
#define TEN_STRING_PRE_BUF_SIZE 256
Expand Down Expand Up @@ -119,8 +119,8 @@ TEN_UTILS_API void ten_string_clear(ten_string_t *self);
*/
TEN_UTILS_API void ten_string_reserve(ten_string_t *self, size_t extra);

TEN_UTILS_API void ten_string_set_from_va_list(ten_string_t *self,
const char *fmt, va_list ap);
TEN_UTILS_API void ten_string_append_from_va_list(ten_string_t *self,
const char *fmt, va_list ap);

/**
* @brief Set the string object with a c string.
Expand Down Expand Up @@ -235,7 +235,7 @@ inline const char *ten_string_get_raw_str(const ten_string_t *self) {
*/
inline size_t ten_string_len(const ten_string_t *self) {
TEN_ASSERT(self && ten_string_check_integrity(self), "Invalid argument.");
return self ? strlen(self->buf) : 0;
return self ? self->first_unused_idx : 0;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/include/ten_utils/lib/typed_list_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
//
#pragma once

#include "ten_utils/macro/check.h"
#include "ten_utils/ten_config.h"

#include "ten_utils/macro/check.h"

#define TEN_TYPED_LIST_NODE_SIGNATURE 0x3CE1EAC77F72D345U

/**
Expand Down
Loading