Skip to content

Commit

Permalink
feat: have an env variable to control if terminal log has color (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored and sunxilin committed Jan 14, 2025
1 parent 8fd8f48 commit 5013eaa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
10 changes: 10 additions & 0 deletions core/include_internal/ten_utils/log/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

#include "ten_utils/log/log.h"

typedef void (*ten_log_formatter_func_t)(ten_string_t *buf, TEN_LOG_LEVEL level,
const char *func_name,
size_t func_name_len,
const char *file_name,
size_t file_name_len, size_t line_no,
const char *msg, size_t msg_len);

TEN_UTILS_PRIVATE_API void ten_log_default_formatter(
ten_string_t *buf, TEN_LOG_LEVEL level, const char *func_name,
size_t func_name_len, const char *file_name, size_t file_name_len,
Expand All @@ -22,3 +29,6 @@ TEN_UTILS_PRIVATE_API void ten_log_colored_formatter(

TEN_UTILS_PRIVATE_API void ten_log_set_formatter(
ten_log_t *self, ten_log_formatter_func_t format_cb, void *user_data);

TEN_UTILS_PRIVATE_API ten_log_formatter_func_t
ten_log_get_formatter_by_name(const char *name);
29 changes: 29 additions & 0 deletions core/src/ten_utils/log/formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
#include "ten_utils/ten_config.h"

#include "include_internal/ten_utils/log/formatter.h"

#include <inttypes.h>
#include <time.h>

Expand All @@ -17,6 +19,33 @@
#include "ten_utils/lib/string.h"
#include "ten_utils/log/log.h"

typedef struct ten_log_formatter_entry_t {
const char *name;
ten_log_formatter_func_t formatter_func;
} ten_log_formatter_entry_t;

static ten_log_formatter_entry_t registered_formatters[] = {
{"default", ten_log_default_formatter},
{"color", ten_log_colored_formatter},
};

static const size_t registered_formatters_size =
sizeof(registered_formatters) / sizeof(ten_log_formatter_entry_t);

ten_log_formatter_func_t ten_log_get_formatter_by_name(const char *name) {
TEN_ASSERT(name, "Invalid argument.");

ten_log_formatter_func_t result = NULL;

for (size_t i = 0; i < registered_formatters_size; i++) {
if (strcmp(registered_formatters[i].name, name) == 0) {
return registered_formatters[i].formatter_func;
}
}

return NULL;
}

void ten_log_set_formatter(ten_log_t *self, ten_log_formatter_func_t format_cb,
void *user_data) {
TEN_ASSERT(self, "Invalid argument.");
Expand Down
23 changes: 20 additions & 3 deletions core/src/ten_utils/log/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,29 @@ void ten_log_output_to_stderr_cb(ten_string_t *msg, void *user_data) {
void ten_log_set_output_to_stderr(ten_log_t *self) {
ten_log_output_set(self, ten_log_output_to_stderr_cb, NULL, NULL);

ten_log_formatter_func_t formatter_func = NULL;

#if defined(OS_LINUX) || defined(OS_MACOS)
ten_log_set_formatter(self, ten_log_colored_formatter, NULL);
// ten_log_set_formatter(self, ten_log_default_formatter, NULL);
formatter_func = ten_log_colored_formatter;
#else
ten_log_set_formatter(self, ten_log_default_formatter, NULL);
formatter_func = ten_log_default_formatter;
#endif

// The default formatter for `stderr` can be overridden using the below
// environment variable.
const char *formatter_env = getenv("TEN_LOG_FORMATTER");
if (formatter_env) {
ten_log_formatter_func_t formatter_func_from_env =
ten_log_get_formatter_by_name(formatter_env);

// If the environment variable specifies a formatter, use it; otherwise,
// stick to the default.
if (formatter_func_from_env) {
formatter_func = formatter_func_from_env;
}
}

ten_log_set_formatter(self, formatter_func, NULL);
}

void ten_log_output_to_file_deinit(ten_log_t *self) {
Expand Down

0 comments on commit 5013eaa

Please sign in to comment.