浏览代码

log: support disable log

Nick Peng 2 年之前
父节点
当前提交
3a1ba73386
共有 6 个文件被更改,包括 44 次插入44 次删除
  1. 1 1
      ReadMe.md
  2. 3 3
      ReadMe_en.md
  3. 1 1
      etc/smartdns/smartdns.conf
  4. 31 32
      src/tlog.c
  5. 6 6
      src/tlog.h
  6. 2 1
      src/util.c

+ 1 - 1
ReadMe.md

@@ -530,7 +530,7 @@ entware|ipkg update<br>ipkg install smartdns|软件源路径:https://bin.entwa
 | log-level | 设置日志级别 | error | fatal、error、warn、notice、info 或 debug | log-level error |
 | log-file | 日志文件路径 | /var/log/smartdns/smartdns.log | 合法路径字符串 | log-file /var/log/smartdns/smartdns.log |
 | log-size | 日志大小 | 128K | 数字 + K、M 或 G | log-size 128K |
-| log-num | 日志归档个数 | 2 | 大于等于 0 的数字 | log-num 2 |
+| log-num | 日志归档个数 | openwrt为2, 其他系统为8 | 大于等于 0 的数字,0表示禁用日志 | log-num 2 |
 | audit-enable | 设置审计启用 | no | [yes\|no] | audit-enable yes |
 | audit-file | 审计文件路径 | /var/log/smartdns/smartdns-audit.log | 合法路径字符串 | audit-file /var/log/smartdns/smartdns-audit.log |
 | audit-size | 审计大小 | 128K | 数字 + K、M 或 G | audit-size 128K |

+ 3 - 3
ReadMe_en.md

@@ -17,7 +17,7 @@ Support Raspberry Pi, openwrt, ASUS router, Windows and other devices.
   - [Usage](#usage)
     - [Use official installation source](#use-official-installation-source)
     - [Download the package](#download-the-package)
-    - [Standard Linux system installation/Raspberry Pi, X86_64 system](#standard-linux-system-installationraspberry-pi-x86_64-system)
+    - [Standard Linux system installation/Raspberry Pi, X86\_64 system](#standard-linux-system-installationraspberry-pi-x86_64-system)
     - [openwrt](#openwrt)
     - [ASUS router native firmware / Merlin firmware](#asus-router-native-firmware--merlin-firmware)
     - [optware/entware](#optwareentware)
@@ -489,11 +489,11 @@ Note: Merlin firmware is derived from ASUS firmware and can theoretically be use
 |log-level|log level|error|fatal,error,warn,notice,info,debug|log-level error
 |log-file|log path|/var/log/smartdns/smartdns.log|File Pah|log-file /var/log/smartdns/smartdns.log
 |log-size|log size|128K|number+K,M,G|log-size 128K
-|log-num|archived log number|2|Integer|log-num 2
+|log-num|archived log number|2 for openwrt, 8 for other system|Integer, 0 means turn off the log|log-num 2
 |audit-enable|audit log enable|no|[yes\|no]|audit-enable yes
 |audit-file|audit log file|/var/log/smartdns/smartdns-audit.log|File Path|audit-file /var/log/smartdns/smartdns-audit.log
 |audit-size|audit log size|128K|number+K,M,G|audit-size 128K
-|audit-num|archived audit log number|2|Integer|audit-num 2
+|audit-num|archived audit log number|2|Integer, 0 means turn off the log|audit-num 2
 |conf-file|additional conf file|None|File path|conf-file /etc/smartdns/smartdns.more.conf
 |server|Upstream UDP DNS server|None|Repeatable <br>`[ip][:port]`: Server IP, port optional. <br>`[-blacklist-ip]`: The "-blacklist-ip" parameter is to filtering IPs which is configured by "blacklist-ip". <br>`[-whitelist-ip]`: whitelist-ip parameter specifies that only the IP range configured in whitelist-ip is accepted. <br>`[-group [group] ...]`: The group to which the DNS server belongs, such as office, foreign, use with nameserver. <br>`[-exclude-default-group]`: Exclude DNS servers from the default group| server 8.8.8.8:53 -blacklist-ip
 |server-tcp|Upstream TCP DNS server|None|Repeatable <br>`[ip][:port]`: Server IP, port optional. <br>`[-blacklist-ip]`: The "-blacklist-ip" parameter is to filtering IPs which is configured by "blacklist-ip". <br>`[-whitelist-ip]`: whitelist-ip parameter specifies that only the IP range configured in whitelist-ip is accepted. <br>`[-group [group] ...]`: The group to which the DNS server belongs, such as office, foreign, use with nameserver. <br>`[-exclude-default-group]`: Exclude DNS servers from the default group| server-tcp 8.8.8.8:53

+ 1 - 1
etc/smartdns/smartdns.conf

@@ -132,7 +132,7 @@ force-qtype-SOA 65
 # log-level: [level], level=fatal, error, warn, notice, info, debug
 # log-file: file path of log file.
 # log-size: size of each log file, support k,m,g
-# log-num: number of logs
+# log-num: number of logs, 0 means disable log
 log-level info
 
 # log-file /var/log/smartdns/smartdns.log

+ 31 - 32
src/tlog.c

@@ -79,9 +79,9 @@ struct tlog_log {
     int zip_pid;
     int multi_log;
     int logscreen;
-    int no_write_log;
     int segment_log;
     int max_line_size;
+    int print_errmsg;
 
     tlog_output_func output_func;
     void *private_data;
@@ -315,6 +315,19 @@ void tlog_set_maxline_size(struct tlog_log *log, int size)
     log->max_line_size = size;
 }
 
+void tlog_logcount(struct tlog_log *log, int count)
+{
+    if (log == NULL) {
+        return;
+    }
+
+    if (count < 0) {
+        count = 0;
+    }
+
+    log->logcount = count;
+}
+
 void tlog_set_permission(struct tlog_log *log, unsigned int file, unsigned int archive)
 {
     log->file_perm = file;
@@ -505,6 +518,10 @@ static int _tlog_vprintf(struct tlog_log *log, vprint_callback print_callback, v
         return -1;
     }
 
+    if (unlikely(log->logcount <= 0 && log->logscreen == 0) ) {
+        return 0;
+    }
+
     if (_tlog_need_drop(log) == 0) {
         return -1;
     }
@@ -1130,7 +1147,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
         unused = write(STDOUT_FILENO, buff, bufflen);
     }
 
-    if (log->no_write_log) {
+    if (log->logcount <= 0) {
         return 0;
     }
 
@@ -1153,7 +1170,6 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
 
     if (log->fd <= 0) {
         /* open a new log file to write */
-        static int print_errmsg = 1;
         time_t now;
 
         time(&now);
@@ -1164,14 +1180,15 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
 
         char logfile[PATH_MAX * 2];
         if (_tlog_mkdir(log->logdir) != 0) {
-            if (print_errmsg == 0) {
+            if (log->print_errmsg == 0) {
                 return -1;
             }
-            print_errmsg = 0;
+            log->print_errmsg = 0;
             fprintf(stderr, "create log dir %s failed, %s\n", log->logdir, strerror(errno));
             if (errno == EACCES && log->logscreen == 0) {
                 fprintf(stderr, "no permission to write log file, output log to console\n");
-                tlog_logscreen_only(log, 1);
+                tlog_logscreen(log, 1);
+                tlog_logcount(log, 0);
             }
             return -1;
         }
@@ -1179,17 +1196,17 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
         log->filesize = 0;
         log->fd = open(logfile, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, log->file_perm);
         if (log->fd < 0) {
-            if (print_errmsg == 0) {
+            if (log->print_errmsg == 0) {
                 return -1;
             }
 
             fprintf(stderr, "open log file %s failed, %s\n", logfile, strerror(errno));
-            print_errmsg = 0;
+            log->print_errmsg = 0;
             return -1;
         }
 
         log->last_try = 0;
-        print_errmsg = 1;
+        log->print_errmsg = 1;
         /* get log file size */
         log->filesize = lseek(log->fd, 0, SEEK_END);
     }
@@ -1577,23 +1594,18 @@ const char *tlog_get_level_string(tlog_level level)
     return tlog_level_str[level];
 }
 
-static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
+void tlog_set_maxlog_count(int count)
 {
-    if (log == NULL) {
-        return;
-    }
-
-    log->logscreen = (enable != 0) ? 1 : 0;
+    tlog_logcount(tlog.root, count);
 }
 
-static void _tlog_log_setlogscreen_only(struct tlog_log *log, int enable)
+static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
 {
     if (log == NULL) {
         return;
     }
 
     log->logscreen = (enable != 0) ? 1 : 0;
-    log->no_write_log = (enable != 0) ? 1 : 0;
 }
 
 void tlog_setlogscreen(int enable)
@@ -1601,11 +1613,6 @@ void tlog_setlogscreen(int enable)
     _tlog_log_setlogscreen(tlog.root, enable);
 }
 
-void tlog_setlogscreen_only(int enable)
-{
-    _tlog_log_setlogscreen_only(tlog.root, enable);
-}
-
 int tlog_write_log(char *buff, int bufflen)
 {
     if (unlikely(tlog.root == NULL)) {
@@ -1624,15 +1631,6 @@ void tlog_logscreen(tlog_log *log, int enable)
     _tlog_log_setlogscreen(log, enable);
 }
 
-void tlog_logscreen_only(tlog_log *log, int enable)
-{
-    if (log == NULL) {
-        return;
-    }
-
-    _tlog_log_setlogscreen_only(log, enable);
-}
-
 int tlog_reg_output_func(tlog_log *log, tlog_output_func output)
 {
     if (log == NULL) {
@@ -1704,12 +1702,13 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
     log->dropped = 0;
     log->buffsize = (buffsize > 0) ? buffsize : TLOG_BUFF_SIZE;
     log->logsize = (maxlogsize >= 0) ? maxlogsize : TLOG_LOG_SIZE;
-    log->logcount = (maxlogcount > 0) ? maxlogcount : TLOG_LOG_COUNT;
+    log->logcount = (maxlogcount <= 0) ? 0 : maxlogcount;
     log->fd = -1;
     log->filesize = 0;
     log->zip_pid = -1;
     log->is_exit = 0;
     log->fail = 0;
+    log->print_errmsg = 1;
     log->waiters = 0;
     log->block = ((flag & TLOG_NONBLOCK) == 0) ? 1 : 0;
     log->nocompress = ((flag & TLOG_NOCOMPRESS) == 0) ? 0 : 1;

+ 6 - 6
src/tlog.h

@@ -104,15 +104,15 @@ extern void tlog_set_logfile(const char *logfile);
 /* enalbe log to screen */
 extern void tlog_setlogscreen(int enable);
 
-/* output log to screen only */
-extern void tlog_setlogscreen_only(int enable);
-
 /* enalbe early log to screen */
 extern void tlog_set_early_printf(int enable);
 
 /* Get log level in string */
 extern const char *tlog_get_level_string(tlog_level level);
 
+/* set max log count */
+extern void tlog_set_maxlog_count(int count);
+
 /*
 Function: Initialize log module
 logfile: log file.
@@ -187,9 +187,6 @@ extern int tlog_vprintf(tlog_log *log, const char *format, va_list ap);
 /* enalbe log to screen */
 extern void tlog_logscreen(tlog_log *log, int enable);
 
-/* enalbe log to screen only*/
-extern void tlog_logscreen_only(tlog_log *log, int enable);
-
 /* register output callback */
 typedef int (*tlog_output_func)(struct tlog_log *log, const char *buff, int bufflen);
 extern int tlog_reg_output_func(tlog_log *log, tlog_output_func output);
@@ -206,6 +203,9 @@ extern int tlog_localtime(struct tlog_time *tm);
 /* set max line size */
 extern void tlog_set_maxline_size(struct tlog_log *log, int size);
 
+/* set max log count */
+extern void tlog_logcount(struct tlog_log *log, int count);
+
 /*
 Function: set log file and archive permission
 log: log stream

+ 2 - 1
src/util.c

@@ -1468,7 +1468,8 @@ int dns_packet_debug(const char *packet_file)
 	struct _dns_read_packet_info *info = NULL;
 	char buff[DNS_PACKSIZE];
 
-	tlog_setlogscreen_only(1);
+	tlog_set_maxlog_count(0);
+	tlog_setlogscreen(1);;
 	tlog_setlevel(TLOG_DEBUG);
 
 	info = _dns_read_packet_file(packet_file);