Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

日志输出到文件时,可以考虑两点建议 #112

Open
tangkai-prc opened this issue Apr 1, 2022 · 2 comments
Open

日志输出到文件时,可以考虑两点建议 #112

tangkai-prc opened this issue Apr 1, 2022 · 2 comments

Comments

@tangkai-prc
Copy link

日志输出到文件时,建议如下
1 提供用户接口,指定输出文件路径+文件名,如果为NULL,则按照默认文件名保存
2 如果用户配置,可以按照天保存到文件中,每天保存为一个文件,文件名可以是log_YYYY_MM_DD.log的形式

@armink
Copy link
Owner

armink commented Apr 1, 2022

挺好的建议哈,你可以试着加一下?

@tangkai-prc
Copy link
Author

tangkai-prc commented Apr 1, 2022

我自己这边改了一版
更改结构体

typedef struct {
    char path[128];          /* add log path*/
    char name[256];        /* full file name */
    size_t max_size;         /* file max size */
    int max_rotate;          /* max rotate file count */
} ElogFileCfg; 

新增 函数
void elog_setFilePath(const char *path, size_t len);

#ifndef DEFALUT_LOG_FILE_PATH
#define DEFALUT_LOG_FILE_PATH "./logs"
#endif
void elog_setFilePath(const char* filepath, size_t len)
{
    if (!filepath) sprintf(local_cfg.path, DEFALUT_LOG_FILE_PATH);
    if (len < sizeof(local_cfg.path)/sizeof(local_cfg.path[0]))
    {
        strncpy(local_cfg.path, filepath, 127);
    }
}

linux 环境下,修改该函数

ElogErrCode elog_file_init(void)
{
    ElogErrCode result = ELOG_NO_ERR;
    ElogFileCfg cfg;

    if (init_ok)
        goto __exit;

        elog_file_port_init();
        if (strlen(local_cfg.path) == 0) 
        {
            sprintf(local_cfg.path, DEFALUT_LOG_FILE_PATH);
        }

        char filename[256] = {0};
	struct tm tm = { 0 };
	struct timeval tv = { 0 };
	gettimeofday(&tv, NULL);
	localtime_r(&tv.tv_sec, &tm);
	if(access(local_cfg.path, 0) == -1)
        {
            mkdir(local_cfg.path, 0775);
        }
	snprintf(filename, 255, "%s/log_%4d-%02d-%02d.log", local_cfg.path, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);

    strncpy(cfg.name, filename, 255);
    cfg.max_size = ELOG_FILE_MAX_SIZE;
    cfg.max_rotate = ELOG_FILE_MAX_ROTATE;

    elog_file_config(&cfg);

    init_ok = true;
__exit:
    return result;
}

2 文件生成时间判断

/* add static fun compare Data*/
static 
void filecompData()
{
    char filename[256] = {0};
    struct tm tm = { 0 };
    struct timeval tv = { 0 };
    gettimeofday(&tv, NULL);
    localtime_r(&tv.tv_sec, &tm);
    snprintf(filename, 255, "%s/log_%4d-%02d-%02d.log",
    local_cfg.path, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
    if (strcmp(filename, local_cfg.name) == 0)
    {
        return;
    }

    fclose(fp);
    strncpy(local_cfg.name, filename, strlen(filename));
    fp = fopen(filename, "a+");
    return;
}

void elog_file_write(const char *log, size_t size)
{
    size_t file_size = 0;

    ELOG_ASSERT(init_ok);
    ELOG_ASSERT(log);

    elog_file_port_lock();

    fseek(fp, 0L, SEEK_END);
    file_size = ftell(fp);
    
    /*重新写入新的日志时间*/
    filecompData();

    if (unlikely(file_size > local_cfg.max_size)) {
#if ELOG_FILE_MAX_ROTATE > 0
        if (!elog_file_rotate()) {
            goto __exit;
        }
#else
        goto __exit;
#endif
    }

    fwrite(log, size, 1, fp);

#ifdef ELOG_FILE_FLUSH_CACHE_ENABLE
    fflush(fp);
#endif

__exit:
    elog_file_port_unlock();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants