class constructors are a great way to have simple initialization sequence

This commit is contained in:
rusefi 2020-05-03 11:49:06 -04:00
parent cfd8889236
commit f7321942e9
6 changed files with 51 additions and 48 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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
*

View File

@ -8,6 +8,5 @@
class Logging;
void initLoggingCentral(void);
char * swapOutputBuffers(int *actualOutputBufferSize);
void scheduleMsg(Logging *logging, const char *fmt, ...);

View File

@ -103,8 +103,6 @@ void rusEfiFunctionalTest(void) {
initTriggerDecoderLogger(&sharedLogger);
#endif /* EFI_SHAFT_POSITION_INPUT */
initIntermediateLoggingBuffer();
engine->setConfig(config);
initializeConsole(&sharedLogger);