ソースを参照

add log_remain_days

ithewei 6 年 前
コミット
aa94611993
4 ファイル変更78 行追加28 行削除
  1. 3 2
      etc/test.conf
  2. 46 18
      hlog.cpp
  3. 11 6
      hlog.h
  4. 18 2
      main.cpp.tmpl

+ 3 - 2
etc/test.conf

@@ -1,10 +1,11 @@
 # [root]
 
-# loglevel = {"DEBUG", "INFO", "WARN", "ERROR"}
+# logfile = logs/test.log
+# loglevel = [VERBOSE,DEBUG,INFO,WARN,ERROR,FATAL,SILENT]
 loglevel = DEBUG
+log_remain_days = 3
 
 # if not set worker_processes, default = ncpu
 worker_processes = 4
 
 port = 8086
-

+ 46 - 18
hlog.cpp

@@ -7,27 +7,52 @@
 
 #include "htime.h"  // for get_datetime
 
-#define LOGBUF_SIZE         (1<<13)  // 8k
-#define LOGFILE_MAXSIZE     (1<<23)  // 8M
-
-static FILE* s_logfp = NULL;
-static char s_logfile[256] = DEFAULT_LOG_FILE;
-static int s_loglevel = DEFAULT_LOG_LEVEL;
-static bool s_logcolor = false;
-static char s_logbuf[LOGBUF_SIZE];
+#define SECONDS_PER_DAY     86400
+
+static char     s_logfile[256] = DEFAULT_LOG_FILE;
+static int      s_loglevel = DEFAULT_LOG_LEVEL;
+static bool     s_logcolor = false;
+static int      s_remain_days = DEFAULT_LOG_REMAIN_DAYS;
+
+static FILE*    s_logfp = NULL;
+static char     s_logbuf[LOG_BUFSIZE];
+static char     s_cur_logfile[256] = {0};
 static std::mutex s_mutex;
 
+static void ts_logfile(time_t ts, char* buf, int len) {
+    struct tm* tm = localtime(&ts);
+    snprintf(buf, len, "%s-%04d-%02d-%02d.log",
+            s_logfile,
+            tm->tm_year+1900,
+            tm->tm_mon+1,
+            tm->tm_mday);
+}
+
 int hlog_set_file(const char* logfile) {
-    if (logfile && strlen(logfile) > 0) {
-        strncpy(s_logfile, logfile, 256);
+    if (logfile == NULL || strlen(logfile) == 0)    return -10;
+
+    strncpy(s_logfile, logfile, sizeof(s_logfile));
+    // remove suffix .log
+    char* suffix = strrchr(s_logfile, '.');
+    if (suffix && strcmp(suffix, ".log") == 0) {
+        *suffix = '\0';
     }
 
+    time_t ts_now = time(NULL);
+    ts_logfile(ts_now, s_cur_logfile, sizeof(s_cur_logfile));
     if (s_logfp) {
         fclose(s_logfp);
         s_logfp = NULL;
     }
-
-    s_logfp = fopen(s_logfile, "a");
+    s_logfp = fopen(s_cur_logfile, "a");
+
+    // remove logfile before s_remain_days
+    if (s_remain_days > 0) {
+        time_t ts_rm  = ts_now - s_remain_days * SECONDS_PER_DAY;
+        char rm_logfile[256] = {0};
+        ts_logfile(ts_rm, rm_logfile, sizeof(rm_logfile));
+        remove(rm_logfile);
+    }
 
     return s_logfp ? 0 : -1;
 }
@@ -36,6 +61,10 @@ void hlog_set_level(int level) {
     s_loglevel = level;
 }
 
+void hlog_set_remain_days(int days) {
+    s_remain_days = days;
+}
+
 void hlog_enable_color(bool enable) {
     s_logcolor = enable;
 }
@@ -64,21 +93,20 @@ int hlog_printf(int level, const char* fmt, ...) {
             return -20;
     }
 
-    if (ftell(s_logfp) > LOGFILE_MAXSIZE) {
+    if (ftell(s_logfp) > MAX_LOG_FILESIZE) {
         fclose(s_logfp);
-        s_logfp = fopen(s_logfile, "w");
+        s_logfp = fopen(s_cur_logfile, "w");
         if (!s_logfp)
             return -30;
     }
 
     datetime_t now = get_datetime();
-
-    int len = snprintf(s_logbuf, LOGBUF_SIZE, "%s[%04d-%02d-%02d %02d:%02d:%02d.%03d][%s]: ",
+    int len = snprintf(s_logbuf, LOG_BUFSIZE, "%s[%04d-%02d-%02d %02d:%02d:%02d.%03d][%s]: ",
         pcolor, now.year, now.month, now.day, now.hour, now.min, now.sec, now.ms, plevel);
 
     va_list ap;
     va_start(ap, fmt);
-    len += vsnprintf(s_logbuf + len, LOGBUF_SIZE-len, fmt, ap);
+    len += vsnprintf(s_logbuf + len, LOG_BUFSIZE-len, fmt, ap);
     va_end(ap);
 
     fprintf(s_logfp, "%s\n", s_logbuf);
@@ -86,7 +114,7 @@ int hlog_printf(int level, const char* fmt, ...) {
         fprintf(s_logfp, CL_CLR);
     }
 
-    fflush(NULL);
+    fflush(s_logfp);
 
     return len;
 }

+ 11 - 6
hlog.h

@@ -29,17 +29,22 @@
     F(LOG_LEVEL_FATAL, "FATAL", CL_RED_WHT)
 
 enum LOG_LEVEL {
-    LOG_LEVEL_NONE = 0,
-#define ENUM_LOG(id, str, clr) id,
-    FOREACH_LOG(ENUM_LOG)
-#undef  ENUM_LOG
+    LOG_LEVEL_VERBOSE = 0,
+#define ENUM_LOG_LEVEL(id, str, clr) id,
+    FOREACH_LOG(ENUM_LOG_LEVEL)
+#undef  ENUM_LOG_LEVEL
+    LOG_LEVEL_SILENT
 };
 
-#define DEFAULT_LOG_FILE    "./default.log"
-#define DEFAULT_LOG_LEVEL   LOG_LEVEL_NONE
+#define DEFAULT_LOG_FILE            "default.log"
+#define DEFAULT_LOG_LEVEL           LOG_LEVEL_VERBOSE
+#define DEFAULT_LOG_REMAIN_DAYS     1
+#define LOG_BUFSIZE                 (1<<13)  // 8k
+#define MAX_LOG_FILESIZE            (1<<23)  // 8M
 
 int     hlog_set_file(const char* file);
 void    hlog_set_level(int level);
+void    hlog_set_remain_days(int days);
 void    hlog_enable_color(bool enable);
 int     hlog_printf(int level, const char* fmt, ...);
 

+ 18 - 2
main.cpp.tmpl

@@ -71,9 +71,16 @@ int parse_confile(const char* confile) {
         exit(-40);
     }
 
+    // logfile
+    string str = g_conf_ctx.parser->GetValue("logfile");
+    if (!str.empty()) {
+        hlog_set_file(str.c_str());
+    }
     // loglevel
     const char* szLoglevel = g_conf_ctx.parser->GetValue("loglevel").c_str();
-    if (stricmp(szLoglevel, "DEBUG") == 0) {
+    if (stricmp(szLoglevel, "VERBOSE") == 0) {
+        g_conf_ctx.loglevel = LOG_LEVEL_VERBOSE;
+    } else if (stricmp(szLoglevel, "DEBUG") == 0) {
         g_conf_ctx.loglevel = LOG_LEVEL_DEBUG;
     } else if (stricmp(szLoglevel, "INFO") == 0) {
         g_conf_ctx.loglevel = LOG_LEVEL_INFO;
@@ -81,10 +88,19 @@ int parse_confile(const char* confile) {
         g_conf_ctx.loglevel = LOG_LEVEL_WARN;
     } else if (stricmp(szLoglevel, "ERROR") == 0) {
         g_conf_ctx.loglevel = LOG_LEVEL_ERROR;
+    } else if (stricmp(szLoglevel, "FATAL") == 0) {
+        g_conf_ctx.loglevel = LOG_LEVEL_FATAL;
+    } else if (stricmp(szLoglevel, "SILENT") == 0) {
+        g_conf_ctx.loglevel = LOG_LEVEL_SILENT;
     } else {
-        g_conf_ctx.loglevel = LOG_LEVEL_DEBUG;
+        g_conf_ctx.loglevel = LOG_LEVEL_VERBOSE;
     }
     hlog_set_level(g_conf_ctx.loglevel);
+    // log_remain_days
+    str = g_conf_ctx.parser->GetValue("log_remain_days");
+    if (!str.empty()) {
+        hlog_set_remain_days(atoi(str.c_str()));
+    }
 
     // worker_processes
     int worker_processes = 0;