/* * 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 */ #include #include #include #include #include #include #include /* initialize OK flag */ static bool init_ok = false; static FILE *fp = NULL; static int fd = -1; 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; elog_file_config(&cfg); init_ok = true; __exit: return result; } void elog_file_write(const char *log, size_t size) { ELOG_ASSERT(init_ok); ELOG_ASSERT(log); struct stat statbuf; statbuf.st_size = 0; fstat(fd, &statbuf); if (unlikely(statbuf.st_size > local_cfg.max_size)) return; elog_file_port_lock(); fwrite(log, size, 1, fp); #ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE fflush(fp); fsync(fd); #endif elog_file_port_unlock(); } void elog_file_deinit(void) { ELOG_ASSERT(init_ok); elog_file_port_deinit(); fclose(fp); } void elog_file_config(ElogFileCfg *cfg) { if (fp) { fclose(fp); } elog_file_port_lock(); local_cfg.name = cfg->name; local_cfg.max_size = cfg->max_size; fp = fopen(local_cfg.name, "a+"); if (fp) fd = fileno(fp); else fd = -1; elog_file_port_unlock(); }