From 712572743ea1f920f3f092fbe97291176de69878 Mon Sep 17 00:00:00 2001 From: Simon Rose Date: Wed, 25 Feb 2026 09:18:55 +0000 Subject: [PATCH] Add warning if errorlog configuration has been clamped --- modules/libcom/src/error/errlog.c | 58 ++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/modules/libcom/src/error/errlog.c b/modules/libcom/src/error/errlog.c index dcd6c0260..edca6efbf 100644 --- a/modules/libcom/src/error/errlog.c +++ b/modules/libcom/src/error/errlog.c @@ -611,6 +611,33 @@ static void errlogInitPvt(void *arg) } } +static +int errlogClampConfig(struct initArgs *config, int bufsize, int maxMsgSize) +{ + int clamped = 0; + + if (bufsize < MIN_BUFFER_SIZE) + { + // This is to deal with the fact that errlogInit() is often called with arg 0 + clamped = bufsize == 0 ? 0 : 1; + bufsize = MIN_BUFFER_SIZE; + } + config->bufsize = bufsize; + + if (maxMsgSize > MAX_MESSAGE_SIZE(bufsize)) + { + clamped = 1; + maxMsgSize = MAX_MESSAGE_SIZE(bufsize); + } + if (maxMsgSize < MIN_MESSAGE_SIZE) + { + clamped = 1; + maxMsgSize = MIN_MESSAGE_SIZE; + } + config->maxMsgSize = maxMsgSize; + + return clamped; +} int errlogInit2(int bufsize, int maxMsgSize) { @@ -620,15 +647,9 @@ int errlogInit2(int bufsize, int maxMsgSize) if (pvt.atExit) return 0; - if (bufsize < MIN_BUFFER_SIZE) - bufsize = MIN_BUFFER_SIZE; - config.bufsize = bufsize; - - if (maxMsgSize > MAX_MESSAGE_SIZE(bufsize)) - maxMsgSize = MAX_MESSAGE_SIZE(bufsize); - if (maxMsgSize < MIN_MESSAGE_SIZE) - maxMsgSize = MIN_MESSAGE_SIZE; - config.maxMsgSize = maxMsgSize; + if (errlogClampConfig(&config, bufsize, maxMsgSize)) { + fprintf(stderr, "Warning: errlog config clamped from (%zu, %zu) to (%zu, %zu)\n", bufsize, maxMsgSize, config.bufsize, config.maxMsgSize); + } epicsThreadOnce(&errlogOnceFlag, errlogInitPvt, &config); if (pvt.errlogInitFailed) { @@ -647,6 +668,7 @@ int errlogBufResize(int bufsize, int maxMsgSize) { char* logbuf = NULL; char* printbuf = NULL; + struct initArgs config; epicsMutexMustLock(pvt.bufSizeLock); @@ -657,16 +679,12 @@ int errlogBufResize(int bufsize, int maxMsgSize) return -1; } - if (bufsize < MIN_BUFFER_SIZE) - bufsize = MIN_BUFFER_SIZE; + if (errlogClampConfig(&config, bufsize, maxMsgSize)) { + fprintf(stderr, "Warning: errlog config clamped from (%zu, %zu) to (%zu, %zu)\n", bufsize, maxMsgSize, config.bufsize, config.maxMsgSize); + } - if (maxMsgSize > MAX_MESSAGE_SIZE(bufsize)) - maxMsgSize = MAX_MESSAGE_SIZE(bufsize); - if (maxMsgSize < MIN_MESSAGE_SIZE) - maxMsgSize = MIN_MESSAGE_SIZE; - - logbuf = calloc(1, bufsize); - printbuf = calloc(1, bufsize); + logbuf = calloc(1, config.bufsize); + printbuf = calloc(1, config.bufsize); epicsMutexMustLock(pvt.msgQueueLock); @@ -678,8 +696,8 @@ int errlogBufResize(int bufsize, int maxMsgSize) free(pvt.print->base); pvt.print->base = printbuf; - pvt.bufSize = bufsize; - pvt.maxMsgSize = maxMsgSize; + pvt.bufSize = config.bufsize; + pvt.maxMsgSize = config.maxMsgSize; epicsMutexUnlock(pvt.msgQueueLock); epicsMutexUnlock(pvt.bufSizeLock);