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:
parent
e5cea3ead0
commit
7259a13569
@ -3,9 +3,42 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
// 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 errStream, bool addCrToPreamble, uint16_t port):
|
||||||
|
isActive(true), logMessage(setMessage),
|
||||||
|
addCrToPreamble(addCrToPreamble), errStream(errStream) {
|
||||||
|
if(not errStream) {
|
||||||
|
// Set pointers if the stream is buffered.
|
||||||
|
setp( buf, buf + BUF_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++){
|
||||||
|
printChar(begin, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
int ServiceInterfaceBuffer::overflow(int c) {
|
int ServiceInterfaceBuffer::overflow(int c) {
|
||||||
|
if(errStream and this->isActive) {
|
||||||
|
if (c != Traits::eof()) {
|
||||||
|
printChar(reinterpret_cast<const char*>(&c), true);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
// Handle output
|
// Handle output
|
||||||
putChars(pbase(), pptr());
|
putChars(pbase(), pptr());
|
||||||
if (c != Traits::eof()) {
|
if (c != Traits::eof()) {
|
||||||
@ -20,53 +53,38 @@ int ServiceInterfaceBuffer::overflow(int c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ServiceInterfaceBuffer::sync(void) {
|
int ServiceInterfaceBuffer::sync(void) {
|
||||||
if (this->isActive) {
|
if(not this->isActive or errStream) {
|
||||||
Clock::TimeOfDay_t loggerTime;
|
if(not errStream) {
|
||||||
Clock::getDateAndTime(&loggerTime);
|
setp(buf, buf + BUF_SIZE - 1);
|
||||||
std::string preamble;
|
|
||||||
if(addCrToPreamble) {
|
|
||||||
preamble += "\r";
|
|
||||||
}
|
}
|
||||||
preamble += log_message + ": | " + zero_padded(loggerTime.hour, 2)
|
return 0;
|
||||||
+ ":" + zero_padded(loggerTime.minute, 2) + ":"
|
}
|
||||||
+ zero_padded(loggerTime.second, 2) + "."
|
|
||||||
+ zero_padded(loggerTime.usecond/1000, 3) + " | ";
|
auto preamble = getPreamble();
|
||||||
// Write log_message and time
|
// Write logMessage and time
|
||||||
this->putChars(preamble.c_str(), preamble.c_str() + preamble.size());
|
this->putChars(preamble.c_str(), preamble.c_str() + preamble.size());
|
||||||
// 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
|
||||||
setp(buf, buf + BUF_SIZE - 1);
|
setp(buf, buf + BUF_SIZE - 1);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string ServiceInterfaceBuffer::getPreamble() {
|
||||||
#ifndef UT699
|
Clock::TimeOfDay_t loggerTime;
|
||||||
|
Clock::getDateAndTime(&loggerTime);
|
||||||
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message,
|
std::string preamble;
|
||||||
uint16_t port, bool addCrToPreamble) {
|
if(addCrToPreamble) {
|
||||||
this->addCrToPreamble = addCrToPreamble;
|
preamble += "\r";
|
||||||
this->log_message = set_message;
|
}
|
||||||
this->isActive = true;
|
preamble += logMessage + ": | " + zero_padded(loggerTime.hour, 2)
|
||||||
setp( buf, buf + BUF_SIZE );
|
+ ":" + zero_padded(loggerTime.minute, 2) + ":"
|
||||||
|
+ zero_padded(loggerTime.second, 2) + "."
|
||||||
|
+ zero_padded(loggerTime.usecond/1000, 3) + " | ";
|
||||||
|
return preamble;
|
||||||
}
|
}
|
||||||
|
|
||||||
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++){
|
|
||||||
printChar(begin);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef UT699
|
#ifdef UT699
|
||||||
|
@ -3,43 +3,64 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstdio>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
#ifndef UT699
|
#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:
|
class ServiceInterfaceBuffer:
|
||||||
public std::basic_streambuf<char,std::char_traits<char>> {
|
public std::streambuf {
|
||||||
friend class ServiceInterfaceStream;
|
friend class ServiceInterfaceStream;
|
||||||
public:
|
public:
|
||||||
ServiceInterfaceBuffer(std::string set_message, uint16_t port,
|
ServiceInterfaceBuffer(std::string setMessage, bool errStream,
|
||||||
bool addCrToPreamble);
|
bool addCrToPreamble, 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.
|
||||||
int overflow(int c = Traits::eof()) override;
|
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.
|
||||||
int sync(void) override;
|
int sync(void) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// For additional message information
|
//! For additional message information
|
||||||
std::string log_message;
|
std::string logMessage;
|
||||||
// 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;
|
||||||
|
//! 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.
|
// 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];
|
||||||
|
|
||||||
// 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();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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>
|
template<typename T>
|
||||||
std::string zero_padded(const T& num, uint8_t width) {
|
std::string zero_padded(const T& num, uint8_t width) {
|
||||||
std::ostringstream string_to_pad;
|
std::ostringstream string_to_pad;
|
||||||
@ -52,6 +73,7 @@ private:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||||
|
|
||||||
|
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
|
||||||
|
bool errStream, bool addCrToPreamble, uint16_t port) :
|
||||||
|
std::ostream(&buf),
|
||||||
|
buf(setMessage, errStream, addCrToPreamble, port) {
|
||||||
|
}
|
||||||
|
|
||||||
void ServiceInterfaceStream::setActive( bool myActive) {
|
void ServiceInterfaceStream::setActive( bool myActive) {
|
||||||
this->buf.isActive = myActive;
|
this->buf.isActive = myActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceInterfaceStream::ServiceInterfaceStream(std::string set_message,
|
std::string ServiceInterfaceStream::getPreamble() {
|
||||||
bool addCrToPreamble, uint16_t port) :
|
return buf.getPreamble();
|
||||||
std::basic_ostream<char, std::char_traits<char>>(&buf),
|
|
||||||
buf(set_message, port, addCrToPreamble) {
|
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,9 @@
|
|||||||
|
|
||||||
#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
|
// 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.
|
// (MUST be defined in main), to let the system know where to write to.
|
||||||
namespace sif {
|
namespace sif {
|
||||||
extern std::ostream debug;
|
extern std::ostream debug;
|
||||||
@ -18,14 +16,41 @@ extern std::ostream error;
|
|||||||
|
|
||||||
|
|
||||||
class ServiceInterfaceStream :
|
class ServiceInterfaceStream :
|
||||||
public std::basic_ostream<char, std::char_traits<char>> {
|
public std::ostream {
|
||||||
protected:
|
protected:
|
||||||
ServiceInterfaceBuffer buf;
|
ServiceInterfaceBuffer buf;
|
||||||
public:
|
public:
|
||||||
ServiceInterfaceStream( std::string set_message,
|
/**
|
||||||
bool addCrToPreamble = false, 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 set_message
|
||||||
|
* @param errStream
|
||||||
|
* @param addCrToPreamble
|
||||||
|
* @param port
|
||||||
|
*/
|
||||||
|
ServiceInterfaceStream(std::string setMessage,
|
||||||
|
bool errStream = false, bool addCrToPreamble = 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();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Forward declaration of interface streams. These are needed so the public
|
||||||
|
// functions can be used by including this header
|
||||||
|
namespace sif {
|
||||||
|
extern ServiceInterfaceStream debugStream;
|
||||||
|
extern ServiceInterfaceStream infoStream;
|
||||||
|
extern ServiceInterfaceStream warningStream;
|
||||||
|
extern ServiceInterfaceStream errorStream;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */
|
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user