hlog.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "hlog.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include "htime.h" // for get_datetime
  6. #define SECONDS_PER_DAY 86400
  7. static char s_logfile[256] = DEFAULT_LOG_FILE;
  8. static int s_loglevel = DEFAULT_LOG_LEVEL;
  9. static bool s_logcolor = false;
  10. static bool s_fflush = true;
  11. static int s_remain_days = DEFAULT_LOG_REMAIN_DAYS;
  12. static char s_logbuf[LOG_BUFSIZE];
  13. // for thread-safe
  14. #include "hmutex.h"
  15. #ifdef _MSC_VER
  16. static hmutex_t s_mutex;
  17. static honce_t s_once = HONCE_INIT;
  18. static void WINAPI __mutex_init() {
  19. hmutex_init(&s_mutex);
  20. }
  21. #define HLOG_LOCK\
  22. honce(&s_once, __mutex_init);\
  23. hmutex_lock(&s_mutex);
  24. #else
  25. static hmutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
  26. #define HLOG_LOCK hmutex_lock(&s_mutex);
  27. #endif
  28. #define HLOG_UNLOCK hmutex_unlock(&s_mutex);
  29. static void ts_logfile(time_t ts, char* buf, int len) {
  30. struct tm* tm = localtime(&ts);
  31. snprintf(buf, len, "%s-%04d-%02d-%02d.log",
  32. s_logfile,
  33. tm->tm_year+1900,
  34. tm->tm_mon+1,
  35. tm->tm_mday);
  36. }
  37. static FILE* shift_logfile() {
  38. static FILE* s_logfp = NULL;
  39. static char s_cur_logfile[256] = {0};
  40. static time_t s_last_logfile_ts = 0;
  41. time_t ts_now = time(NULL);
  42. int interval_days = s_last_logfile_ts == 0 ? 0 : (ts_now / SECONDS_PER_DAY - s_last_logfile_ts / SECONDS_PER_DAY);;
  43. if (s_logfp == NULL || interval_days > 0) {
  44. // close old logfile
  45. if (s_logfp) {
  46. fclose(s_logfp);
  47. s_logfp = NULL;
  48. }
  49. else {
  50. interval_days = 30;
  51. }
  52. if (interval_days >= s_remain_days) {
  53. // remove [today-interval_days, today-s_remain_days] logfile
  54. char rm_logfile[256] = {0};
  55. for (int i = interval_days; i >= s_remain_days; --i) {
  56. time_t ts_rm = ts_now - i * SECONDS_PER_DAY;
  57. ts_logfile(ts_rm, rm_logfile, sizeof(rm_logfile));
  58. remove(rm_logfile);
  59. }
  60. }
  61. else {
  62. // remove today-s_remain_days logfile
  63. char rm_logfile[256] = {0};
  64. time_t ts_rm = ts_now - s_remain_days * SECONDS_PER_DAY;
  65. ts_logfile(ts_rm, rm_logfile, sizeof(rm_logfile));
  66. remove(rm_logfile);
  67. }
  68. }
  69. // open today logfile
  70. if (s_logfp == NULL) {
  71. ts_logfile(ts_now, s_cur_logfile, sizeof(s_cur_logfile));
  72. s_logfp = fopen(s_cur_logfile, "a"); // note: append-mode for multi-processes
  73. s_last_logfile_ts = ts_now;
  74. }
  75. // rewrite if too big
  76. if (s_logfp && ftell(s_logfp) > MAX_LOG_FILESIZE) {
  77. fclose(s_logfp);
  78. s_logfp = NULL;
  79. s_logfp = fopen(s_cur_logfile, "w");
  80. }
  81. return s_logfp;
  82. }
  83. int hlog_set_file(const char* logfile) {
  84. if (logfile == NULL || strlen(logfile) == 0) return -10;
  85. strncpy(s_logfile, logfile, sizeof(s_logfile));
  86. // remove suffix .log
  87. char* suffix = strrchr(s_logfile, '.');
  88. if (suffix && strcmp(suffix, ".log") == 0) {
  89. *suffix = '\0';
  90. }
  91. return 0;
  92. }
  93. void hlog_set_level(int level) {
  94. s_loglevel = level;
  95. }
  96. void hlog_set_remain_days(int days) {
  97. s_remain_days = days;
  98. }
  99. void hlog_enable_color(int on) {
  100. s_logcolor = on;
  101. }
  102. int hlog_printf(int level, const char* fmt, ...) {
  103. if (level < s_loglevel)
  104. return -10;
  105. const char* pcolor = "";
  106. const char* plevel = "";
  107. #define CASE_LOG(id, str, clr) \
  108. case id: plevel = str; pcolor = clr; break;
  109. switch (level) {
  110. FOREACH_LOG(CASE_LOG)
  111. }
  112. #undef CASE_LOG
  113. HLOG_LOCK
  114. FILE* fp = shift_logfile();
  115. if (fp == NULL) {
  116. HLOG_UNLOCK
  117. return -20;
  118. }
  119. datetime_t now = get_datetime();
  120. int len = snprintf(s_logbuf, LOG_BUFSIZE, "[%04d-%02d-%02d %02d:%02d:%02d.%03d][%s]: ",
  121. now.year, now.month, now.day, now.hour, now.min, now.sec, now.ms, plevel);
  122. va_list ap;
  123. va_start(ap, fmt);
  124. len += vsnprintf(s_logbuf + len, LOG_BUFSIZE-len, fmt, ap);
  125. va_end(ap);
  126. if (s_logcolor) {
  127. fprintf(fp, "%s%s%s\n", pcolor, s_logbuf, CL_CLR);
  128. }
  129. else {
  130. fprintf(fp, "%s\n", s_logbuf);
  131. }
  132. if (s_fflush) {
  133. fflush(fp);
  134. }
  135. HLOG_UNLOCK
  136. return len;
  137. }
  138. void hlog_set_fflush(int on) {
  139. s_fflush = on;
  140. }
  141. void hlog_fflush() {
  142. HLOG_LOCK
  143. FILE* fp = shift_logfile();
  144. if (fp) {
  145. fflush(fp);
  146. }
  147. HLOG_UNLOCK
  148. }