hlog.cpp 4.0 KB

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