Merge pull request 'service interface stream enhancements' (#93) from KSat/fsfw:mueller_ServiceStreamEnhancement into master
This commit is contained in:
commit
e1c17409d9
@ -1,11 +1,58 @@
|
|||||||
#include <framework/timemanager/Clock.h>
|
#include <framework/timemanager/Clock.h>
|
||||||
#include <framework/serviceinterface/ServiceInterfaceBuffer.h>
|
#include <framework/serviceinterface/ServiceInterfaceBuffer.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
// to be implemented by bsp
|
// to be implemented by bsp
|
||||||
extern "C" void printChar(const char*);
|
extern "C" void printChar(const char*, bool errStream);
|
||||||
|
|
||||||
|
#ifndef UT699
|
||||||
|
|
||||||
|
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
|
||||||
|
bool addCrToPreamble, bool buffered , bool errStream, uint16_t port):
|
||||||
|
isActive(true), logMessage(setMessage),
|
||||||
|
addCrToPreamble(addCrToPreamble), buffered(buffered),
|
||||||
|
errStream(errStream) {
|
||||||
|
if(buffered) {
|
||||||
|
// Set pointers if the stream is buffered.
|
||||||
|
setp( buf, buf + BUF_SIZE );
|
||||||
|
}
|
||||||
|
preamble.reserve(MAX_PREAMBLE_SIZE);
|
||||||
|
preamble.resize(MAX_PREAMBLE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
||||||
|
char array[BUF_SIZE];
|
||||||
|
uint32_t length = end - begin;
|
||||||
|
if (length > sizeof(array)) {
|
||||||
|
length = sizeof(array);
|
||||||
|
}
|
||||||
|
memcpy(array, begin, length);
|
||||||
|
|
||||||
|
for(; begin != end; begin++){
|
||||||
|
if(errStream) {
|
||||||
|
printChar(begin, true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printChar(begin, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
int ServiceInterfaceBuffer::overflow(int c) {
|
int ServiceInterfaceBuffer::overflow(int c) {
|
||||||
|
if(not buffered and this->isActive) {
|
||||||
|
if (c != Traits::eof()) {
|
||||||
|
if(errStream) {
|
||||||
|
printChar(reinterpret_cast<const char*>(&c), true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printChar(reinterpret_cast<const char*>(&c), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
// Handle output
|
// Handle output
|
||||||
putChars(pbase(), pptr());
|
putChars(pbase(), pptr());
|
||||||
if (c != Traits::eof()) {
|
if (c != Traits::eof()) {
|
||||||
@ -20,52 +67,70 @@ int ServiceInterfaceBuffer::overflow(int c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ServiceInterfaceBuffer::sync(void) {
|
int ServiceInterfaceBuffer::sync(void) {
|
||||||
if (this->isActive) {
|
if(not this->isActive and not buffered) {
|
||||||
Clock::TimeOfDay_t loggerTime;
|
if(not buffered) {
|
||||||
Clock::getDateAndTime(&loggerTime);
|
setp(buf, buf + BUF_SIZE - 1);
|
||||||
char preamble[96] = { 0 };
|
}
|
||||||
sprintf(preamble, "%s: | %lu:%02lu:%02lu.%03lu | ",
|
return 0;
|
||||||
this->log_message.c_str(), (unsigned long) loggerTime.hour,
|
|
||||||
(unsigned long) loggerTime.minute,
|
|
||||||
(unsigned long) loggerTime.second,
|
|
||||||
(unsigned long) loggerTime.usecond /1000);
|
|
||||||
// Write log_message and time
|
|
||||||
this->putChars(preamble, preamble + sizeof(preamble));
|
|
||||||
// Handle output
|
|
||||||
this->putChars(pbase(), pptr());
|
|
||||||
}
|
}
|
||||||
|
if(not buffered) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t preambleSize = 0;
|
||||||
|
auto preamble = getPreamble(&preambleSize);
|
||||||
|
// Write logMessage and time
|
||||||
|
this->putChars(preamble.data(), preamble.data() + preambleSize);
|
||||||
|
// Handle output
|
||||||
|
this->putChars(pbase(), pptr());
|
||||||
// This tells that buffer is empty again
|
// This tells that buffer is empty again
|
||||||
setp(buf, buf + BUF_SIZE - 1);
|
setp(buf, buf + BUF_SIZE - 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef UT699
|
bool ServiceInterfaceBuffer::isBuffered() const {
|
||||||
|
return buffered;
|
||||||
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, uint16_t port) {
|
|
||||||
this->log_message = set_message;
|
|
||||||
this->isActive = true;
|
|
||||||
setp( buf, buf + BUF_SIZE );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
|
||||||
char array[BUF_SIZE];
|
Clock::TimeOfDay_t loggerTime;
|
||||||
uint32_t length = end - begin;
|
Clock::getDateAndTime(&loggerTime);
|
||||||
if (length > sizeof(array)) {
|
size_t currentSize = 0;
|
||||||
length = sizeof(array);
|
char* parsePosition = &preamble[0];
|
||||||
|
if(addCrToPreamble) {
|
||||||
|
preamble[0] = '\r';
|
||||||
|
currentSize += 1;
|
||||||
|
parsePosition += 1;
|
||||||
}
|
}
|
||||||
memcpy(array, begin, length);
|
int32_t charCount = sprintf(parsePosition,
|
||||||
|
"%s: | %02" SCNu32 ":%02" SCNu32 ":%02" SCNu32 ".%03" SCNu32 " | ",
|
||||||
for( ; begin != end; begin++){
|
this->logMessage.c_str(), loggerTime.hour,
|
||||||
printChar(begin);
|
loggerTime.minute,
|
||||||
|
loggerTime.second,
|
||||||
|
loggerTime.usecond /1000);
|
||||||
|
if(charCount < 0) {
|
||||||
|
printf("ServiceInterfaceBuffer: Failure parsing preamble\r\n");
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
if(charCount > MAX_PREAMBLE_SIZE) {
|
||||||
|
printf("ServiceInterfaceBuffer: Char count too large for maximum "
|
||||||
|
"preamble size");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
currentSize += charCount;
|
||||||
|
if(preambleSize != nullptr) {
|
||||||
|
*preambleSize = currentSize;
|
||||||
|
}
|
||||||
|
return preamble;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef UT699
|
#ifdef UT699
|
||||||
#include <framework/osal/rtems/Interrupt.h>
|
#include <framework/osal/rtems/Interrupt.h>
|
||||||
|
|
||||||
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, uint16_t port) {
|
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message,
|
||||||
|
uint16_t port) {
|
||||||
this->log_message = set_message;
|
this->log_message = set_message;
|
||||||
this->isActive = true;
|
this->isActive = true;
|
||||||
setp( buf, buf + BUF_SIZE );
|
setp( buf, buf + BUF_SIZE );
|
||||||
|
@ -1,51 +1,71 @@
|
|||||||
#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
|
#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
|
||||||
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
|
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
|
||||||
|
|
||||||
|
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iosfwd>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstdio>
|
#include <iomanip>
|
||||||
|
|
||||||
#ifndef UT699
|
#ifndef UT699
|
||||||
class ServiceInterfaceBuffer: public std::basic_streambuf<char,
|
|
||||||
std::char_traits<char> > {
|
/**
|
||||||
|
* @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::streambuf {
|
||||||
friend class ServiceInterfaceStream;
|
friend class ServiceInterfaceStream;
|
||||||
public:
|
public:
|
||||||
ServiceInterfaceBuffer(std::string set_message, uint16_t port);
|
static constexpr uint8_t MAX_PREAMBLE_SIZE = 40;
|
||||||
|
|
||||||
|
ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble,
|
||||||
|
bool buffered, bool errStream, uint16_t port);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool isActive;
|
bool isActive;
|
||||||
// This is called when buffer becomes full. If
|
//! This is called when buffer becomes full. If
|
||||||
// buffer is not used, then this is called every
|
//! buffer is not used, then this is called every
|
||||||
// time when characters are put to stream.
|
//! time when characters are put to stream.
|
||||||
virtual int overflow(int c = Traits::eof());
|
int overflow(int c = Traits::eof()) override;
|
||||||
|
|
||||||
// This function is called when stream is flushed,
|
//! This function is called when stream is flushed,
|
||||||
// for example when std::endl is put to stream.
|
//! for example when std::endl is put to stream.
|
||||||
virtual int sync(void);
|
int sync(void) override;
|
||||||
|
|
||||||
|
bool isBuffered() const;
|
||||||
private:
|
private:
|
||||||
// For additional message information
|
//! For additional message information
|
||||||
std::string log_message;
|
std::string logMessage;
|
||||||
|
std::string preamble;
|
||||||
// For EOF detection
|
// For EOF detection
|
||||||
typedef std::char_traits<char> Traits;
|
typedef std::char_traits<char> Traits;
|
||||||
|
|
||||||
// Work in buffer mode. It is also possible to work without buffer.
|
//! This is useful for some terminal programs which do not have
|
||||||
|
//! implicit carriage return with newline characters.
|
||||||
|
bool addCrToPreamble;
|
||||||
|
|
||||||
|
//! Specifies whether the stream operates in buffered or unbuffered mode.
|
||||||
|
bool buffered;
|
||||||
|
//! This specifies to print to stderr and work in unbuffered mode.
|
||||||
|
bool errStream;
|
||||||
|
|
||||||
|
//! Needed for buffered mode.
|
||||||
static size_t const BUF_SIZE = 128;
|
static size_t const BUF_SIZE = 128;
|
||||||
char buf[BUF_SIZE];
|
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);
|
void putChars(char const* begin, char const* end);
|
||||||
|
|
||||||
|
std::string getPreamble(size_t * preambleSize = nullptr);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef UT699
|
#ifdef UT699
|
||||||
class ServiceInterfaceBuffer: public std::basic_streambuf<char,
|
class ServiceInterfaceBuffer: public std::basic_streambuf<char,
|
||||||
std::char_traits<char> > {
|
std::char_traits<char> > {
|
||||||
|
@ -1,11 +1,32 @@
|
|||||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||||
|
|
||||||
|
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
|
||||||
|
bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) :
|
||||||
|
std::ostream(&streambuf),
|
||||||
|
streambuf(setMessage, addCrToPreamble, buffered, errStream, port) {}
|
||||||
|
|
||||||
void ServiceInterfaceStream::setActive( bool myActive) {
|
void ServiceInterfaceStream::setActive( bool myActive) {
|
||||||
this->buf.isActive = myActive;
|
this->streambuf.isActive = myActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceInterfaceStream::ServiceInterfaceStream(std::string set_message,
|
std::string ServiceInterfaceStream::getPreamble() {
|
||||||
uint16_t port) :
|
return streambuf.getPreamble();
|
||||||
std::basic_ostream<char, std::char_traits<char> >(&buf), buf(
|
}
|
||||||
set_message, port) {
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,28 +3,56 @@
|
|||||||
|
|
||||||
#include <framework/serviceinterface/ServiceInterfaceBuffer.h>
|
#include <framework/serviceinterface/ServiceInterfaceBuffer.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iosfwd>
|
|
||||||
#include <sstream>
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
// Unfortunately, there must be a forward declaration of log_fe
|
/**
|
||||||
// (MUST be defined in main), to let the system know where to write to.
|
* Generic service interface stream which can be used like std::cout or
|
||||||
namespace sif {
|
* std::cerr but has additional capability. Add preamble and timestamp
|
||||||
extern std::ostream debug;
|
* to output. Can be run in buffered or unbuffered mode.
|
||||||
extern std::ostream info;
|
*/
|
||||||
extern std::ostream warning;
|
class ServiceInterfaceStream : public std::ostream {
|
||||||
extern std::ostream error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class ServiceInterfaceStream : public std::basic_ostream< char, std::char_traits< char > > {
|
|
||||||
protected:
|
|
||||||
ServiceInterfaceBuffer buf;
|
|
||||||
public:
|
public:
|
||||||
ServiceInterfaceStream( std::string set_message, uint16_t port = 1234 );
|
/**
|
||||||
|
* This constructor is used by specifying the preamble message.
|
||||||
|
* Optionally, the output can be directed to stderr and a CR character
|
||||||
|
* can be prepended to the preamble.
|
||||||
|
* @param setMessage message of preamble.
|
||||||
|
* @param addCrToPreamble Useful for applications like Puttty.
|
||||||
|
* @param buffered specify whether to use buffered mode.
|
||||||
|
* @param errStream specify which output stream to use (stderr or stdout).
|
||||||
|
*/
|
||||||
|
ServiceInterfaceStream(std::string setMessage,
|
||||||
|
bool addCrToPreamble = false, bool buffered = true,
|
||||||
|
bool errStream = false, uint16_t port = 1234);
|
||||||
|
|
||||||
|
//! An inactive stream will not print anything.
|
||||||
void setActive( bool );
|
void setActive( bool );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This can be used to retrieve the preamble in case it should be printed in
|
||||||
|
* the unbuffered mode.
|
||||||
|
* @return Preamle consisting of log message and timestamp.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Forward declaration of interface streams. These should be instantiated in
|
||||||
|
// main. They can then be used like std::cout or std::cerr.
|
||||||
|
namespace sif {
|
||||||
|
extern ServiceInterfaceStream debug;
|
||||||
|
extern ServiceInterfaceStream info;
|
||||||
|
extern ServiceInterfaceStream warning;
|
||||||
|
extern ServiceInterfaceStream error;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */
|
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user