Fix libcommon log printing

This commit is contained in:
Roger A. Light
2025-10-29 23:06:33 +00:00
parent a3ea371e39
commit 27aa1e14cb
3 changed files with 34 additions and 4 deletions
+2
View File
@@ -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
+23 -4
View File
@@ -24,6 +24,7 @@ Contributors:
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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;
}
+9
View File
@@ -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);
}