ulog_file_be.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-01-07 ChenYong first version
  9. */
  10. #include <FreeRTOS.h>
  11. #include <ff_stdio.h>
  12. #include "board.h"
  13. #include "ulog.h"
  14. #include "ulog_file.h"
  15. #define ULOG_FILE_BE_NAME "file"
  16. #ifndef ULOG_FILE_ROOT_PATH
  17. #define ULOG_FILE_ROOT_PATH "/logs"
  18. #endif
  19. #ifndef ULOG_FILE_NAME_BASE
  20. #define ULOG_FILE_NAME_BASE "ulog.log"
  21. #endif
  22. #ifndef ULOG_FILE_MAX_NUM
  23. #define ULOG_FILE_MAX_NUM 5
  24. #endif
  25. #ifndef ULOG_FILE_MAX_SIZE
  26. #define ULOG_FILE_MAX_SIZE (1024 * 512)
  27. #endif
  28. #define ULOG_FILE_PATH_LEN 128
  29. #ifdef ULOG_FILE_BACKEND_ENABLE
  30. #if defined(ULOG_ASYNC_OUTPUT_THREAD_STACK) && (ULOG_ASYNC_OUTPUT_THREAD_STACK < 2048)
  31. #error "The value of ULOG_ASYNC_OUTPUT_THREAD_STACK must be greater than 2048."
  32. #endif
  33. static struct ulog_backend ulog_file;
  34. static char g_file_path[ULOG_FILE_PATH_LEN] = {0};
  35. static FF_FILE *g_file_fd = NULL;
  36. /* rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
  37. static int ulog_file_rotate(void)
  38. {
  39. #define SUFFIX_LEN 10
  40. /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
  41. static char old_path[ULOG_FILE_PATH_LEN], new_path[ULOG_FILE_PATH_LEN];
  42. int index = 0, err = 0;
  43. FF_FILE *file_fd = NULL;
  44. size_t base_len = 0;
  45. int result = pdTRUE;
  46. memcpy(old_path, g_file_path, ULOG_FILE_PATH_LEN);
  47. memcpy(new_path, g_file_path, ULOG_FILE_PATH_LEN);
  48. base_len = strlen(ULOG_FILE_ROOT_PATH) + strlen(ULOG_FILE_NAME_BASE) + 1;
  49. if (g_file_fd)
  50. {
  51. ff_fclose(g_file_fd);
  52. }
  53. for (index = ULOG_FILE_MAX_NUM - 2; index >= 0; --index)
  54. {
  55. snprintf(old_path + base_len, SUFFIX_LEN, index ? ".%d" : "", index - 1);
  56. snprintf(new_path + base_len, SUFFIX_LEN, ".%d", index);
  57. /* remove the old file */
  58. if ((file_fd = ff_fopen(new_path, "rb")) != NULL)
  59. {
  60. ff_fclose(file_fd);
  61. ff_remove(new_path);
  62. }
  63. /* change the new log file to old file name */
  64. if ((file_fd = ff_fopen(old_path , "rb")) != NULL)
  65. {
  66. ff_fclose(file_fd);
  67. err = ff_rename(old_path, new_path, 1);
  68. }
  69. if (err < 0)
  70. {
  71. result = pdFALSE;
  72. goto __exit;
  73. }
  74. }
  75. __exit:
  76. /* reopen the file */
  77. g_file_fd = ff_fopen(g_file_path, "a+");
  78. return result;
  79. }
  80. static void ulog_file_backend_output(struct ulog_backend *backend, uint32_t level,
  81. const char *tag, int is_raw, const char *log, size_t len)
  82. {
  83. size_t file_size = 0;
  84. /* check log file directory */
  85. if (ff_finddir(ULOG_FILE_ROOT_PATH) == pdFALSE)
  86. {
  87. ff_mkdir(ULOG_FILE_ROOT_PATH);
  88. }
  89. if (g_file_fd == NULL)
  90. {
  91. snprintf(g_file_path, ULOG_FILE_PATH_LEN, "%s/%s", ULOG_FILE_ROOT_PATH, ULOG_FILE_NAME_BASE);
  92. g_file_fd = ff_fopen(g_file_path, "a+");
  93. if (g_file_fd == NULL)
  94. {
  95. printf("ulog file(%s) open failed.", g_file_path);
  96. return;
  97. }
  98. }
  99. ff_fseek(g_file_fd, 0, FF_SEEK_END);
  100. file_size = ff_filelength(g_file_fd);
  101. if (file_size > ULOG_FILE_MAX_SIZE)
  102. {
  103. if (!ulog_file_rotate())
  104. {
  105. return;
  106. }
  107. }
  108. ff_fwrite(log, 1, len, g_file_fd);
  109. /* flush file cache */
  110. ff_fclose(g_file_fd);
  111. g_file_fd = ff_fopen(g_file_path, "a+");
  112. }
  113. /* initialize the ulog file backend */
  114. int ulog_file_backend_init(void)
  115. {
  116. ulog_file.output = ulog_file_backend_output;
  117. ulog_backend_register(&ulog_file, ULOG_FILE_BE_NAME, pdFALSE);
  118. return 0;
  119. }
  120. /* uninitialize the ulog file backend */
  121. int ulog_file_backend_deinit(void)
  122. {
  123. if (g_file_fd)
  124. {
  125. ff_fclose(g_file_fd);
  126. g_file_fd = NULL;
  127. }
  128. ulog_backend_unregister(&ulog_file);
  129. return 0;
  130. }
  131. #endif /* ULOG_FILE_BACKEND_ENABLE */