From 54f3b686dc2a52d952ccf03945c19985440eb49c Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 15 Jul 2020 22:28:19 +0100 Subject: [PATCH] Fix "slow" file based logging by switching to line based buffering. Closes #1689. Closes #1741. Thanks to Brett M. Gordon and tt92. --- ChangeLog.txt | 2 ++ src/logging.c | 8 +------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 28a04a47d8..0b458670a0 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -13,6 +13,8 @@ Broker: Closes #1726. - Fix websockets clients sometimes not being disconnected promptly. Closes #1718. +- Fix "slow" file based logging by switching to line based buffering. + Closes #1689. Closes #1741. Client library: - Improved documentation around connect callback return codes. Close #1730. diff --git a/src/logging.c b/src/logging.c index c1bc10b10a..a72eb274da 100644 --- a/src/logging.c +++ b/src/logging.c @@ -115,6 +115,7 @@ int log__init(struct mosquitto__config *config) log_priorities = MOSQ_LOG_ERR; log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to open log file %s for writing.", config->log_file); } + setvbuf(config->log_fptr, NULL, _IOLBF, 0); restore_privileges(); } #ifdef WITH_DLT @@ -181,7 +182,6 @@ int log__vprintf(int priority, const char *fmt, va_list va) const char *topic; int syslog_priority; time_t now = time(NULL); - static time_t last_flush = 0; char time_buf[50]; bool log_timestamp = true; char *log_timestamp_format = NULL; @@ -293,7 +293,6 @@ int log__vprintf(int priority, const char *fmt, va_list va) }else{ fprintf(stdout, "%s\n", s); } - fflush(stdout); } if(log_destinations & MQTT3_LOG_STDERR){ if(log_timestamp){ @@ -305,7 +304,6 @@ int log__vprintf(int priority, const char *fmt, va_list va) }else{ fprintf(stderr, "%s\n", s); } - fflush(stderr); } if(log_destinations & MQTT3_LOG_FILE && log_fptr){ if(log_timestamp){ @@ -317,10 +315,6 @@ int log__vprintf(int priority, const char *fmt, va_list va) }else{ fprintf(log_fptr, "%s\n", s); } - if(now - last_flush > 1){ - fflush(log_fptr); - last_flush = now; - } } if(log_destinations & MQTT3_LOG_SYSLOG){ #ifndef WIN32