协能can协议
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

elog_file.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* initialize OK flag */
  35. static bool init_ok = false;
  36. static FILE *fp = NULL;
  37. static ElogFileCfg local_cfg;
  38. ElogErrCode elog_file_init(void)
  39. {
  40. ElogErrCode result = ELOG_NO_ERR;
  41. ElogFileCfg cfg;
  42. if (init_ok)
  43. goto __exit;
  44. elog_file_port_init();
  45. cfg.name = ELOG_FILE_NAME;
  46. cfg.max_size = ELOG_FILE_MAX_SIZE;
  47. cfg.max_rotate = ELOG_FILE_MAX_ROTATE;
  48. elog_file_config(&cfg);
  49. init_ok = true;
  50. __exit:
  51. return result;
  52. }
  53. /*
  54. * rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0
  55. */
  56. static bool elog_file_rotate(void)
  57. {
  58. #define SUFFIX_LEN 10
  59. /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
  60. int n, err = 0;
  61. char oldpath[256], newpath[256];
  62. size_t base = strlen(local_cfg.name);
  63. bool result = true;
  64. FILE *tmp_fp;
  65. memcpy(oldpath, local_cfg.name, base);
  66. memcpy(newpath, local_cfg.name, base);
  67. fclose(fp);
  68. for (n = local_cfg.max_rotate - 1; n >= 0; --n) {
  69. snprintf(oldpath + base, SUFFIX_LEN, n ? ".%d" : "", n - 1);
  70. snprintf(newpath + base, SUFFIX_LEN, ".%d", n);
  71. /* remove the old file */
  72. if ((tmp_fp = fopen(newpath , "r")) != NULL) {
  73. fclose(tmp_fp);
  74. remove(newpath);
  75. }
  76. /* change the new log file to old file name */
  77. if ((tmp_fp = fopen(oldpath , "r")) != NULL) {
  78. fclose(tmp_fp);
  79. err = rename(oldpath, newpath);
  80. }
  81. if (err < 0) {
  82. result = false;
  83. goto __exit;
  84. }
  85. }
  86. __exit:
  87. /* reopen the file */
  88. fp = fopen(local_cfg.name, "a+");
  89. return result;
  90. }
  91. void elog_file_write(const char *log, size_t size)
  92. {
  93. size_t file_size = 0;
  94. ELOG_ASSERT(init_ok);
  95. ELOG_ASSERT(log);
  96. elog_file_port_lock();
  97. fseek(fp, 0L, SEEK_END);
  98. file_size = ftell(fp);
  99. if (unlikely(file_size > local_cfg.max_size)) {
  100. #if ELOG_FILE_MAX_ROTATE > 0
  101. if (!elog_file_rotate()) {
  102. goto __exit;
  103. }
  104. #else
  105. goto __exit;
  106. #endif
  107. }
  108. fwrite(log, size, 1, fp);
  109. #ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE
  110. fflush(fp);
  111. #endif
  112. __exit:
  113. elog_file_port_unlock();
  114. }
  115. void elog_file_deinit(void)
  116. {
  117. ELOG_ASSERT(init_ok);
  118. elog_file_port_deinit();
  119. fclose(fp);
  120. }
  121. void elog_file_config(ElogFileCfg *cfg)
  122. {
  123. if (fp) {
  124. fclose(fp);
  125. }
  126. elog_file_port_lock();
  127. local_cfg.name = cfg->name;
  128. local_cfg.max_size = cfg->max_size;
  129. local_cfg.max_rotate = cfg->max_rotate;
  130. fp = fopen(local_cfg.name, "a+");
  131. elog_file_port_unlock();
  132. }