refactor: shorten log levels and move source locations to the end (#2104)

This commit is contained in:
leejet
2026-10-06 18:26:49 +08:00
committed by GitHub
parent 019bcfc0ae
commit 203a882a28
2 changed files with 27 additions and 25 deletions
+18 -21
View File
@@ -91,34 +91,34 @@ void log_print(enum sd_log_level_t level, const char* log, sd_log_level_t min_le
switch (level) {
case SD_LOG_DEBUG:
tag_color = 37;
level_str = "DEBUG";
level_str = "D";
break;
case SD_LOG_VERBOSE:
tag_color = 37;
level_str = "VERBOSE";
level_str = "V";
break;
case SD_LOG_INFO:
tag_color = 34;
level_str = "INFO";
level_str = "I";
break;
case SD_LOG_WARN:
tag_color = 35;
level_str = "WARN";
level_str = "W";
break;
case SD_LOG_ERROR:
tag_color = 31;
level_str = "ERROR";
level_str = "E";
break;
default:
tag_color = 33;
level_str = "?????";
level_str = "?";
break;
}
if (color) {
fprintf(out_stream, "\033[%d;1m[%-7s]\033[0m ", tag_color, level_str);
fprintf(out_stream, "\033[%d;1m[%s]\033[0m ", tag_color, level_str);
} else {
fprintf(out_stream, "[%-7s] ", level_str);
fprintf(out_stream, "[%s] ", level_str);
}
fflush(out_stream);
print_utf8(out_stream, log);
@@ -131,18 +131,15 @@ void example_log_printf(sd_log_level_t level, const char* file, int line, const
va_list args;
va_start(args, format);
static char log_buffer[LOG_BUFFER_SIZE + 1];
int written = snprintf(log_buffer, LOG_BUFFER_SIZE, "%s:%-4d - ", sd_basename(file).c_str(), line);
if (written >= 0 && written < static_cast<int>(LOG_BUFFER_SIZE)) {
vsnprintf(log_buffer + written, LOG_BUFFER_SIZE - written, format, args);
}
size_t len = strlen(log_buffer);
if (len == 0 || log_buffer[len - 1] != '\n') {
strncat(log_buffer, "\n", LOG_BUFFER_SIZE - len);
}
log_print(level, log_buffer, log_level, log_color);
char log_buffer[LOG_BUFFER_SIZE + 1] = {};
vsnprintf(log_buffer, sizeof(log_buffer), format, args);
va_end(args);
std::string message = log_buffer;
while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) {
message.pop_back();
}
message += " - " + sd_basename(file) + ":" + std::to_string(line) + '\n';
log_print(level, message.c_str(), log_level, log_color);
}
+9 -4
View File
@@ -644,9 +644,14 @@ void* sd_log_cb_data = nullptr;
static void sd_log_dispatch(sd_log_level_t level, const std::string& origin, const std::string& text) {
if (sd_log_cb == nullptr)
return;
std::string message = origin + " - " + text;
if (message.back() != '\n') {
message += '\n';
std::string message = text;
while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) {
message.pop_back();
}
if (origin == "ggml") {
message = origin + ": " + message + '\n';
} else {
message += " - " + origin + '\n';
}
sd_log_cb(level, message.c_str(), sd_log_cb_data);
}
@@ -656,7 +661,7 @@ void log_printf(sd_log_level_t level, const char* file, int line, const char* fo
va_start(args, format);
std::string message = sd_vformat(format, args);
va_end(args);
std::string origin = sd_format("%s:%-4d", sd_basename(file).c_str(), line);
std::string origin = sd_format("%s:%d", sd_basename(file).c_str(), line);
sd_log_dispatch(level, origin, message);
}