-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Copyright © 2024 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. | ||
// | ||
#pragma once | ||
|
||
#include "ten_utils/ten_config.h" | ||
|
||
#include "ten_utils/log/log.h" | ||
|
||
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, | ||
size_t line_no, const char *msg, size_t msg_len); | ||
|
||
TEN_UTILS_PRIVATE_API void ten_log_colored_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, | ||
size_t line_no, const char *msg, size_t msg_len); | ||
|
||
TEN_UTILS_PRIVATE_API void ten_log_set_formatter( | ||
ten_log_t *self, ten_log_formatter_func_t format_cb, void *user_data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Copyright © 2024 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. | ||
// | ||
#pragma once | ||
|
||
#include "ten_utils/ten_config.h" | ||
|
||
#define TEN_LOG_COLOR_RESET "\033[0m" | ||
#define TEN_LOG_COLOR_RED "\033[31m" | ||
#define TEN_LOG_COLOR_GREEN "\033[32m" | ||
#define TEN_LOG_COLOR_YELLOW "\033[33m" | ||
#define TEN_LOG_COLOR_BLUE "\033[34m" | ||
#define TEN_LOG_COLOR_MAGENTA "\033[35m" | ||
#define TEN_LOG_COLOR_CYAN "\033[36m" | ||
#define TEN_LOG_COLOR_WHITE "\033[37m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// Copyright © 2024 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. | ||
// | ||
#include "ten_utils/ten_config.h" | ||
|
||
#include <time.h> | ||
|
||
#include "include_internal/ten_utils/log/level.h" | ||
#include "include_internal/ten_utils/log/log.h" | ||
#include "include_internal/ten_utils/log/pid.h" | ||
#include "include_internal/ten_utils/log/termcolor.h" | ||
#include "include_internal/ten_utils/log/time.h" | ||
#include "ten_utils/lib/string.h" | ||
#include "ten_utils/log/log.h" | ||
|
||
void ten_log_set_formatter(ten_log_t *self, ten_log_formatter_func_t format_cb, | ||
void *user_data) { | ||
TEN_ASSERT(self, "Invalid argument."); | ||
|
||
self->formatter.format_cb = format_cb; | ||
self->formatter.user_data = user_data; | ||
} | ||
|
||
// Default formatter (no colors). | ||
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, | ||
size_t line_no, const char *msg, | ||
size_t msg_len) { | ||
struct tm time_info; | ||
size_t msec = 0; | ||
|
||
ten_log_get_time(&time_info, &msec); | ||
ten_log_add_time_string(buf, &time_info, msec); | ||
|
||
int64_t pid = 0; | ||
int64_t tid = 0; | ||
ten_log_get_pid_tid(&pid, &tid); | ||
|
||
ten_string_append_formatted(buf, " %d(%d) %c", pid, tid, | ||
ten_log_level_char(level)); | ||
|
||
if (func_name_len) { | ||
ten_string_append_formatted(buf, " %.*s", (int)func_name_len, func_name); | ||
} | ||
|
||
size_t actual_file_name_len = 0; | ||
const char *actual_file_name = | ||
filename(file_name, file_name_len, &actual_file_name_len); | ||
if (actual_file_name_len) { | ||
ten_string_append_formatted(buf, "@%.*s:%d", (int)actual_file_name_len, | ||
actual_file_name, (int)line_no); | ||
} | ||
|
||
ten_string_append_formatted(buf, " %.*s", (int)msg_len, msg); | ||
} | ||
|
||
// Colored formatter. | ||
void ten_log_colored_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, | ||
size_t line_no, const char *msg, | ||
size_t msg_len) { | ||
struct tm time_info; | ||
size_t msec = 0; | ||
ten_log_get_time(&time_info, &msec); | ||
ten_log_add_time_string(buf, &time_info, msec); | ||
|
||
int64_t pid = 0; | ||
int64_t tid = 0; | ||
ten_log_get_pid_tid(&pid, &tid); | ||
|
||
// Determine color based on log level. | ||
const char *level_color = NULL; | ||
switch (level) { | ||
case TEN_LOG_LEVEL_FATAL: | ||
case TEN_LOG_LEVEL_ERROR: | ||
level_color = TEN_LOG_COLOR_RED; | ||
break; | ||
case TEN_LOG_LEVEL_WARN: | ||
level_color = TEN_LOG_COLOR_YELLOW; | ||
break; | ||
case TEN_LOG_LEVEL_INFO: | ||
level_color = TEN_LOG_COLOR_GREEN; | ||
break; | ||
case TEN_LOG_LEVEL_DEBUG: | ||
case TEN_LOG_LEVEL_VERBOSE: | ||
level_color = TEN_LOG_COLOR_CYAN; | ||
break; | ||
default: | ||
level_color = TEN_LOG_COLOR_WHITE; | ||
break; | ||
} | ||
|
||
ten_string_append_formatted(buf, " %d(%d) %s%c%s", pid, tid, level_color, | ||
ten_log_level_char(level), TEN_LOG_COLOR_RESET); | ||
|
||
// Add color to function name. | ||
if (func_name_len) { | ||
ten_string_append_formatted(buf, " %s%.*s%s", TEN_LOG_COLOR_MAGENTA, | ||
(int)func_name_len, func_name, | ||
TEN_LOG_COLOR_RESET); | ||
} | ||
|
||
// Add color to file name and line number. | ||
size_t actual_file_name_len = 0; | ||
const char *actual_file_name = | ||
filename(file_name, file_name_len, &actual_file_name_len); | ||
if (actual_file_name_len) { | ||
ten_string_append_formatted(buf, "%s@%.*s:%d%s", TEN_LOG_COLOR_BLUE, | ||
(int)actual_file_name_len, actual_file_name, | ||
(int)line_no, TEN_LOG_COLOR_RESET); | ||
} | ||
|
||
// Add color to message. | ||
ten_string_append_formatted(buf, " %s%.*s%s", TEN_LOG_COLOR_WHITE, | ||
(int)msg_len, msg, TEN_LOG_COLOR_RESET); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters