minor improvements and docuemntation

This commit is contained in:
Robin Müller 2021-01-03 01:47:01 +01:00
parent e300207f48
commit 717027792e
2 changed files with 18 additions and 12 deletions

View File

@ -14,6 +14,7 @@ uint8_t printBuffer[fsfwconfig::FSFW_PRINT_BUFFER_SIZE];
void fsfwPrint(fsfw::PrintLevel printType, const char* fmt, va_list arg) { void fsfwPrint(fsfw::PrintLevel printType, const char* fmt, va_list arg) {
uint32_t len = 0; uint32_t len = 0;
char* bufferPosition = reinterpret_cast<char*>(printBuffer);
/* Check logger level */ /* Check logger level */
if(printType == fsfw::PrintLevel::NONE or printType > printLevel) { if(printType == fsfw::PrintLevel::NONE or printType > printLevel) {
@ -24,31 +25,31 @@ void fsfwPrint(fsfw::PrintLevel printType, const char* fmt, va_list arg) {
#if FSFW_COLORED_OUTPUT == 1 #if FSFW_COLORED_OUTPUT == 1
if(printType == fsfw::PrintLevel::INFO) { if(printType == fsfw::PrintLevel::INFO) {
len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_GREEN); len += sprintf(bufferPosition, fsfw::ANSI_COLOR_GREEN);
} }
else if(printType == fsfw::PrintLevel::DEBUG) { else if(printType == fsfw::PrintLevel::DEBUG) {
len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_MAGENTA); len += sprintf(bufferPosition, fsfw::ANSI_COLOR_MAGENTA);
} }
else if(printType == fsfw::PrintLevel::WARNING) { else if(printType == fsfw::PrintLevel::WARNING) {
len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_YELLOW); len += sprintf(bufferPosition, fsfw::ANSI_COLOR_YELLOW);
} }
else if(printType == fsfw::PrintLevel::ERROR_TYPE) { else if(printType == fsfw::PrintLevel::ERROR_TYPE) {
len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_RED); len += sprintf(bufferPosition, fsfw::ANSI_COLOR_RED);
} }
#endif #endif
if (printType == fsfw::PrintLevel::INFO) { if (printType == fsfw::PrintLevel::INFO) {
len += sprintf((char *)printBuffer + len, "INFO: "); len += sprintf(bufferPosition + len, "INFO: ");
} }
if(printType == fsfw::PrintLevel::DEBUG) { if(printType == fsfw::PrintLevel::DEBUG) {
len += sprintf((char *)printBuffer + len, "DEBUG: "); len += sprintf(bufferPosition + len, "DEBUG: ");
} }
if(printType == fsfw::PrintLevel::WARNING) { if(printType == fsfw::PrintLevel::WARNING) {
len += sprintf((char *)printBuffer + len, "WARNING: "); len += sprintf(bufferPosition + len, "WARNING: ");
} }
if(printType == fsfw::PrintLevel::ERROR_TYPE) { if(printType == fsfw::PrintLevel::ERROR_TYPE) {
len += sprintf((char *)printBuffer + len, "ERROR: "); len += sprintf(bufferPosition + len, "ERROR: ");
} }
Clock::TimeOfDay_t now; Clock::TimeOfDay_t now;
@ -56,14 +57,13 @@ void fsfwPrint(fsfw::PrintLevel printType, const char* fmt, va_list arg) {
/* /*
* Log current time to terminal if desired. * Log current time to terminal if desired.
*/ */
len += sprintf((char*)printBuffer + len, "| %lu:%02lu:%02lu.%03lu | ", len += sprintf(bufferPosition + len, "| %lu:%02lu:%02lu.%03lu | ",
(unsigned long) now.hour, (unsigned long) now.hour,
(unsigned long) now.minute, (unsigned long) now.minute,
(unsigned long) now.second, (unsigned long) now.second,
(unsigned long) now.usecond /1000); (unsigned long) now.usecond /1000);
len += vsnprintf((char *)(printBuffer + len), len += vsnprintf(bufferPosition + len, sizeof(printBuffer) - len, fmt, arg);
sizeof(printBuffer)-len, fmt, arg);
printf("%s", printBuffer); printf("%s", printBuffer);
} }

View File

@ -14,7 +14,13 @@ enum class PrintLevel {
void setPrintLevel(PrintLevel printLevel); void setPrintLevel(PrintLevel printLevel);
PrintLevel getPrintLevel(); PrintLevel getPrintLevel();
/**
* These functions can be used like the C stdio printf and forward the
* supplied formatted string arguments to a printf function.
* They prepend the string with a color (if enabled), a log preamble and
* a timestamp.
* @param fmt Formatted string
*/
void printInfo(const char *fmt, ...); void printInfo(const char *fmt, ...);
void printWarning(const char* fmt, ...); void printWarning(const char* fmt, ...);
void printDebug(const char* fmt, ...); void printDebug(const char* fmt, ...);