/* * This file is part of the EasyLogger Library. * * Copyright (c) 2015-2019, Qintl, * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * 'Software'), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * Function: Save log to file. * Created on: 2019-01-05 * Radkil fixed in 20260313 */ #define LOG_TAG "elog.file" #include #include #include #include #include "elog_file.h" #include "rd_stdio.h" #ifdef ELOG_FILE_ENABLE /* initialize OK flag */ static bool init_ok = false; static void *fp = NULL; static ElogFileCfg local_cfg; ElogErrCode elog_file_init(void) { ElogErrCode result = ELOG_NO_ERR; ElogFileCfg cfg; if (init_ok) goto __exit; elog_file_port_init(); cfg.name = ELOG_FILE_NAME; cfg.max_size = ELOG_FILE_MAX_SIZE; cfg.max_rotate = ELOG_FILE_MAX_ROTATE; elog_file_config(&cfg); init_ok = true; __exit: return result; } /* * rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ static bool elog_file_rotate(void) { #define SUFFIX_LEN 10 /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ int n, err = 0; char oldpath[256]= {0}, newpath[256] = {0}; size_t base = RD_STRLEN(local_cfg.name); bool result = true; void *tmp_fp; RD_MEMCPY(oldpath, local_cfg.name, base); RD_MEMCPY(newpath, local_cfg.name, base); fclose(fp); for (n = local_cfg.max_rotate - 1; n >= 0; --n) { RD_SNPRINTF(oldpath + base, SUFFIX_LEN, n ? ".%d" : "", n - 1); if (n > 9999) n = 9999; RD_SNPRINTF(newpath + base, SUFFIX_LEN, ".%04d", n); /* remove the old file */ if ((tmp_fp = Rd_Fopen(newpath , "r")) != NULL) { Rd_Mclose(tmp_fp); Rd_remove(newpath); } /* change the new log file to old file name */ if ((tmp_fp = Rd_Fopen(oldpath , "r")) != NULL) { Rd_Mclose(tmp_fp); err = Rd_rename(oldpath, newpath); } if (err < 0) { result = false; goto __exit; } } __exit: /* reopen the file */ fp = Rd_Fopen(local_cfg.name, "a+"); return result; } void elog_file_write(const char *log, size_t size) { size_t file_size = 0; ELOG_ASSERT(init_ok); ELOG_ASSERT(log); if(fp == NULL) { return; } elog_file_port_lock(); Rd_Mseek(fp, 0L, SEEK_END); file_size = Rd_Mtell(fp); if (unlikely(file_size > local_cfg.max_size)) { #if ELOG_FILE_MAX_ROTATE > 0 if (!elog_file_rotate()) { goto __exit; } #else goto __exit; #endif } Rd_Mwrite(log, size, fp); #ifdef ELOG_FILE_FLUSH_CACHE_ENABLE Rd_fflush(fp); #endif __exit: elog_file_port_unlock(); } void elog_file_deinit(void) { ELOG_ASSERT(init_ok); ElogFileCfg cfg = {NULL, 0, 0}; elog_file_config(&cfg); elog_file_port_deinit(); init_ok = false; } void elog_file_config(ElogFileCfg *cfg) { elog_file_port_lock(); if (fp) { Rd_Mclose(fp); fp = NULL; } if (cfg != NULL) { local_cfg.name = cfg->name; local_cfg.max_size = cfg->max_size; local_cfg.max_rotate = cfg->max_rotate; if (local_cfg.name != NULL && RD_STRLEN(local_cfg.name) > 0) fp = Rd_Fopen(local_cfg.name, "a+"); } elog_file_port_unlock(); } #endif /* ELOG_FILE_ENABLE */