added colored output for streams as well

This commit is contained in:
Robin Müller 2021-01-03 01:35:17 +01:00
parent b30405fee7
commit 0e2875b22d
8 changed files with 52 additions and 38 deletions

View File

@ -9,25 +9,26 @@
//! the C stdio functions can be used alternatively //! the C stdio functions can be used alternatively
#define FSFW_CPP_OSTREAM_ENABLED 1 #define FSFW_CPP_OSTREAM_ENABLED 1
//! Reduced printout to further decrease code size //! More FSFW related printouts.
//! Be careful, this also turns off most diagnostic prinouts! //! Be careful, this also turns off most diagnostic prinouts!
#define FSFW_ENHANCED_PRINTOUT 0 #define FSFW_ENHANCED_PRINTOUT 0
//! Can be used to completely disable printouts, even the C stdio ones. //! Can be used to completely disable printouts, even the C stdio ones.
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 0 && FSFW_ENHANCED_PRINTOUT == 0
#define FSFW_DISABLE_PRINTOUT 0 #define FSFW_DISABLE_PRINTOUT 0
#endif #endif
//! Can be used to enable additional debugging printouts for developing the FSFW //! Can be used to enable additional debugging printouts for developing the FSFW
#define FSFW_PRINT_VERBOSITY_LEVEL 0 #define FSFW_PRINT_VERBOSITY_LEVEL 0
//! Can be used to disable the ANSI color sequences for C stdio.
#define FSFW_COLORED_OUTPUT 1
//! If FSFW_OBJ_EVENT_TRANSLATION is set to one, //! If FSFW_OBJ_EVENT_TRANSLATION is set to one,
//! additional output which requires the translation files translateObjects //! additional output which requires the translation files translateObjects
//! and translateEvents (and their compiled source files) //! and translateEvents (and their compiled source files)
#define FSFW_OBJ_EVENT_TRANSLATION 0 #define FSFW_OBJ_EVENT_TRANSLATION 0
#define FSFW_COLORED_OUTPUT 1
#if FSFW_OBJ_EVENT_TRANSLATION == 1 #if FSFW_OBJ_EVENT_TRANSLATION == 1
//! Specify whether info events are printed too. //! Specify whether info events are printed too.
#define FSFW_DEBUG_INFO 1 #define FSFW_DEBUG_INFO 1

View File

@ -1,5 +1,7 @@
#include "../timemanager/Clock.h" #include "../timemanager/Clock.h"
#include "ServiceInterfaceBuffer.h" #include "ServiceInterfaceBuffer.h"
#include "serviceInterfaceDefintions.h"
#include <FSFWConfig.h>
#include <cstring> #include <cstring>
#include <inttypes.h> #include <inttypes.h>
@ -17,6 +19,21 @@ ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
// Set pointers if the stream is buffered. // Set pointers if the stream is buffered.
setp( buf, buf + BUF_SIZE ); setp( buf, buf + BUF_SIZE );
} }
#if FSFW_COLORED_OUTPUT == 1
if(setMessage.find("DEBUG")) {
colorPrefix = fsfw::ANSI_COLOR_MAGENTA;
}
else if(setMessage.find("INFO")) {
colorPrefix = fsfw::ANSI_COLOR_GREEN;
}
else if(setMessage.find("WARNING")) {
colorPrefix = fsfw::ANSI_COLOR_YELLOW;
}
else if(setMessage.find("ERROR")) {
colorPrefix = fsfw::ANSI_COLOR_RED;
}
#endif
preamble.reserve(MAX_PREAMBLE_SIZE); preamble.reserve(MAX_PREAMBLE_SIZE);
preamble.resize(MAX_PREAMBLE_SIZE); preamble.resize(MAX_PREAMBLE_SIZE);
} }
@ -102,6 +119,12 @@ std::string* ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
currentSize += 1; currentSize += 1;
parsePosition += 1; parsePosition += 1;
} }
#if FSFW_COLORED_OUTPUT == 1
currentSize += sprintf(parsePosition, "%s", colorPrefix.c_str());
parsePosition += colorPrefix.size();
#endif
int32_t charCount = sprintf(parsePosition, int32_t charCount = sprintf(parsePosition,
"%s: | %02" SCNu32 ":%02" SCNu32 ":%02" SCNu32 ".%03" SCNu32 " | ", "%s: | %02" SCNu32 ":%02" SCNu32 ":%02" SCNu32 ".%03" SCNu32 " | ",
this->logMessage.c_str(), loggerTime.hour, this->logMessage.c_str(), loggerTime.hour,

View File

@ -42,6 +42,11 @@ private:
//! For additional message information //! For additional message information
std::string logMessage; std::string logMessage;
std::string preamble; std::string preamble;
#if FSFW_COLORED_OUTPUT == 1
std::string colorPrefix;
#endif
// For EOF detection // For EOF detection
typedef std::char_traits<char> Traits; typedef std::char_traits<char> Traits;

View File

@ -1,8 +1,10 @@
#include "ServiceInterfacePrinter.h" #include "ServiceInterfacePrinter.h"
#include "serviceInterfaceDefintions.h"
#include "../timemanager/Clock.h" #include "../timemanager/Clock.h"
#include <FSFWConfig.h> #include <FSFWConfig.h>
#include <cstdarg> #include <cstdarg>
#include <cstdint> #include <cstdint>
@ -25,7 +27,7 @@ void fsfwPrint(fsfw::PrintLevel printType, const char* fmt, va_list arg) {
len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_GREEN); len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_GREEN);
} }
else if(printType == fsfw::PrintLevel::DEBUG) { else if(printType == fsfw::PrintLevel::DEBUG) {
len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_BLUE); len += sprintf((char *)printBuffer, 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((char *)printBuffer, fsfw::ANSI_COLOR_YELLOW);

View File

@ -11,15 +11,6 @@ enum class PrintLevel {
DEBUG = 4 DEBUG = 4
}; };
static const char* const ANSI_COLOR_RED = "\x1b[31m";
static const char* const ANSI_COLOR_GREEN = "\x1b[32m";
static const char* const ANSI_COLOR_YELLOW = "\x1b[33m";
static const char* const ANSI_COLOR_BLUE = "\x1b[34m";
static const char* const ANSI_COLOR_MAGENTA = "\x1b[35m";
static const char* const ANSI_COLOR_CYAN = "\x1b[36m";
static const char* const ANSI_COLOR_RESET = "\x1b[0m";
void setPrintLevel(PrintLevel printLevel); void setPrintLevel(PrintLevel printLevel);
PrintLevel getPrintLevel(); PrintLevel getPrintLevel();

View File

@ -13,20 +13,3 @@ std::string* ServiceInterfaceStream::getPreamble() {
return streambuf.getPreamble(); return streambuf.getPreamble();
} }
void ServiceInterfaceStream::print(std::string error,
bool withPreamble, bool withNewline, bool flush) {
if(not streambuf.isBuffered() and withPreamble) {
*this << getPreamble() << error;
}
else {
*this << error;
}
if(withNewline) {
*this << "\n";
}
// if mode is non-buffered, no need to flush.
if(flush and streambuf.isBuffered()) {
this->flush();
}
}

View File

@ -35,13 +35,6 @@ public:
*/ */
std::string* getPreamble(); std::string* getPreamble();
/**
* This prints an error with a preamble. Useful if using the unbuffered
* mode. Flushes in default mode (prints immediately).
*/
void print(std::string error, bool withPreamble = true,
bool withNewline = true, bool flush = true);
protected: protected:
ServiceInterfaceBuffer streambuf; ServiceInterfaceBuffer streambuf;
}; };

View File

@ -0,0 +1,16 @@
#ifndef FSFW_SERVICEINTERFACE_SERVICEINTERFACEDEFINTIONS_H_
#define FSFW_SERVICEINTERFACE_SERVICEINTERFACEDEFINTIONS_H_
namespace fsfw {
static const char* const ANSI_COLOR_RED = "\x1b[31m";
static const char* const ANSI_COLOR_GREEN = "\x1b[32m";
static const char* const ANSI_COLOR_YELLOW = "\x1b[33m";
static const char* const ANSI_COLOR_BLUE = "\x1b[34m";
static const char* const ANSI_COLOR_MAGENTA = "\x1b[35m";
static const char* const ANSI_COLOR_CYAN = "\x1b[36m";
static const char* const ANSI_COLOR_RESET = "\x1b[0m";
}
#endif /* FSFW_SERVICEINTERFACE_SERVICEINTERFACEDEFINTIONS_H_ */