new service interface stream

This commit is contained in:
Robin Müller 2020-06-04 19:40:43 +02:00
parent f2a9d29696
commit 6ff1cf46c5
4 changed files with 63 additions and 64 deletions

View File

@ -1,6 +1,7 @@
#include <framework/timemanager/Clock.h>
#include <framework/serviceinterface/ServiceInterfaceBuffer.h>
#include <cstring>
#include <inttypes.h>
// to be implemented by bsp
extern "C" void printChar(const char*, bool errStream);
@ -16,6 +17,8 @@ ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
// Set pointers if the stream is buffered.
setp( buf, buf + BUF_SIZE );
}
preamble.reserve(96);
preamble.resize(96);
}
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
@ -65,16 +68,22 @@ int ServiceInterfaceBuffer::overflow(int c) {
}
int ServiceInterfaceBuffer::sync(void) {
if(not this->isActive) {
if(not this->isActive and not buffered) {
if(not buffered) {
setp(buf, buf + BUF_SIZE - 1);
}
return 0;
}
if(not buffered) {
return 0;
}
auto preamble = getPreamble();
size_t preambleSize = 0;
auto preamble = getPreamble(&preambleSize);
uint8_t debugArray[96];
memcpy(debugArray, preamble.data(), preambleSize);
// Write logMessage and time
this->putChars(preamble.c_str(), preamble.c_str() + preamble.size());
this->putChars(preamble.data(), preamble.data() + preambleSize);
// Handle output
this->putChars(pbase(), pptr());
// This tells that buffer is empty again
@ -82,18 +91,35 @@ int ServiceInterfaceBuffer::sync(void) {
return 0;
}
bool ServiceInterfaceBuffer::isBuffered() const {
return buffered;
}
std::string ServiceInterfaceBuffer::getPreamble() {
std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
Clock::TimeOfDay_t loggerTime;
Clock::getDateAndTime(&loggerTime);
std::string preamble;
std::string preamble (96, 0);
size_t currentSize = 0;
char* parsePosition = &preamble[0];
if(addCrToPreamble) {
preamble += "\r";
preamble[0] = '\r';
currentSize += 1;
parsePosition += 1;
}
int32_t charCount = sprintf(parsePosition,
"%s: | %02" SCNu32 ":%02" SCNu32 ":%02" SCNu32 ".%03" SCNu32 " | ",
this->logMessage.c_str(), loggerTime.hour,
loggerTime.minute,
loggerTime.second,
loggerTime.usecond /1000);
if(charCount < 0) {
printf("ServiceInterfaceBuffer: Failure parsing preamble");
return "";
}
currentSize += charCount;
if(preambleSize != nullptr) {
*preambleSize = currentSize;
}
preamble += logMessage + ": | " + zero_padded(loggerTime.hour, 2)
+ ":" + zero_padded(loggerTime.minute, 2) + ":"
+ zero_padded(loggerTime.second, 2) + "."
+ zero_padded(loggerTime.usecond/1000, 3) + " | ";
return preamble;
}

View File

@ -1,6 +1,7 @@
#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <iostream>
#include <sstream>
#include <iomanip>
@ -33,18 +34,20 @@ protected:
//! for example when std::endl is put to stream.
int sync(void) override;
bool isBuffered() const;
private:
//! For additional message information
std::string logMessage;
std::string preamble;
// 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.
bool addCrToPreamble;
bool buffered;
//! This specifies to print to stderr and work in unbuffered mode.
bool errStream;
bool buffered;
// Work in buffer mode. It is also possible to work without buffer.
static size_t const BUF_SIZE = 128;
char buf[BUF_SIZE];
@ -52,39 +55,12 @@ private:
//! 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;
string_to_pad << std::setw(width) << std::setfill('0') << num;
std::string result = string_to_pad.str();
if (result.length() > width)
{
result.erase(0, result.length() - width);
}
return result;
}
std::string getPreamble(size_t * preambleSize = nullptr);
};
#endif
#ifdef UT699
class ServiceInterfaceBuffer: public std::basic_streambuf<char,
std::char_traits<char> > {

View File

@ -2,21 +2,20 @@
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) :
std::ostream(&buf),
buf(setMessage, addCrToPreamble, buffered, errStream, port) {
}
std::ostream(&streambuf),
streambuf(setMessage, addCrToPreamble, buffered, errStream, port) {}
void ServiceInterfaceStream::setActive( bool myActive) {
this->buf.isActive = myActive;
this->streambuf.isActive = myActive;
}
std::string ServiceInterfaceStream::getPreamble() {
return buf.getPreamble();
return streambuf.getPreamble();
}
void ServiceInterfaceStream::print(std::string error,
bool withPreamble, bool withNewline, bool flush) {
if(not buffered and withPreamble) {
if(not streambuf.isBuffered() and withPreamble) {
*this << getPreamble() << error;
}
else {
@ -27,7 +26,7 @@ void ServiceInterfaceStream::print(std::string error,
*this << "\n";
}
// if mode is non-buffered, no need to flush.
if(flush and buffered) {
if(flush and streambuf.isBuffered()) {
this->flush();
}
}

View File

@ -10,25 +10,22 @@
* std::cerr but has additional capability. Add preamble and timestamp
* to output. Can be run in buffered or unbuffered mode.
*/
class ServiceInterfaceStream :
public std::ostream {
protected:
ServiceInterfaceBuffer buf;
class ServiceInterfaceStream : public std::ostream {
public:
/**
* 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,
/**
* 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.
//! An inactive stream will not print anything.
void setActive( bool );
/**
@ -45,7 +42,8 @@ public:
void print(std::string error, bool withPreamble = true,
bool withNewline = true, bool flush = true);
bool buffered = false;
protected:
ServiceInterfaceBuffer streambuf;
};
// Forward declaration of interface streams. These should be instantiated in