2021-01-03 01:49:12 +01:00
|
|
|
#include <FSFWConfig.h>
|
2021-01-02 23:22:50 +01:00
|
|
|
#include "ServiceInterfacePrinter.h"
|
2021-01-03 01:35:17 +01:00
|
|
|
#include "serviceInterfaceDefintions.h"
|
2021-01-03 01:02:07 +01:00
|
|
|
#include "../timemanager/Clock.h"
|
|
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2021-01-08 20:55:36 +01:00
|
|
|
static fsfw::PrintLevel printLevel = fsfw::PrintLevel::DEBUG_LEVEL;
|
2021-01-03 15:15:03 +01:00
|
|
|
#if defined(WIN32) && FSFW_COLORED_OUTPUT == 1
|
2021-01-08 20:55:36 +01:00
|
|
|
static bool consoleInitialized = false;
|
2021-01-06 19:49:58 +01:00
|
|
|
#endif /* defined(WIN32) && FSFW_COLORED_OUTPUT == 1 */
|
2021-01-08 20:55:36 +01:00
|
|
|
static bool addCrAtEnd = false;
|
2021-01-06 19:49:58 +01:00
|
|
|
|
|
|
|
#if FSFW_DISABLE_PRINTOUT == 0
|
|
|
|
uint8_t printBuffer[fsfwconfig::FSFW_PRINT_BUFFER_SIZE];
|
2021-01-03 01:02:07 +01:00
|
|
|
|
|
|
|
void fsfwPrint(fsfw::PrintLevel printType, const char* fmt, va_list arg) {
|
2021-01-03 15:15:03 +01:00
|
|
|
|
|
|
|
#if defined(WIN32) && FSFW_COLORED_OUTPUT == 1
|
|
|
|
if(not consoleInitialized) {
|
|
|
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
DWORD dwMode = 0;
|
|
|
|
GetConsoleMode(hOut, &dwMode);
|
|
|
|
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
SetConsoleMode(hOut, dwMode);
|
|
|
|
}
|
|
|
|
consoleInitialized = true;
|
|
|
|
#endif
|
|
|
|
|
2021-01-03 01:49:12 +01:00
|
|
|
size_t len = 0;
|
2021-01-03 01:47:01 +01:00
|
|
|
char* bufferPosition = reinterpret_cast<char*>(printBuffer);
|
2021-01-03 01:02:07 +01:00
|
|
|
|
|
|
|
/* Check logger level */
|
|
|
|
if(printType == fsfw::PrintLevel::NONE or printType > printLevel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Log message to terminal */
|
|
|
|
|
|
|
|
#if FSFW_COLORED_OUTPUT == 1
|
2021-01-08 20:55:36 +01:00
|
|
|
if(printType == fsfw::PrintLevel::INFO_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition, fsfw::ANSI_COLOR_GREEN);
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
2021-01-08 20:55:36 +01:00
|
|
|
else if(printType == fsfw::PrintLevel::DEBUG_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition, fsfw::ANSI_COLOR_MAGENTA);
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
2021-01-08 20:55:36 +01:00
|
|
|
else if(printType == fsfw::PrintLevel::WARNING_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition, fsfw::ANSI_COLOR_YELLOW);
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
2021-01-08 20:55:36 +01:00
|
|
|
else if(printType == fsfw::PrintLevel::ERROR_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition, fsfw::ANSI_COLOR_RED);
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-08 20:55:36 +01:00
|
|
|
if (printType == fsfw::PrintLevel::INFO_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition + len, "INFO: ");
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
2021-01-08 20:55:36 +01:00
|
|
|
if(printType == fsfw::PrintLevel::DEBUG_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition + len, "DEBUG: ");
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
2021-01-08 20:55:36 +01:00
|
|
|
if(printType == fsfw::PrintLevel::WARNING_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition + len, "WARNING: ");
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 20:55:36 +01:00
|
|
|
if(printType == fsfw::PrintLevel::ERROR_LEVEL) {
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition + len, "ERROR: ");
|
2021-01-03 01:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Clock::TimeOfDay_t now;
|
|
|
|
Clock::getDateAndTime(&now);
|
|
|
|
/*
|
|
|
|
* Log current time to terminal if desired.
|
|
|
|
*/
|
2021-01-03 01:47:01 +01:00
|
|
|
len += sprintf(bufferPosition + len, "| %lu:%02lu:%02lu.%03lu | ",
|
2021-01-03 01:02:07 +01:00
|
|
|
(unsigned long) now.hour,
|
|
|
|
(unsigned long) now.minute,
|
|
|
|
(unsigned long) now.second,
|
|
|
|
(unsigned long) now.usecond /1000);
|
|
|
|
|
2021-01-03 01:47:01 +01:00
|
|
|
len += vsnprintf(bufferPosition + len, sizeof(printBuffer) - len, fmt, arg);
|
2021-01-03 01:02:07 +01:00
|
|
|
|
2021-01-08 02:06:27 +01:00
|
|
|
if(addCrAtEnd) {
|
|
|
|
len += sprintf(bufferPosition + len, "\r");
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:02:07 +01:00
|
|
|
printf("%s", printBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void fsfw::printInfo(const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-01-08 20:55:36 +01:00
|
|
|
fsfwPrint(fsfw::PrintLevel::INFO_LEVEL, fmt, args);
|
2021-01-03 01:02:07 +01:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fsfw::printWarning(const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-01-08 20:55:36 +01:00
|
|
|
fsfwPrint(fsfw::PrintLevel::WARNING_LEVEL, fmt, args);
|
2021-01-03 01:02:07 +01:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fsfw::printDebug(const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-01-08 20:55:36 +01:00
|
|
|
fsfwPrint(fsfw::PrintLevel::DEBUG_LEVEL, fmt, args);
|
2021-01-03 01:02:07 +01:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2021-01-08 02:06:27 +01:00
|
|
|
void fsfw::setToAddCrAtEnd(bool addCrAtEnd_) {
|
|
|
|
addCrAtEnd = addCrAtEnd_;
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:02:07 +01:00
|
|
|
void fsfw::printError(const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-01-08 20:55:36 +01:00
|
|
|
fsfwPrint(fsfw::PrintLevel::ERROR_LEVEL, fmt, args);
|
2021-01-03 01:02:07 +01:00
|
|
|
va_end(args);
|
|
|
|
}
|
2021-01-06 19:49:58 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
void fsfw::printInfo(const char *fmt, ...) {}
|
|
|
|
void fsfw::printWarning(const char *fmt, ...) {}
|
|
|
|
void fsfw::printDebug(const char *fmt, ...) {}
|
|
|
|
void fsfw::printError(const char *fmt, ...) {}
|
|
|
|
|
|
|
|
#endif /* FSFW_DISABLE_PRINTOUT == 0 */
|
|
|
|
|
|
|
|
void fsfw::setPrintLevel(PrintLevel printLevel_) {
|
|
|
|
printLevel = printLevel_;
|
|
|
|
}
|
|
|
|
|
|
|
|
fsfw::PrintLevel fsfw::getPrintLevel() {
|
|
|
|
return printLevel;
|
|
|
|
}
|