From f7321942e9bcbc16c2b8a5f484a353068bd4c0e6 Mon Sep 17 00:00:00 2001 From: rusefi Date: Sun, 3 May 2020 11:49:06 -0400 Subject: [PATCH] class constructors are a great way to have simple initialization sequence --- firmware/rusefi.cpp | 1 - firmware/util/datalogging.cpp | 47 ++++++++++---------- firmware/util/datalogging.h | 2 - firmware/util/loggingcentral.cpp | 46 +++++++++++-------- firmware/util/loggingcentral.h | 1 - simulator/simulator/rusEfiFunctionalTest.cpp | 2 - 6 files changed, 51 insertions(+), 48 deletions(-) diff --git a/firmware/rusefi.cpp b/firmware/rusefi.cpp index 08837224ed..53b3e65a4b 100644 --- a/firmware/rusefi.cpp +++ b/firmware/rusefi.cpp @@ -161,7 +161,6 @@ void runRusEfi(void) { efiAssertVoid(CUSTOM_RM_STACK_1, getCurrentRemainingStack() > 512, "init s"); assertEngineReference(); engine->setConfig(config); - initIntermediateLoggingBuffer(); addConsoleAction(CMD_REBOOT, scheduleReboot); addConsoleAction(CMD_REBOOT_DFU, jump_to_bootloader); diff --git a/firmware/util/datalogging.cpp b/firmware/util/datalogging.cpp index 4d778122e3..8e942bfff8 100644 --- a/firmware/util/datalogging.cpp +++ b/firmware/util/datalogging.cpp @@ -39,10 +39,29 @@ #include "console_io.h" #include "os_util.h" -static MemoryStream intermediateLoggingBuffer; static uint8_t intermediateLoggingBufferData[INTERMEDIATE_LOGGING_BUFFER_SIZE] CCM_OPTIONAL; -//todo define max-printf-buffer -static bool intermediateLoggingBufferInited = false; + +class IntermediateLogging { +public: + /** + * Class constructors are a great way to have simple initialization sequence + */ + IntermediateLogging() { + msObjectInit(&intermediateLoggingBuffer, intermediateLoggingBufferData, INTERMEDIATE_LOGGING_BUFFER_SIZE, 0); + } + MemoryStream intermediateLoggingBuffer; + + // todo: look into chsnprintf once on Chibios 3 + void vappendPrintfI(Logging *logging, const char *fmt, va_list arg) { + intermediateLoggingBuffer.eos = 0; // reset + efiAssertVoid(CUSTOM_ERR_6603, getCurrentRemainingStack() > 128, "lowstck#1b"); + chvprintf((BaseSequentialStream *) &intermediateLoggingBuffer, fmt, arg); + intermediateLoggingBuffer.buffer[intermediateLoggingBuffer.eos] = 0; // need to terminate explicitly + logging->append((char *)intermediateLoggingBuffer.buffer); + } +}; + +static IntermediateLogging intermediateLogging; /** * @returns true if data does not fit into this buffer @@ -93,26 +112,13 @@ void appendFast(Logging *logging, const char *text) { logging->linePointer = s - 1; } -// todo: look into chsnprintf once on Chibios 3 -static void vappendPrintfI(Logging *logging, const char *fmt, va_list arg) { - if (!intermediateLoggingBufferInited) { - firmwareError(CUSTOM_ERR_BUFF_INIT_ERROR, "intermediateLoggingBufferInited not inited!"); - return; - } - intermediateLoggingBuffer.eos = 0; // reset - efiAssertVoid(CUSTOM_ERR_6603, getCurrentRemainingStack() > 128, "lowstck#1b"); - chvprintf((BaseSequentialStream *) &intermediateLoggingBuffer, fmt, arg); - intermediateLoggingBuffer.buffer[intermediateLoggingBuffer.eos] = 0; // need to terminate explicitly - logging->append((char *)intermediateLoggingBuffer.buffer); -} - /** * this method acquires system lock to guard the shared intermediateLoggingBuffer memory stream */ void Logging::vappendPrintf(const char *fmt, va_list arg) { efiAssertVoid(CUSTOM_ERR_6604, getCurrentRemainingStack() > 128, "lowstck#5b"); int wasLocked = lockAnyContext(); - vappendPrintfI(this, fmt, arg); + intermediateLogging.vappendPrintfI(this, fmt, arg); if (!wasLocked) { unlockAnyContext(); } @@ -273,13 +279,6 @@ uint32_t remainingSize(Logging *logging) { return logging->bufferSize - loggingSize(logging); } -void initIntermediateLoggingBuffer(void) { - initLoggingCentral(); - - msObjectInit(&intermediateLoggingBuffer, intermediateLoggingBufferData, INTERMEDIATE_LOGGING_BUFFER_SIZE, 0); - intermediateLoggingBufferInited = true; -} - #else /* unit test implementations */ void Logging::vappendPrintf(const char *fmt, va_list arg) { diff --git a/firmware/util/datalogging.h b/firmware/util/datalogging.h index feb691ea37..ac200fe449 100644 --- a/firmware/util/datalogging.h +++ b/firmware/util/datalogging.h @@ -47,8 +47,6 @@ public: char DEFAULT_BUFFER[200]; }; -void initIntermediateLoggingBuffer(void); - int isInitialized(Logging *logging); void initLoggingExt(Logging *logging, const char *name, char *buffer, int bufferSize); diff --git a/firmware/util/loggingcentral.cpp b/firmware/util/loggingcentral.cpp index a91b547886..30cbef7cfa 100644 --- a/firmware/util/loggingcentral.cpp +++ b/firmware/util/loggingcentral.cpp @@ -30,16 +30,34 @@ static char *accumulationBuffer; static log_buf_t pendingBuffers0; static log_buf_t pendingBuffers1; -/** - * amount of data accumulated so far - */ -static uint32_t accumulatedSize; - /** * We copy all the pending data into this buffer once we are ready to push it out */ static char * outputBuffer; + +class LoggingCentral { +public: + /** + * Class constructors are a great way to have simple initialization sequence + */ + LoggingCentral() { + pendingBuffers0[0] = 0; + pendingBuffers1[0] = 0; + accumulationBuffer = pendingBuffers0; + outputBuffer = pendingBuffers1; + accumulatedSize = 0; + } + + /** + * amount of data accumulated so far + */ + uint32_t accumulatedSize; +}; + +static LoggingCentral loggingCentral; + + /** * This method appends the content of specified thread-local logger into the global buffer * of logging content. @@ -54,7 +72,7 @@ void scheduleLogging(Logging *logging) { int newLength = efiStrlen(logging->buffer); bool alreadyLocked = lockOutputBuffer(); - if (accumulatedSize + newLength >= MAX_DL_CAPACITY) { + if (loggingCentral.accumulatedSize + newLength >= MAX_DL_CAPACITY) { /** * if no one is consuming the data we have to drop it * this happens in case of serial-over-USB, todo: find a better solution? @@ -66,8 +84,8 @@ void scheduleLogging(Logging *logging) { return; } // memcpy is faster then strcpy because it is not looking for line terminator - memcpy(accumulationBuffer + accumulatedSize, logging->buffer, newLength + 1); - accumulatedSize += newLength; + memcpy(accumulationBuffer + loggingCentral.accumulatedSize, logging->buffer, newLength + 1); + loggingCentral.accumulatedSize += newLength; if (!alreadyLocked) { unlockOutputBuffer(); } @@ -93,12 +111,12 @@ char * swapOutputBuffers(int *actualOutputBufferSize) { char *temp = outputBuffer; #if EFI_ENABLE_ASSERTS - expectedOutputSize = accumulatedSize; + expectedOutputSize = loggingCentral.accumulatedSize; #endif /* EFI_ENABLE_ASSERTS */ outputBuffer = accumulationBuffer; accumulationBuffer = temp; - accumulatedSize = 0; + loggingCentral.accumulatedSize = 0; accumulationBuffer[0] = 0; if (!alreadyLocked) { @@ -119,14 +137,6 @@ char * swapOutputBuffers(int *actualOutputBufferSize) { return outputBuffer; } -void initLoggingCentral(void) { - pendingBuffers0[0] = 0; - pendingBuffers1[0] = 0; - accumulationBuffer = pendingBuffers0; - outputBuffer = pendingBuffers1; - accumulatedSize = 0; -} - /** * rusEfi business logic invokes this method in order to eventually print stuff to rusEfi console * diff --git a/firmware/util/loggingcentral.h b/firmware/util/loggingcentral.h index 013677f79c..79a27c8b12 100644 --- a/firmware/util/loggingcentral.h +++ b/firmware/util/loggingcentral.h @@ -8,6 +8,5 @@ class Logging; -void initLoggingCentral(void); char * swapOutputBuffers(int *actualOutputBufferSize); void scheduleMsg(Logging *logging, const char *fmt, ...); diff --git a/simulator/simulator/rusEfiFunctionalTest.cpp b/simulator/simulator/rusEfiFunctionalTest.cpp index 0d2e9254fe..71681347a8 100644 --- a/simulator/simulator/rusEfiFunctionalTest.cpp +++ b/simulator/simulator/rusEfiFunctionalTest.cpp @@ -103,8 +103,6 @@ void rusEfiFunctionalTest(void) { initTriggerDecoderLogger(&sharedLogger); #endif /* EFI_SHAFT_POSITION_INPUT */ - initIntermediateLoggingBuffer(); - engine->setConfig(config); initializeConsole(&sharedLogger);