From a03d1fbe45f04a5fa168f40a457ebd1aa3e551f9 Mon Sep 17 00:00:00 2001 From: Simon Rose Date: Tue, 24 Feb 2026 13:53:14 +0000 Subject: [PATCH] Add commandline utility to resize buffer after being initialised --- modules/libcom/src/error/errlog.c | 29 +++++++++++++++++++++++ modules/libcom/src/error/errlog.h | 9 +++++++ modules/libcom/src/iocsh/libComRegister.c | 17 +++++++++++++ 3 files changed, 55 insertions(+) diff --git a/modules/libcom/src/error/errlog.c b/modules/libcom/src/error/errlog.c index 3aff8e2ec..b5678fcba 100644 --- a/modules/libcom/src/error/errlog.c +++ b/modules/libcom/src/error/errlog.c @@ -77,6 +77,7 @@ typedef struct { } buffer_t; static struct { + epicsMutexId bufSizeLock; /* const after errlogInit() */ size_t maxMsgSize; /* alloc size of both buffer_t::base */ @@ -548,6 +549,7 @@ static void errlogInitPvt(void *arg) */ pvt.errlogInitFailed = TRUE; + pvt.bufSizeLock = epicsMutexCreate(); pvt.bufSize = pconfig->bufsize; pvt.maxMsgSize = pconfig->maxMsgSize; ellInit(&pvt.listenerList); @@ -566,6 +568,7 @@ static void errlogInitPvt(void *arg) errSymBld(); /* Better not to do this lazily... */ if(pvt.waitForWork + && pvt.bufSizeLock && pvt.listenerLock && pvt.msgQueueLock && pvt.waitForSeq @@ -612,6 +615,32 @@ int errlogInit(int bufsize) return errlogInit2(bufsize, DEFAULT_MAX_MSG_SIZE); } +void errlogBufResize(int bufsize, int maxMsgSize) +{ + if (bufsize < MIN_BUFFER_SIZE) + bufsize = MIN_BUFFER_SIZE; + if (maxMsgSize < MIN_MESSAGE_SIZE) + maxMsgSize = MIN_MESSAGE_SIZE; + else if (maxMsgSize > MAX_MESSAGE_SIZE) + maxMsgSize = MAX_MESSAGE_SIZE; + + errlogFlush(); + + epicsMutexMustLock(pvt.bufSizeLock); + epicsMutexMustLock(pvt.msgQueueLock); + + pvt.bufSize = bufsize; + pvt.maxMsgSize = maxMsgSize; + + free(pvt.log->base); + pvt.log->base = callocMustSucceed(1, pvt.bufSize, "Failed to allocate memory for errorlog buffer"); + free(pvt.print->base); + pvt.print->base = callocMustSucceed(1, pvt.bufSize, "Failed to allocate memory for errorlog buffer"); + + epicsMutexUnlock(pvt.msgQueueLock); + epicsMutexUnlock(pvt.bufSizeLock); +} + void errlogFlush(void) { /* wait for both buffers to be handled to know that all currently diff --git a/modules/libcom/src/error/errlog.h b/modules/libcom/src/error/errlog.h index a204bec87..aea8437da 100644 --- a/modules/libcom/src/error/errlog.h +++ b/modules/libcom/src/error/errlog.h @@ -224,6 +224,15 @@ 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 void 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 709452473..844ee14e2 100644 --- a/modules/libcom/src/iocsh/libComRegister.c +++ b/modules/libcom/src/iocsh/libComRegister.c @@ -290,6 +290,22 @@ 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) +{ + errlogBufResize(args[0].ival, args[1].ival); +} + /* errlog */ IOCSH_STATIC_FUNC void errlog(const char *message) { @@ -500,6 +516,7 @@ void epicsStdCall libComRegister(void) iocshRegister(&eltcFuncDef, eltcCallFunc); iocshRegister(&errlogInitFuncDef,errlogInitCallFunc); iocshRegister(&errlogInit2FuncDef,errlogInit2CallFunc); + iocshRegister(&errlogBufResizeFuncDef,errlogBufResizeCallFunc); iocshRegister(&errlogFuncDef, errlogCallFunc); iocshRegister(&iocLogPrefixFuncDef, iocLogPrefixCallFunc);