From 2a2cb0310baac013dde63c5e651f3cfc363a1780 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 17 Nov 2020 11:06:20 +0000 Subject: [PATCH] Fix file logging on Windows. Closes #1880. Thanks to J1EXA, richl, and EnneiteZer. --- ChangeLog.txt | 1 + src/logging.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index f6ccf7b4..f79bcee0 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -3,6 +3,7 @@ Broker: - Fix listener not being reassociated with client when reloading a persistence file and `per_listener_settings true` is set and the client did not set a username. Closes #1891. +- Fix file logging on Windows. Closes #1880. 1.6.12 - 2020-08-19 diff --git a/src/logging.c b/src/logging.c index ac079a85..52afadc9 100644 --- a/src/logging.c +++ b/src/logging.c @@ -39,6 +39,8 @@ extern struct mosquitto_db int_db; HANDLE syslog_h; #endif +static char log_fptr_buffer[BUFSIZ]; + /* Options for logging should be: * * A combination of: @@ -135,7 +137,7 @@ int log__init(struct mosquitto__config *config) } config->log_fptr = mosquitto__fopen(config->log_file, "at", true); if(config->log_fptr){ - setvbuf(config->log_fptr, NULL, _IOLBF, 0); + setvbuf(config->log_fptr, log_fptr_buffer, _IOLBF, sizeof(log_fptr_buffer)); }else{ log_destinations = MQTT3_LOG_STDERR; log_priorities = MOSQ_LOG_ERR; @@ -345,6 +347,10 @@ int log__vprintf(int priority, const char *fmt, va_list va) }else{ fprintf(log_fptr, "%s\n", s); } +#ifdef WIN32 + /* Windows does support line buffering, so flush. */ + fflush(log_fptr); +#endif } if(log_destinations & MQTT3_LOG_SYSLOG){ #ifndef WIN32