new service interface stream
This commit is contained in:
parent
f2a9d29696
commit
6ff1cf46c5
@ -1,6 +1,7 @@
|
|||||||
#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*, bool errStream);
|
extern "C" void printChar(const char*, bool errStream);
|
||||||
@ -16,6 +17,8 @@ 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 );
|
||||||
}
|
}
|
||||||
|
preamble.reserve(96);
|
||||||
|
preamble.resize(96);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
|
||||||
@ -65,16 +68,22 @@ int ServiceInterfaceBuffer::overflow(int c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ServiceInterfaceBuffer::sync(void) {
|
int ServiceInterfaceBuffer::sync(void) {
|
||||||
if(not this->isActive) {
|
if(not this->isActive and not buffered) {
|
||||||
if(not buffered) {
|
if(not buffered) {
|
||||||
setp(buf, buf + BUF_SIZE - 1);
|
setp(buf, buf + BUF_SIZE - 1);
|
||||||
}
|
}
|
||||||
return 0;
|
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
|
// Write logMessage and time
|
||||||
this->putChars(preamble.c_str(), preamble.c_str() + preamble.size());
|
this->putChars(preamble.data(), preamble.data() + preambleSize);
|
||||||
// Handle output
|
// Handle output
|
||||||
this->putChars(pbase(), pptr());
|
this->putChars(pbase(), pptr());
|
||||||
// This tells that buffer is empty again
|
// This tells that buffer is empty again
|
||||||
@ -82,18 +91,35 @@ int ServiceInterfaceBuffer::sync(void) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ServiceInterfaceBuffer::isBuffered() const {
|
||||||
|
return buffered;
|
||||||
|
}
|
||||||
|
|
||||||
std::string ServiceInterfaceBuffer::getPreamble() {
|
std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
|
||||||
Clock::TimeOfDay_t loggerTime;
|
Clock::TimeOfDay_t loggerTime;
|
||||||
Clock::getDateAndTime(&loggerTime);
|
Clock::getDateAndTime(&loggerTime);
|
||||||
std::string preamble;
|
std::string preamble (96, 0);
|
||||||
|
size_t currentSize = 0;
|
||||||
|
char* parsePosition = &preamble[0];
|
||||||
if(addCrToPreamble) {
|
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;
|
return preamble;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#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 <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
@ -33,18 +34,20 @@ protected:
|
|||||||
//! for example when std::endl is put to stream.
|
//! for example when std::endl is put to stream.
|
||||||
int sync(void) override;
|
int sync(void) override;
|
||||||
|
|
||||||
|
bool isBuffered() const;
|
||||||
private:
|
private:
|
||||||
//! For additional message information
|
//! For additional message information
|
||||||
std::string logMessage;
|
std::string logMessage;
|
||||||
|
std::string preamble;
|
||||||
// For EOF detection
|
// For EOF detection
|
||||||
typedef std::char_traits<char> Traits;
|
typedef std::char_traits<char> Traits;
|
||||||
//! This is useful for some terminal programs which do not have
|
//! This is useful for some terminal programs which do not have
|
||||||
//! implicit carriage return with newline characters.
|
//! implicit carriage return with newline characters.
|
||||||
bool addCrToPreamble;
|
bool addCrToPreamble;
|
||||||
bool buffered;
|
|
||||||
//! This specifies to print to stderr and work in unbuffered mode.
|
//! This specifies to print to stderr and work in unbuffered mode.
|
||||||
bool errStream;
|
bool errStream;
|
||||||
|
bool buffered;
|
||||||
// Work in buffer mode. It is also possible to work without buffer.
|
// Work in buffer mode. It is also possible to work without buffer.
|
||||||
static size_t const BUF_SIZE = 128;
|
static size_t const BUF_SIZE = 128;
|
||||||
char buf[BUF_SIZE];
|
char buf[BUF_SIZE];
|
||||||
@ -52,39 +55,12 @@ private:
|
|||||||
//! 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();
|
std::string getPreamble(size_t * preambleSize = nullptr);
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#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> > {
|
||||||
|
@ -2,21 +2,20 @@
|
|||||||
|
|
||||||
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
|
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
|
||||||
bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) :
|
bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) :
|
||||||
std::ostream(&buf),
|
std::ostream(&streambuf),
|
||||||
buf(setMessage, addCrToPreamble, buffered, errStream, port) {
|
streambuf(setMessage, addCrToPreamble, buffered, errStream, port) {}
|
||||||
}
|
|
||||||
|
|
||||||
void ServiceInterfaceStream::setActive( bool myActive) {
|
void ServiceInterfaceStream::setActive( bool myActive) {
|
||||||
this->buf.isActive = myActive;
|
this->streambuf.isActive = myActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ServiceInterfaceStream::getPreamble() {
|
std::string ServiceInterfaceStream::getPreamble() {
|
||||||
return buf.getPreamble();
|
return streambuf.getPreamble();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServiceInterfaceStream::print(std::string error,
|
void ServiceInterfaceStream::print(std::string error,
|
||||||
bool withPreamble, bool withNewline, bool flush) {
|
bool withPreamble, bool withNewline, bool flush) {
|
||||||
if(not buffered and withPreamble) {
|
if(not streambuf.isBuffered() and withPreamble) {
|
||||||
*this << getPreamble() << error;
|
*this << getPreamble() << error;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -27,7 +26,7 @@ void ServiceInterfaceStream::print(std::string error,
|
|||||||
*this << "\n";
|
*this << "\n";
|
||||||
}
|
}
|
||||||
// if mode is non-buffered, no need to flush.
|
// if mode is non-buffered, no need to flush.
|
||||||
if(flush and buffered) {
|
if(flush and streambuf.isBuffered()) {
|
||||||
this->flush();
|
this->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,25 +10,22 @@
|
|||||||
* std::cerr but has additional capability. Add preamble and timestamp
|
* std::cerr but has additional capability. Add preamble and timestamp
|
||||||
* to output. Can be run in buffered or unbuffered mode.
|
* to output. Can be run in buffered or unbuffered mode.
|
||||||
*/
|
*/
|
||||||
class ServiceInterfaceStream :
|
class ServiceInterfaceStream : public std::ostream {
|
||||||
public std::ostream {
|
|
||||||
protected:
|
|
||||||
ServiceInterfaceBuffer buf;
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* This constructor is used by specifying the preamble message.
|
* This constructor is used by specifying the preamble message.
|
||||||
* Optionally, the output can be directed to stderr and a CR character
|
* Optionally, the output can be directed to stderr and a CR character
|
||||||
* can be prepended to the preamble.
|
* can be prepended to the preamble.
|
||||||
* @param setMessage message of preamble.
|
* @param setMessage message of preamble.
|
||||||
* @param addCrToPreamble Useful for applications like Puttty.
|
* @param addCrToPreamble Useful for applications like Puttty.
|
||||||
* @param buffered specify whether to use buffered mode.
|
* @param buffered specify whether to use buffered mode.
|
||||||
* @param errStream specify which output stream to use (stderr or stdout).
|
* @param errStream specify which output stream to use (stderr or stdout).
|
||||||
*/
|
*/
|
||||||
ServiceInterfaceStream(std::string setMessage,
|
ServiceInterfaceStream(std::string setMessage,
|
||||||
bool addCrToPreamble = false, bool buffered = true,
|
bool addCrToPreamble = false, bool buffered = true,
|
||||||
bool errStream = false, uint16_t port = 1234);
|
bool errStream = false, uint16_t port = 1234);
|
||||||
|
|
||||||
//! An inactive stream will not print anything.
|
//! An inactive stream will not print anything.
|
||||||
void setActive( bool );
|
void setActive( bool );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,7 +42,8 @@ public:
|
|||||||
void print(std::string error, bool withPreamble = true,
|
void print(std::string error, bool withPreamble = true,
|
||||||
bool withNewline = true, bool flush = true);
|
bool withNewline = true, bool flush = true);
|
||||||
|
|
||||||
bool buffered = false;
|
protected:
|
||||||
|
ServiceInterfaceBuffer streambuf;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Forward declaration of interface streams. These should be instantiated in
|
// Forward declaration of interface streams. These should be instantiated in
|
||||||
|
Loading…
Reference in New Issue
Block a user