代码拉取完成,页面将自动刷新
// log.c
#include "log.h"
void log_message(LogLevel level, const char *format, ...) {
int log_fd = open("server.log", O_RDWR | O_CREAT | O_APPEND, 0666);
if (log_fd == -1) {
perror("open server.log");
return;
}
char log_buffer[2048];
time_t now;
struct tm *tm_info;
time(&now);
tm_info = localtime(&now);
strftime(log_buffer, sizeof(log_buffer), "[%Y-%m-%d %H:%M:%s]", tm_info);
const char *level_str;
switch (level) {
case LOG_LEVEL_INFO:
level_str = "INFO";
break;
case LOG_LEVEL_WARNING:
level_str = "WARNING";
break;
case LOG_LEVEL_ERROR:
level_str = "ERROR";
break;
default:
level_str = "UNKNOWN";
break;
}
strcat(log_buffer, " ");
strcat(log_buffer, level_str);
strcat(log_buffer, ": ");
va_list args;
va_start(args, format);
vsnprintf(log_buffer + strlen(log_buffer), sizeof(log_buffer) - strlen(log_buffer), format, args);
va_end(args);
strcat(log_buffer, "\n");
if (write(log_fd, log_buffer, strlen(log_buffer)) == -1) {
perror("write log file failed");
}
close(log_fd);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。