加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log.c 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
gzh 提交于 2024-10-23 21:35 . ls命令
// 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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化