diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2018-11-07 12:19:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2018-11-07 12:20:17 +0000 |
commit | a64a253794ac64cb40befee54db53bde17dd0d49 (patch) | |
tree | c1024acc5f6e508814b944d99f112259bb28b1be /libnetdata/log/log.c | |
parent | New upstream version 1.10.0+dfsg (diff) | |
download | netdata-upstream/1.11.0+dfsg.tar.xz netdata-upstream/1.11.0+dfsg.zip |
New upstream version 1.11.0+dfsgupstream/1.11.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | libnetdata/log/log.c (renamed from src/log.c) | 67 |
1 files changed, 38 insertions, 29 deletions
diff --git a/src/log.c b/libnetdata/log/log.c index edb4482a0..198e98bd9 100644 --- a/src/log.c +++ b/libnetdata/log/log.c @@ -1,4 +1,6 @@ -#include "common.h" +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "../libnetdata.h" int web_server_is_multithreaded = 1; @@ -56,7 +58,7 @@ static inline void log_unlock() { netdata_mutex_unlock(&log_mutex); } -int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) { +static FILE *open_log_file(int fd, FILE *fp, const char *filename, int *enabled_syslog, int is_stdaccess, int *fd_ptr) { int f, devnull = 0; if(!filename || !*filename || !strcmp(filename, "none") || !strcmp(filename, "/dev/null")) { @@ -75,8 +77,10 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) // don't do anything if the user is willing // to have the standard one if(!strcmp(filename, "system")) { - if(fd != -1 && fp != &stdaccess) - return fd; + if(fd != -1 && !is_stdaccess) { + if(fd_ptr) *fd_ptr = fd; + return fp; + } filename = "stderr"; } @@ -91,19 +95,20 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664); if(f == -1) { error("Cannot open file '%s'. Leaving %d to its default.", filename, fd); - return fd; + if(fd_ptr) *fd_ptr = fd; + return fp; } } - if(devnull && fp == &stdaccess) { - fd = -1; - *fp = NULL; - } - // if there is a level-2 file pointer // flush it before switching the level-1 fds - if(fp && *fp) - fflush(*fp); + if(fp) + fflush(fp); + + if(devnull && is_stdaccess) { + fd = -1; + fp = NULL; + } if(fd != f && fd != -1) { // it automatically closes @@ -111,57 +116,61 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) if (t == -1) { error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename); close(f); - return fd; + if(fd_ptr) *fd_ptr = fd; + return fp; } // info("dup2() new fd %d to old fd %d for '%s'", f, fd, filename); close(f); } else fd = f; - if(fp && !*fp) { - *fp = fdopen(fd, "a"); - if (!*fp) + if(!fp) { + fp = fdopen(fd, "a"); + if (!fp) error("Cannot fdopen() fd %d ('%s')", fd, filename); else { - if (setvbuf(*fp, NULL, _IOLBF, 0) != 0) + if (setvbuf(fp, NULL, _IOLBF, 0) != 0) error("Cannot set line buffering on fd %d ('%s')", fd, filename); } } - return fd; + if(fd_ptr) *fd_ptr = fd; + return fp; } void reopen_all_log_files() { if(stdout_filename) - open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog); + open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL); if(stderr_filename) - open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog); + open_log_file(STDERR_FILENO, stderr, stderr_filename, &error_log_syslog, 0, NULL); if(stdaccess_filename) - stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog); + stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd); } void open_all_log_files() { // disable stdin - open_log_file(STDIN_FILENO, (FILE **)&stdin, "/dev/null", NULL); + open_log_file(STDIN_FILENO, stdin, "/dev/null", NULL, 0, NULL); - open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog); - open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog); - stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog); + open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL); + open_log_file(STDERR_FILENO, stderr, stderr_filename, &error_log_syslog, 0, NULL); + stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd); } // ---------------------------------------------------------------------------- // error log throttling -time_t error_log_throttle_period_backup = 0; time_t error_log_throttle_period = 1200; unsigned long error_log_errors_per_period = 200; +unsigned long error_log_errors_per_period_backup = 0; int error_log_limit(int reset) { static time_t start = 0; static unsigned long counter = 0, prevented = 0; + // fprintf(stderr, "FLOOD: counter=%lu, allowed=%lu, backup=%lu, period=%llu\n", counter, error_log_errors_per_period, error_log_errors_per_period_backup, (unsigned long long)error_log_throttle_period); + // do not throttle if the period is 0 if(error_log_throttle_period == 0) return 0; @@ -181,7 +190,7 @@ int error_log_limit(int reset) { if(prevented) { char date[LOG_DATE_LENGTH]; log_date(date, LOG_DATE_LENGTH); - fprintf(stderr, "%s: %s Resetting logging for process '%s' (prevented %lu logs in the last %ld seconds).\n" + fprintf(stderr, "%s: %s LOG FLOOD PROTECTION reset for process '%s' (prevented %lu logs in the last %ld seconds).\n" , date , program_name , program_name @@ -202,7 +211,7 @@ int error_log_limit(int reset) { if(prevented) { char date[LOG_DATE_LENGTH]; log_date(date, LOG_DATE_LENGTH); - fprintf(stderr, "%s: %s Resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n" + fprintf(stderr, "%s: %s LOG FLOOD PROTECTION resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n" , date , program_name , program_name @@ -224,7 +233,7 @@ int error_log_limit(int reset) { if(!prevented) { char date[LOG_DATE_LENGTH]; log_date(date, LOG_DATE_LENGTH); - fprintf(stderr, "%s: %s Too many logs (%lu logs in %ld seconds, threshold is set to %lu logs in %ld seconds). Preventing more logs from process '%s' for %ld seconds.\n" + fprintf(stderr, "%s: %s LOG FLOOD PROTECTION too many logs (%lu logs in %ld seconds, threshold is set to %lu logs in %ld seconds). Preventing more logs from process '%s' for %ld seconds.\n" , date , program_name , counter |