flog.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "flog.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <time.h>
  7. #include <sys/stat.h>
  8. #include <pthread.h>
  9. #include <unistd.h>
  10. #define FLOG_MAX_CATS 32
  11. static char g_path[512];
  12. static long g_rotate_ms = 3600000;
  13. static long g_max_bytes = 1 << 20; /* 1 MiB */
  14. static FILE *g_fp = NULL;
  15. static long long g_last_rotate_ms = 0;
  16. static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
  17. typedef struct {
  18. char cat[32];
  19. long long last_ms;
  20. } FlogThrottle;
  21. static FlogThrottle g_throttle[FLOG_MAX_CATS];
  22. static int g_num_throttle = 0;
  23. static long long now_ms(void) {
  24. struct timespec ts;
  25. clock_gettime(CLOCK_MONOTONIC, &ts);
  26. return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  27. }
  28. int flog_init(const char *path, long rotate_ms, long max_bytes) {
  29. pthread_mutex_lock(&g_mutex);
  30. if (g_fp) { fclose(g_fp); g_fp = NULL; }
  31. g_path[0] = '\0';
  32. if (path && path[0]) {
  33. strncpy(g_path, path, sizeof(g_path) - 1);
  34. g_fp = fopen(path, "a");
  35. if (!g_fp) {
  36. pthread_mutex_unlock(&g_mutex);
  37. fprintf(stderr, "flog: cannot open %s\n", path);
  38. return -1;
  39. }
  40. }
  41. if (rotate_ms > 0) g_rotate_ms = rotate_ms;
  42. if (max_bytes > 0) g_max_bytes = max_bytes;
  43. g_last_rotate_ms = now_ms();
  44. pthread_mutex_unlock(&g_mutex);
  45. return 0;
  46. }
  47. void flog_shutdown(void) {
  48. pthread_mutex_lock(&g_mutex);
  49. if (g_fp) { fclose(g_fp); g_fp = NULL; }
  50. pthread_mutex_unlock(&g_mutex);
  51. }
  52. int flog_enabled(void) {
  53. return g_fp != NULL;
  54. }
  55. void flog(const char *fmt, ...) {
  56. if (!g_fp) return;
  57. char msg[1024];
  58. va_list ap;
  59. va_start(ap, fmt);
  60. vsnprintf(msg, sizeof(msg), fmt, ap);
  61. va_end(ap);
  62. time_t t = time(NULL);
  63. struct tm tm;
  64. localtime_r(&t, &tm);
  65. pthread_mutex_lock(&g_mutex);
  66. if (!g_fp) { pthread_mutex_unlock(&g_mutex); return; }
  67. fprintf(g_fp, "[%04d-%02d-%02d %02d:%02d:%02d] %s\n",
  68. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  69. tm.tm_hour, tm.tm_min, tm.tm_sec, msg);
  70. fflush(g_fp);
  71. pthread_mutex_unlock(&g_mutex);
  72. }
  73. void flog_throttled(const char *cat, long min_interval_ms, const char *fmt, ...) {
  74. if (!g_fp || !cat) return;
  75. long long now = now_ms();
  76. int i = 0;
  77. pthread_mutex_lock(&g_mutex);
  78. for (i = 0; i < g_num_throttle; i++)
  79. if (strcmp(g_throttle[i].cat, cat) == 0) break;
  80. if (i >= FLOG_MAX_CATS) {
  81. pthread_mutex_unlock(&g_mutex);
  82. return;
  83. }
  84. if (i == g_num_throttle) {
  85. strncpy(g_throttle[i].cat, cat, sizeof(g_throttle[i].cat) - 1);
  86. g_throttle[i].cat[sizeof(g_throttle[i].cat) - 1] = '\0';
  87. g_throttle[i].last_ms = 0;
  88. g_num_throttle++;
  89. }
  90. if (now - g_throttle[i].last_ms < min_interval_ms) {
  91. pthread_mutex_unlock(&g_mutex);
  92. return;
  93. }
  94. g_throttle[i].last_ms = now;
  95. pthread_mutex_unlock(&g_mutex);
  96. char msg[1024];
  97. va_list ap;
  98. va_start(ap, fmt);
  99. vsnprintf(msg, sizeof(msg), fmt, ap);
  100. va_end(ap);
  101. time_t t = time(NULL);
  102. struct tm tm;
  103. localtime_r(&t, &tm);
  104. pthread_mutex_lock(&g_mutex);
  105. if (!g_fp) { pthread_mutex_unlock(&g_mutex); return; }
  106. fprintf(g_fp, "[%04d-%02d-%02d %02d:%02d:%02d] %s\n",
  107. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  108. tm.tm_hour, tm.tm_min, tm.tm_sec, msg);
  109. fflush(g_fp);
  110. pthread_mutex_unlock(&g_mutex);
  111. }
  112. void flog_rotate_check(void) {
  113. pthread_mutex_lock(&g_mutex);
  114. if (!g_fp) { pthread_mutex_unlock(&g_mutex); return; }
  115. long long now = now_ms();
  116. int do_trunc = 0;
  117. if (g_rotate_ms > 0 && now - g_last_rotate_ms >= g_rotate_ms) {
  118. do_trunc = 1;
  119. g_last_rotate_ms = now;
  120. }
  121. if (g_max_bytes > 0) {
  122. struct stat st;
  123. if (fstat(fileno(g_fp), &st) == 0 && st.st_size >= g_max_bytes)
  124. do_trunc = 1;
  125. }
  126. if (do_trunc) {
  127. fclose(g_fp);
  128. g_fp = fopen(g_path, "w");
  129. if (!g_fp) {
  130. pthread_mutex_unlock(&g_mutex);
  131. fprintf(stderr, "flog: cannot reopen %s\n", g_path);
  132. return;
  133. }
  134. fprintf(g_fp, "--- log cleared at %lld ms ---\n", now);
  135. fflush(g_fp);
  136. }
  137. pthread_mutex_unlock(&g_mutex);
  138. }