added colored output for streams as well

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

View File

@ -1,5 +1,7 @@
#include "../timemanager/Clock.h"
#include "ServiceInterfaceBuffer.h"
#include "serviceInterfaceDefintions.h"
#include <FSFWConfig.h>
#include <cstring>
#include <inttypes.h>
@ -17,6 +19,21 @@ ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
// Set pointers if the stream is buffered.
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.resize(MAX_PREAMBLE_SIZE);
}
@ -102,6 +119,12 @@ std::string* ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
currentSize += 1;
parsePosition += 1;
}
#if FSFW_COLORED_OUTPUT == 1
currentSize += sprintf(parsePosition, "%s", colorPrefix.c_str());
parsePosition += colorPrefix.size();
#endif
int32_t charCount = sprintf(parsePosition,
"%s: | %02" SCNu32 ":%02" SCNu32 ":%02" SCNu32 ".%03" SCNu32 " | ",
this->logMessage.c_str(), loggerTime.hour,

View File

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

View File

@ -1,8 +1,10 @@
#include "ServiceInterfacePrinter.h"
#include "serviceInterfaceDefintions.h"
#include "../timemanager/Clock.h"
#include <FSFWConfig.h>
#include <cstdarg>
#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);
}
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) {
len += sprintf((char *)printBuffer, fsfw::ANSI_COLOR_YELLOW);

View File

@ -11,15 +11,6 @@ enum class PrintLevel {
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);
PrintLevel getPrintLevel();

View File

@ -13,20 +13,3 @@ std::string* ServiceInterfaceStream::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();
/**
* 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:
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_ */