From 27aa1e14cbd0f248ccd7e8dbd403884795887b8d Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 29 Oct 2025 23:06:33 +0000 Subject: [PATCH] Fix libcommon log printing --- include/mosquitto/libcommon_file.h | 2 ++ libcommon/file_common.c | 27 +++++++++++++++++++++++---- src/logging.c | 9 +++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/include/mosquitto/libcommon_file.h b/include/mosquitto/libcommon_file.h index 4ad3646a..2304a6e7 100644 --- a/include/mosquitto/libcommon_file.h +++ b/include/mosquitto/libcommon_file.h @@ -61,6 +61,8 @@ libmosqcommon_EXPORT int mosquitto_read_file(const char *file, bool restrict_rea */ libmosqcommon_EXPORT char *mosquitto_trimblanks(char *str); +extern void (*libcommon_vprintf)(const char *fmt, va_list va); + #ifdef __cplusplus } #endif diff --git a/libcommon/file_common.c b/libcommon/file_common.c index 1ba4d8b3..4895ad41 100644 --- a/libcommon/file_common.c +++ b/libcommon/file_common.c @@ -24,6 +24,7 @@ Contributors: #include #include #include +#include #include #include #include @@ -46,6 +47,24 @@ Contributors: #include "mosquitto.h" +void (*libcommon_vprintf)(const char *fmt, va_list va) = NULL; + + +void libcommon_printf(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + + if(libcommon_vprintf){ + libcommon_vprintf(fmt, va); + }else{ + vfprintf(stderr, fmt, va); + } + + va_end(va); +} + FILE *mosquitto_fopen(const char *path, const char *mode, bool restrict_read) { @@ -185,7 +204,7 @@ FILE *mosquitto_fopen(const char *path, const char *mode, bool restrict_read) if(restrict_read){ if(statbuf.st_mode & S_IRWXO){ - fprintf(stderr, + libcommon_printf( "Warning: File %s has world readable permissions. Future versions will refuse to load this file.\n" "To fix this, use `chmod 0700 %s`.\n", path, path); @@ -199,7 +218,7 @@ FILE *mosquitto_fopen(const char *path, const char *mode, bool restrict_read) getpwuid_r(getuid(), &pw, buf, sizeof(buf), &result); if(result){ - fprintf(stderr, + libcommon_printf( "Warning: File %s owner is not %s. Future versions will refuse to load this file." "To fix this, use `chown %s %s`.\n", path, result->pw_name, result->pw_name, path); @@ -214,7 +233,7 @@ FILE *mosquitto_fopen(const char *path, const char *mode, bool restrict_read) struct group grp, *result; if(getgrgid_r(getgid(), &grp, buf, sizeof(buf), &result) == 0){ - fprintf(stderr, + libcommon_printf( "Warning: File %s group is not %s. Future versions will refuse to load this file.\n", path, result->gr_name); } @@ -226,7 +245,7 @@ FILE *mosquitto_fopen(const char *path, const char *mode, bool restrict_read) } if(!S_ISREG(statbuf.st_mode)){ - fprintf(stderr, "Error: %s is not a file.", path); + libcommon_printf("Error: %s is not a file.", path); fclose(fptr); return NULL; } diff --git a/src/logging.c b/src/logging.c index 6dc19044..8cdaabd4 100644 --- a/src/logging.c +++ b/src/logging.c @@ -50,6 +50,7 @@ static const char *LOG_TAG = "mosquitto"; #endif static char log_fptr_buffer[BUFSIZ]; +static void libcommon__vprintf(const char *fmt, va_list va); /* Options for logging should be: * @@ -114,6 +115,8 @@ int log__init(struct mosquitto__config *config) { int rc = 0; + libcommon_vprintf = libcommon__vprintf; + log_priorities = config->log_type; log_destinations = config->log_dest; @@ -438,3 +441,9 @@ BROKER_EXPORT void mosquitto_log_printf(int level, const char *fmt, ...) log__vprintf((unsigned int)level, fmt, va); va_end(va); } + + +static void libcommon__vprintf(const char *fmt, va_list va) +{ + log__vprintf(MOSQ_LOG_INFO, fmt, va); +}