elog_file.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This file is part of the EasyLogger Library.
  3. *
  4. * Copyright (c) 2015-2019, Qintl, <qintl_linux@163.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: Save log to file.
  26. * Created on: 2019-01-05
  27. */
  28. #define LOG_TAG "elog.file"
  29. #include <stdio.h>
  30. #include <stdbool.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "elog_file.h"
  34. #ifdef ELOG_FILE_ENABLE
  35. /* initialize OK flag */
  36. static bool init_ok = false;
  37. static FILE *fp = NULL;
  38. static ElogFileCfg local_cfg;
  39. int elog_file_init(void)
  40. {
  41. int result = 0;
  42. ElogFileCfg cfg;
  43. if (init_ok)
  44. goto __exit;
  45. elog_file_port_init();
  46. cfg.name = ELOG_FILE_NAME;
  47. cfg.max_size = ELOG_FILE_MAX_SIZE;
  48. cfg.max_rotate = ELOG_FILE_MAX_ROTATE;
  49. elog_file_config(&cfg);
  50. init_ok = true;
  51. __exit:
  52. return result;
  53. }
  54. /*
  55. * rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0
  56. */
  57. static bool elog_file_rotate(void)
  58. {
  59. #define SUFFIX_LEN 16
  60. /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
  61. int n, err = 0;
  62. char oldpath[256]= {0}, newpath[256] = {0};
  63. size_t base = strlen(local_cfg.name);
  64. bool result = true;
  65. FILE *tmp_fp;
  66. memcpy(oldpath, local_cfg.name, base);
  67. memcpy(newpath, local_cfg.name, base);
  68. fclose(fp);
  69. for (n = local_cfg.max_rotate - 1; n >= 0; --n) {
  70. snprintf(oldpath + base, SUFFIX_LEN, n ? ".%d" : "", n - 1);
  71. snprintf(newpath + base, SUFFIX_LEN, ".%d", n);
  72. /* remove the old file */
  73. if ((tmp_fp = fopen(newpath , "r")) != NULL) {
  74. fclose(tmp_fp);
  75. remove(newpath);
  76. }
  77. /* change the new log file to old file name */
  78. if ((tmp_fp = fopen(oldpath , "r")) != NULL) {
  79. fclose(tmp_fp);
  80. err = rename(oldpath, newpath);
  81. }
  82. if (err < 0) {
  83. result = false;
  84. goto __exit;
  85. }
  86. }
  87. __exit:
  88. /* reopen the file */
  89. fp = fopen(local_cfg.name, "a+");
  90. return result;
  91. }
  92. void elog_file_write(const char *log, size_t size)
  93. {
  94. size_t file_size = 0;
  95. assert(init_ok);
  96. assert(log);
  97. if(fp == NULL) {
  98. return;
  99. }
  100. elog_file_port_lock();
  101. fseek(fp, 0L, SEEK_END);
  102. file_size = ftell(fp);
  103. if (unlikely(file_size > local_cfg.max_size)) {
  104. #if ELOG_FILE_MAX_ROTATE > 0
  105. if (!elog_file_rotate()) {
  106. goto __exit;
  107. }
  108. #else
  109. goto __exit;
  110. #endif
  111. }
  112. fwrite(log, size, 1, fp);
  113. #ifdef ELOG_FILE_FLUSH_CACHE_ENABLE
  114. fflush(fp);
  115. #endif
  116. __exit:
  117. elog_file_port_unlock();
  118. }
  119. void elog_file_deinit(void)
  120. {
  121. assert(init_ok);
  122. ElogFileCfg cfg = {NULL, 0, 0};
  123. elog_file_config(&cfg);
  124. elog_file_port_deinit();
  125. init_ok = false;
  126. }
  127. void elog_file_config(ElogFileCfg *cfg)
  128. {
  129. elog_file_port_lock();
  130. if (fp) {
  131. fclose(fp);
  132. fp = NULL;
  133. }
  134. if (cfg != NULL) {
  135. local_cfg.name = cfg->name;
  136. local_cfg.max_size = cfg->max_size;
  137. local_cfg.max_rotate = cfg->max_rotate;
  138. if (local_cfg.name != NULL && strlen(local_cfg.name) > 0)
  139. fp = fopen(local_cfg.name, "a+");
  140. }
  141. elog_file_port_unlock();
  142. }
  143. #endif /* ELOG_FILE_ENABLE */