diff --git a/modules/libcom/src/error/errlog.c b/modules/libcom/src/error/errlog.c index c4362f763..7dbfe519d 100644 --- a/modules/libcom/src/error/errlog.c +++ b/modules/libcom/src/error/errlog.c @@ -615,28 +615,44 @@ int errlogInit(int bufsize) return errlogInit2(bufsize, DEFAULT_MAX_MSG_SIZE); } -void errlogBufResize(int bufsize, int maxMsgSize) +int errlogBufResize(int bufsize, int maxMsgSize) { + char* logbuf = NULL; + char* printbuf = NULL; + + 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; + } + 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(); + logbuf = calloc(1, bufsize); + printbuf = calloc(1, bufsize); - epicsMutexMustLock(pvt.bufSizeLock); epicsMutexMustLock(pvt.msgQueueLock); + memcpy(logbuf, pvt.log->base, pvt.bufSize); + free(pvt.log->base); + pvt.log->base = logbuf; + + memcpy(printbuf, &pvt.print->base, pvt.bufSize); + free(pvt.print->base); + pvt.print->base = printbuf; + 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); } diff --git a/modules/libcom/src/error/errlog.h b/modules/libcom/src/error/errlog.h index a204bec87..2fe01cf7e 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 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 cbf262608..30f5917f5 100644 --- a/modules/libcom/src/iocsh/libComRegister.c +++ b/modules/libcom/src/iocsh/libComRegister.c @@ -304,7 +304,7 @@ static const iocshFuncDef errlogBufResizeFuncDef = { }; static void errlogBufResizeCallFunc(const iocshArgBuf *args) { - errlogBufResize(args[0].ival, args[1].ival); + iocshSetError(errlogBufResize(args[0].ival, args[1].ival)); } /* errlogShow */