preamble changes started

This commit is contained in:
Robin Müller 2020-06-04 14:08:26 +02:00
parent 764608005b
commit f8fb370ae7
5 changed files with 104 additions and 52 deletions

View File

@ -68,7 +68,7 @@ ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId,
} }
sif::error << "Component " << std::hex << componentId << sif::error << "Component " << std::hex << componentId <<
" not found, not adding it to pst" << std::endl; " not found, not adding it to pst\r" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }

View File

@ -8,13 +8,15 @@ extern "C" void printChar(const char*, bool errStream);
#ifndef UT699 #ifndef UT699
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage, ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
bool errStream, bool addCrToPreamble, uint16_t port): bool addCrToPreamble, bool buffered , bool errStream, uint16_t port):
isActive(true), logMessage(setMessage), isActive(true), logMessage(setMessage),
addCrToPreamble(addCrToPreamble), errStream(errStream) { addCrToPreamble(addCrToPreamble), buffered(buffered),
if(not errStream) { errStream(errStream) {
if(buffered) {
// 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);
} }
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) { void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
@ -26,16 +28,27 @@ void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
memcpy(array, begin, length); memcpy(array, begin, length);
for(; begin != end; begin++){ for(; begin != end; begin++){
printChar(begin, false); if(errStream) {
printChar(begin, true);
}
else {
printChar(begin, false);
}
} }
} }
#endif #endif
int ServiceInterfaceBuffer::overflow(int c) { int ServiceInterfaceBuffer::overflow(int c) {
if(errStream and this->isActive) { if(not buffered and this->isActive) {
if (c != Traits::eof()) { if (c != Traits::eof()) {
printChar(reinterpret_cast<const char*>(&c), true); if(errStream) {
printChar(reinterpret_cast<const char*>(&c), true);
}
else {
printChar(reinterpret_cast<const char*>(&c), false);
}
} }
return 0; return 0;
} }
@ -53,16 +66,17 @@ int ServiceInterfaceBuffer::overflow(int c) {
} }
int ServiceInterfaceBuffer::sync(void) { int ServiceInterfaceBuffer::sync(void) {
if(not this->isActive or errStream) { if(not this->isActive) {
if(not errStream) { if(not buffered) {
setp(buf, buf + BUF_SIZE - 1); setp(buf, buf + BUF_SIZE - 1);
} }
return 0; return 0;
} }
auto preamble = getPreamble(); size_t preambleSize = 0;
auto preamble = getPreamble(&preambleSize);
// Write logMessage and time // Write logMessage and time
this->putChars(preamble.c_str(), preamble.c_str() + preamble.size()); this->putChars(preamble.c_str(), preamble.c_str() + 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
@ -71,17 +85,34 @@ int ServiceInterfaceBuffer::sync(void) {
} }
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; //preamble.clear();
//std::string preamble;
size_t currentSize = 0;
if(addCrToPreamble) { if(addCrToPreamble) {
preamble += "\r"; preamble[0] = '\r';
currentSize += 1;
} }
preamble += logMessage + ": | " + zero_padded(loggerTime.hour, 2) logMessage.copy(preamble.data() + 1, logMessage.size());
+ ":" + zero_padded(loggerTime.minute, 2) + ":" currentSize += logMessage.size();
+ zero_padded(loggerTime.second, 2) + "." preamble[++currentSize] = ':';
+ zero_padded(loggerTime.usecond/1000, 3) + " | "; preamble[++currentSize] = ' ';
preamble[++currentSize] = '|';
zero_padded(loggerTime.hour, 2).copy(preamble.data() + )
//preamble.c_str() + 1 = logMessage;
//
// + ": | " + zero_padded(loggerTime.hour, 2)
// + ":" + zero_padded(loggerTime.minute, 2) + ":"
// + zero_padded(loggerTime.second, 2) + "."
// + zero_padded(loggerTime.usecond/1000, 3) + " | ";
currentSize += logMessage.size(); //+ 4 +2 +1 +2 +1 +2 +1 + 3 + 3;
preamble[currentSize] = '\0';
printf("%s", preamble.c_str());
uint8_t debugArray[96];
memcpy(debugArray, preamble.data(), currentSize);
*preambleSize = currentSize;
return preamble; return preamble;
} }

View File

@ -19,8 +19,8 @@ class ServiceInterfaceBuffer:
public std::streambuf { public std::streambuf {
friend class ServiceInterfaceStream; friend class ServiceInterfaceStream;
public: public:
ServiceInterfaceBuffer(std::string setMessage, bool errStream, ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble,
bool addCrToPreamble, uint16_t port); bool buffered, bool errStream, uint16_t port);
protected: protected:
bool isActive; bool isActive;
@ -36,11 +36,13 @@ protected:
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;
@ -51,7 +53,7 @@ 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. * This helper function returns the zero padded string version of a number.

View File

@ -1,15 +1,33 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage, ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
bool errStream, bool addCrToPreamble, uint16_t port) : bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) :
std::ostream(&streambuf), std::ostream(&buf),
streambuf(setMessage, errStream, addCrToPreamble, port) { buf(setMessage, addCrToPreamble, buffered, errStream, port) {
} }
void ServiceInterfaceStream::setActive( bool myActive) { void ServiceInterfaceStream::setActive( bool myActive) {
this->streambuf.isActive = myActive; this->buf.isActive = myActive;
} }
std::string ServiceInterfaceStream::getPreamble() { std::string ServiceInterfaceStream::getPreamble() {
return streambuf.getPreamble(); return buf.getPreamble();
}
void ServiceInterfaceStream::print(std::string error,
bool withPreamble, bool withNewline, bool flush) {
if(not buffered and withPreamble) {
*this << getPreamble() << error;
}
else {
*this << error;
}
if(withNewline) {
*this << "\n";
}
// if mode is non-buffered, no need to flush.
if(flush and buffered) {
this->flush();
}
} }

View File

@ -5,36 +5,28 @@
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>
/* Unfortunately, there must be a forward declaration of the log front end /**
* (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
* The ServiceInterfaceStream instances, which are declared below and * std::cerr but has additional capability. Add preamble and timestamp
* can instantaited somewhere else can be passed to these front ends by passing * to output. Can be run in buffered or unbuffered mode.
* their underlying buffers with .rdbuf() */ */
namespace sif {
extern std::ostream debug;
extern std::ostream info;
extern std::ostream warning;
extern std::ostream error;
}
class ServiceInterfaceStream : class ServiceInterfaceStream :
public std::ostream { public std::ostream {
protected: protected:
ServiceInterfaceBuffer streambuf; 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 set_message * @param setMessage message of preamble.
* @param errStream * @param addCrToPreamble Useful for applications like Puttty.
* @param addCrToPreamble * @param buffered specify whether to use buffered mode.
* @param port * @param errStream specify which output stream to use (stderr or stdout).
*/ */
ServiceInterfaceStream(std::string setMessage, ServiceInterfaceStream(std::string setMessage,
bool errStream = false, bool addCrToPreamble = false, bool addCrToPreamble = false, bool buffered = true,
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,15 +37,24 @@ public:
* @return Preamle consisting of log message and timestamp. * @return Preamle consisting of log message and timestamp.
*/ */
std::string getPreamble(); 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);
bool buffered = false;
}; };
// Forward declaration of interface streams. These are needed so the public // Forward declaration of interface streams. These should be instantiated in
// functions can be used by including this header // main. They can then be used like std::cout or std::cerr.
namespace sif { namespace sif {
extern ServiceInterfaceStream debugStream; extern ServiceInterfaceStream debug;
extern ServiceInterfaceStream infoStream; extern ServiceInterfaceStream info;
extern ServiceInterfaceStream warningStream; extern ServiceInterfaceStream warning;
extern ServiceInterfaceStream errorStream; extern ServiceInterfaceStream error;
} }
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */ #endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */