修改自定义 _printf()

This commit is contained in:
2024-10-30 10:59:48 +08:00
parent 62417c9201
commit 67a36b150d
6 changed files with 28 additions and 27 deletions

View File

@@ -22,9 +22,17 @@ void _printf(const char *format, ...)
va_list args;
va_start(args, format);
// 打印到控制台
vprintf(format, args);
va_end(args); // 结束对变参列表的处理
// 获取当前时间
time_t now = time(NULL);
struct tm local_time;
localtime_r(&now, &local_time);
char time_str[20]; // YYYY-MM-DD HH:MM:SS 格式
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &local_time);
// 打印时间戳到控制台
printf("[%s] ", time_str);
vprintf(format, args); // 打印内容到控制台
va_end(args); // 结束对变参列表的处理
// 重新启动变参列表
va_start(args, format);
@@ -32,13 +40,6 @@ void _printf(const char *format, ...)
// 打开日志文件(追加模式)
FILE *log_file = fopen(PRINT_LOG_FILE, "a");
if (log_file != NULL) {
// 获取当前时间
time_t now = time(NULL);
struct tm local_time;
localtime_r(&now, &local_time);
char time_str[20]; // YYYY-MM-DD HH:MM:SS 格式
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &local_time);
// 打印时间戳到日志文件
fprintf(log_file, "[%s] ", time_str);
@@ -51,10 +52,10 @@ void _printf(const char *format, ...)
perror("Unable to open log file");
}
va_end(args); // 结束对变参列表的处理
va_end(args); // 结束对变参列表的处理
}
void split_string(char string[], char delims[], char (*whitelist_ip)[WHITELIST_IP_NUM])
{
int i = 0;