Optimize the output of log and stat

This commit is contained in:
luwenpeng
2024-05-16 11:52:14 +08:00
parent 4c50a6dca7
commit 8d8a266f60
9 changed files with 120 additions and 51 deletions

View File

@@ -21,7 +21,8 @@ struct log_config
{
enum log_output output;
enum log_level level;
char log_file[256];
char work_dir[1024];
char log_file[1024];
};
struct log_context
@@ -194,9 +195,11 @@ static int log_reopen()
int new_fd;
int old_fd;
struct tm local;
char buff[512] = {0};
char buff[4096] = {0};
local_time(&local);
snprintf(buff, sizeof(buff), "%s.%d-%02d-%02d", g_log_ctx->config.log_file, local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
snprintf(buff, sizeof(buff), "%s/%s.%d-%02d-%02d",
g_log_ctx->config.work_dir, g_log_ctx->config.log_file,
local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
new_fd = open(buff, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (new_fd == -1)
@@ -227,6 +230,12 @@ int log_init(const char *config_file)
{
memset(g_log_ctx, 0, sizeof(struct log_context));
if (getcwd(g_log_ctx->config.work_dir, sizeof(g_log_ctx->config.work_dir)) == NULL)
{
fprintf(stderr, "getcwd() failed, %s\n", strerror(errno));
return -1;
}
if (parse_config(&g_log_ctx->config, config_file) != 0)
{
return -1;
@@ -255,6 +264,11 @@ void log_free()
}
}
int log_level_enabled(enum log_level level)
{
return level >= g_log_ctx->config.level;
}
void log_reload_level(const char *config_file)
{
struct log_config config;
@@ -267,11 +281,6 @@ void log_reload_level(const char *config_file)
void log_print(enum log_level level, const char *module, const char *fmt, ...)
{
if (level < g_log_ctx->config.level)
{
return;
}
int nwrite;
char buf[4096] = {0};
char *p = buf;