ulog_file_be.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <rtthread.h>
  11. #include <dfs_posix.h>
  12. #include <dfs.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. #if defined(ULOG_ASYNC_OUTPUT_THREAD_STACK) && (ULOG_ASYNC_OUTPUT_THREAD_STACK < 2048)
  30. #error "The value of ULOG_ASYNC_OUTPUT_THREAD_STACK must be greater than 2048."
  31. #endif
  32. static struct ulog_backend ulog_file;
  33. static char g_file_path[ULOG_FILE_PATH_LEN] = {0};
  34. static int g_file_fd = -1;
  35. /* rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
  36. static rt_bool_t ulog_file_rotate(void)
  37. {
  38. #define SUFFIX_LEN 10
  39. /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
  40. static char old_path[ULOG_FILE_PATH_LEN], new_path[ULOG_FILE_PATH_LEN];
  41. int index = 0, err = 0, file_fd = 0;
  42. size_t base_len = 0;
  43. rt_bool_t result = RT_FALSE;
  44. rt_memcpy(old_path, g_file_path, ULOG_FILE_PATH_LEN);
  45. rt_memcpy(new_path, g_file_path, ULOG_FILE_PATH_LEN);
  46. base_len = rt_strlen(ULOG_FILE_ROOT_PATH) + rt_strlen(ULOG_FILE_NAME_BASE) + 1;
  47. if (g_file_fd >= 0)
  48. {
  49. close(g_file_fd);
  50. }
  51. for (index = ULOG_FILE_MAX_NUM - 2; index >= 0; --index)
  52. {
  53. rt_snprintf(old_path + base_len, SUFFIX_LEN, index ? ".%d" : "", index - 1);
  54. rt_snprintf(new_path + base_len, SUFFIX_LEN, ".%d", index);
  55. /* remove the old file */
  56. if ((file_fd = open(new_path, O_RDONLY)) >= 0)
  57. {
  58. close(file_fd);
  59. unlink(new_path);
  60. }
  61. /* change the new log file to old file name */
  62. if ((file_fd = open(old_path , O_RDONLY)) >= 0)
  63. {
  64. close(file_fd);
  65. err = rename(old_path, new_path);
  66. }
  67. if (err < 0)
  68. {
  69. result = RT_FALSE;
  70. goto __exit;
  71. }
  72. }
  73. __exit:
  74. /* reopen the file */
  75. g_file_fd = open(g_file_path, O_CREAT | O_RDWR | O_APPEND);
  76. return result;
  77. }
  78. static void ulog_file_backend_output(struct ulog_backend *backend, rt_uint32_t level,
  79. const char *tag, rt_bool_t is_raw, const char *log, size_t len)
  80. {
  81. size_t file_size = 0;
  82. /* check log file directory */
  83. if (access(ULOG_FILE_ROOT_PATH, 0) < 0)
  84. {
  85. mkdir(ULOG_FILE_ROOT_PATH, 0);
  86. }
  87. if (g_file_fd < 0)
  88. {
  89. rt_snprintf(g_file_path, ULOG_FILE_PATH_LEN, "%s/%s", ULOG_FILE_ROOT_PATH, ULOG_FILE_NAME_BASE);
  90. g_file_fd = open(g_file_path, O_CREAT | O_RDWR);
  91. if (g_file_fd < 0)
  92. {
  93. rt_kprintf("ulog file(%s) open failed.", g_file_path);
  94. return;
  95. }
  96. }
  97. file_size = lseek(g_file_fd, 0, SEEK_END);
  98. if (file_size > ULOG_FILE_MAX_SIZE)
  99. {
  100. if (!ulog_file_rotate())
  101. {
  102. return;
  103. }
  104. }
  105. write(g_file_fd, log, len);
  106. /* flush file cache */
  107. fsync(g_file_fd);
  108. }
  109. /* initialize the ulog file backend */
  110. int ulog_file_backend_init(void)
  111. {
  112. ulog_file.output = ulog_file_backend_output;
  113. ulog_backend_register(&ulog_file, ULOG_FILE_BE_NAME, RT_FALSE);
  114. return 0;
  115. }
  116. /* uninitialize the ulog file backend */
  117. int ulog_file_backend_deinit(void)
  118. {
  119. if (g_file_fd >= 0)
  120. {
  121. close(g_file_fd);
  122. g_file_fd = -1;
  123. }
  124. ulog_backend_unregister(&ulog_file);
  125. return 0;
  126. }