From 10e0ef6d3ec38bc223483d439eb3e420a2eac23b Mon Sep 17 00:00:00 2001 From: Simon Rose Date: Wed, 25 Feb 2026 10:30:51 +0000 Subject: [PATCH] Remove errlogBufResize in favour of allowing errlogInit[2] to be called repeatedly --- modules/libcom/src/error/errlog.c | 140 ++++++++++++---------- modules/libcom/src/error/errlog.h | 9 -- modules/libcom/src/iocsh/libComRegister.c | 17 --- 3 files changed, 75 insertions(+), 91 deletions(-) diff --git a/modules/libcom/src/error/errlog.c b/modules/libcom/src/error/errlog.c index edca6efbf..446bb835c 100644 --- a/modules/libcom/src/error/errlog.c +++ b/modules/libcom/src/error/errlog.c @@ -556,9 +556,37 @@ struct initArgs { size_t maxMsgSize; }; +static +int errlogClampConfig(struct initArgs *config) +{ + int clamped = 0; + + if (config->bufsize < MIN_BUFFER_SIZE) + { + clamped = 1; + config->bufsize = MIN_BUFFER_SIZE; + } + + if (config->maxMsgSize > MAX_MESSAGE_SIZE(config->bufsize)) + { + clamped = 1; + config->maxMsgSize = MAX_MESSAGE_SIZE(config->bufsize); + } + if (config->maxMsgSize < MIN_MESSAGE_SIZE) + { + clamped = 1; + config->maxMsgSize = MIN_MESSAGE_SIZE; + } + + return clamped; +} + static void errlogInitPvt(void *arg) { struct initArgs *pconfig = (struct initArgs *) arg; + + errlogClampConfig(pconfig); + epicsThreadId tid = NULL; epicsThreadOpts topts = EPICS_THREAD_OPTS_INIT; @@ -611,81 +639,33 @@ static void errlogInitPvt(void *arg) } } +// This function must be called with pvt.bufSizeLock held static -int errlogClampConfig(struct initArgs *config, int bufsize, int maxMsgSize) +void errlogBufResize(struct initArgs config) { - 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; + if (config.bufsize < pvt.bufSize) { + fprintf(stderr, "Warning: Cannot shrink buffer size.\n"); + return; } - 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) -{ - static epicsThreadOnceId errlogOnceFlag = EPICS_THREAD_ONCE_INIT; - struct initArgs config; - - if (pvt.atExit) - return 0; - - if (errlogClampConfig(&config, bufsize, maxMsgSize)) { + size_t bufsize = config.bufsize; + size_t maxMsgSize = config.maxMsgSize; + if (errlogClampConfig(&config)) { 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) { - fprintf(stderr,"errlogInit failed\n"); - exit(1); + char *logbuf = calloc(1, config.bufsize); + if (!logbuf) { + fprintf(stderr, ERL_ERROR ": Failed to allocated errlog log buffer.\n"); + return; } - return 0; -} - -int errlogInit(int bufsize) -{ - return errlogInit2(bufsize, DEFAULT_MAX_MSG_SIZE); -} - -int errlogBufResize(int bufsize, int maxMsgSize) -{ - char* logbuf = NULL; - char* printbuf = NULL; - struct initArgs config; - - epicsMutexMustLock(pvt.bufSizeLock); - - // Only increasing the buffer size is allowed! - if (bufsize < pvt.bufSize) { - epicsMutexUnlock(pvt.bufSizeLock); - fprintf(stderr, "Shrinking the errorlog buffer is not allowed.\n"); - return -1; + char *printbuf = calloc(1, config.bufsize); + if (!printbuf) { + free(logbuf); + fprintf(stderr, ERL_ERROR ": Failed to allocated errlog print buffer.\n"); + return; } - if (errlogClampConfig(&config, bufsize, maxMsgSize)) { - fprintf(stderr, "Warning: errlog config clamped from (%zu, %zu) to (%zu, %zu)\n", bufsize, maxMsgSize, config.bufsize, config.maxMsgSize); - } - - logbuf = calloc(1, config.bufsize); - printbuf = calloc(1, config.bufsize); - epicsMutexMustLock(pvt.msgQueueLock); memcpy(logbuf, pvt.log->base, pvt.bufSize); @@ -700,7 +680,37 @@ int errlogBufResize(int bufsize, int maxMsgSize) pvt.maxMsgSize = config.maxMsgSize; epicsMutexUnlock(pvt.msgQueueLock); +} + +int errlogInit2(int bufsize, int maxMsgSize) +{ + static epicsThreadOnceId errlogOnceFlag = EPICS_THREAD_ONCE_INIT; + struct initArgs config = {bufsize, maxMsgSize}; + + if (pvt.atExit) + return 0; + + epicsThreadOnce(&errlogOnceFlag, errlogInitPvt, &config); + if (pvt.errlogInitFailed) { + fprintf(stderr,"errlogInit failed\n"); + exit(1); + } + + // Don't resize the buffer for any of the calls to errlogInit(0) + if (bufsize == 0) return 0; + + epicsMutexMustLock(pvt.bufSizeLock); + if (pvt.bufSize != config.bufsize || pvt.maxMsgSize != config.maxMsgSize) { + // We are resizing the buffer after initialisation + errlogBufResize(config); + } epicsMutexUnlock(pvt.bufSizeLock); + return 0; +} + +int errlogInit(int bufsize) +{ + return errlogInit2(bufsize, DEFAULT_MAX_MSG_SIZE); } void errlogShow(int level) diff --git a/modules/libcom/src/error/errlog.h b/modules/libcom/src/error/errlog.h index 2fe01cf7e..a204bec87 100644 --- a/modules/libcom/src/error/errlog.h +++ b/modules/libcom/src/error/errlog.h @@ -224,15 +224,6 @@ LIBCOM_API int errlogInit(int bufsize); */ LIBCOM_API int errlogInit2(int bufsize, int maxMsgSize); -/** - * errlogBufResize can be used to reinitialize the error logging system with a larger buffer and maximum message size. - * The default buffer size is 1280 bytes, and the default maximum message size is 256. - * - * \param bufsize The desired buffer size - * \param maxMsgSize The desired max message size - */ -LIBCOM_API int errlogBufResize(int bufsize, int maxMsgSize); - /** Wakes up the errlog task and then waits until all messages are flushed from the queue. */ LIBCOM_API void errlogFlush(void); diff --git a/modules/libcom/src/iocsh/libComRegister.c b/modules/libcom/src/iocsh/libComRegister.c index e254387ca..42c9f353a 100644 --- a/modules/libcom/src/iocsh/libComRegister.c +++ b/modules/libcom/src/iocsh/libComRegister.c @@ -291,22 +291,6 @@ static void errlogInit2CallFunc(const iocshArgBuf *args) errlogInit2(args[0].ival, args[1].ival); } -/* errlogBufResize */ -static const iocshArg errlogBufResizeArg0 = { "bufSize",iocshArgInt}; -static const iocshArg errlogBufResizeArg1 = { "maxMsgSize",iocshArgInt}; -static const iocshArg * const errlogBufResizeArgs[] = - {&errlogBufResizeArg0, &errlogBufResizeArg1}; -static const iocshFuncDef errlogBufResizeFuncDef = { - "errlogBufResize", 2, errlogBufResizeArgs, - "Reinitialize error log client buffer size and maximum message size\n" - " bufSize - size of circular buffer (default = 1280 bytes)\n" - " maxMsgSize - maximum size of error message (default = 256 bytes)\n" -}; -static void errlogBufResizeCallFunc(const iocshArgBuf *args) -{ - iocshSetError(errlogBufResize(args[0].ival, args[1].ival)); -} - /* errlogShow */ static const iocshArg errlogShowArg0 = { "level",iocshArgInt}; static const iocshArg * const errlogShowArgs[1] = {&errlogShowArg0}; @@ -536,7 +520,6 @@ void epicsStdCall libComRegister(void) iocshRegister(&eltcFuncDef, eltcCallFunc); iocshRegister(&errlogInitFuncDef,errlogInitCallFunc); iocshRegister(&errlogInit2FuncDef,errlogInit2CallFunc); - iocshRegister(&errlogBufResizeFuncDef,errlogBufResizeCallFunc); iocshRegister(&errlogShowFuncDef,errlogShowCallFunc); iocshRegister(&errlogFuncDef, errlogCallFunc); iocshRegister(&iocLogPrefixFuncDef, iocLogPrefixCallFunc);