1
0
forked from fsfw/fsfw

more improvements:

1. New optional flag to redirect print to stderr. THis can be useful on
host environemtns (e.g linux)
2. non-buffered mode if this flag is true: the preamble msut be printed
manually
2. Getter function for preamble for that case.
3. printChar function: specify whether to print to stderr or stdout
This commit is contained in:
2020-06-03 23:14:17 +02:00
parent e5cea3ead0
commit 7259a13569
4 changed files with 131 additions and 62 deletions

View File

@ -3,43 +3,64 @@
#include <iostream>
#include <sstream>
#include <cstdio>
#include <iomanip>
#ifndef UT699
/**
* @brief This is the underlying stream buffer which implements the
* streambuf class and overloads the overflow() and sync() methods
* @details
* This class is used to modify the output of the stream, for example by adding.
* It also calls the char printing function which is implemented in the
* board supply package (BSP).
*/
class ServiceInterfaceBuffer:
public std::basic_streambuf<char,std::char_traits<char>> {
public std::streambuf {
friend class ServiceInterfaceStream;
public:
ServiceInterfaceBuffer(std::string set_message, uint16_t port,
bool addCrToPreamble);
ServiceInterfaceBuffer(std::string setMessage, bool errStream,
bool addCrToPreamble, uint16_t port);
protected:
bool isActive;
// This is called when buffer becomes full. If
// buffer is not used, then this is called every
// time when characters are put to stream.
//! This is called when buffer becomes full. If
//! buffer is not used, then this is called every
//! time when characters are put to stream.
int overflow(int c = Traits::eof()) override;
// This function is called when stream is flushed,
// for example when std::endl is put to stream.
//! This function is called when stream is flushed,
//! for example when std::endl is put to stream.
int sync(void) override;
private:
// For additional message information
std::string log_message;
//! For additional message information
std::string logMessage;
// For EOF detection
typedef std::char_traits<char> Traits;
// This is useful for some terminal programs which do not have
// implicit carriage return with newline characters.
//! This is useful for some terminal programs which do not have
//! implicit carriage return with newline characters.
bool addCrToPreamble;
//! This specifies to print to stderr and work in unbuffered mode.
bool errStream;
// Work in buffer mode. It is also possible to work without buffer.
static size_t const BUF_SIZE = 128;
char buf[BUF_SIZE];
// In this function, the characters are parsed.
//! In this function, the characters are parsed.
void putChars(char const* begin, char const* end);
std::string getPreamble();
/**
* This helper function returns the zero padded string version of a number.
* The type is deduced automatically.
* @tparam T
* @param num
* @param width
* @return
*/
template<typename T>
std::string zero_padded(const T& num, uint8_t width) {
std::ostringstream string_to_pad;
@ -52,6 +73,7 @@ private:
return result;
}
};
#endif