utils 按作用拆成 log/macros/memory 三个模块

This commit is contained in:
2026-03-23 19:05:39 +08:00
parent fa93b3d733
commit 67f0c169f5
15 changed files with 179 additions and 175 deletions

43
src/base/log.c Normal file
View File

@@ -0,0 +1,43 @@
#include "base/log.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "app.h"
const char *current_time_str(void) {
static char buffer[25];
time_t now;
struct tm tm;
time(&now);
localtime_r(&now, &tm);
if (!strftime(buffer, sizeof(buffer), "%Y-%m-%d %T", &tm)) buffer[0] = '\0';
return buffer;
}
void _fatal(int line, const char *fct, const char *file, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
const char *format = "(EE) [%s] %s\n%s:%d:%s:\n";
fprintf(stderr, format, APP_NAME, current_time_str(), file, line, fct);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
void _warn(int line, const char *fct, const char *file, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
const char *format = "(WW) [%s] %s\n%s:%d:%s:\n";
fprintf(stderr, format, APP_NAME, current_time_str(), file, line, fct);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}