service interface testing for eclipse

This commit is contained in:
Robin Müller 2020-04-21 19:57:16 +02:00
parent cd7e47ccbb
commit e3ad5d25c1
4 changed files with 52 additions and 23 deletions

View File

@ -23,14 +23,16 @@ int ServiceInterfaceBuffer::sync(void) {
if (this->isActive) {
Clock::TimeOfDay_t loggerTime;
Clock::getDateAndTime(&loggerTime);
char preamble[96] = { 0 };
sprintf(preamble, "%s: | %lu:%02lu:%02lu.%03lu | ",
this->log_message.c_str(), (unsigned long) loggerTime.hour,
(unsigned long) loggerTime.minute,
(unsigned long) loggerTime.second,
(unsigned long) loggerTime.usecond /1000);
std::string preamble;
if(addCrToPreamble) {
preamble += "\r";
}
preamble += log_message + ": | " + zero_padded(loggerTime.hour, 2)
+ ":" + zero_padded(loggerTime.minute, 2) + ":"
+ zero_padded(loggerTime.second, 2) + "."
+ zero_padded(loggerTime.usecond/1000, 3) + " | ";
// Write log_message and time
this->putChars(preamble, preamble + sizeof(preamble));
this->putChars(preamble.c_str(), preamble.c_str() + preamble.size());
// Handle output
this->putChars(pbase(), pptr());
}
@ -39,9 +41,13 @@ int ServiceInterfaceBuffer::sync(void) {
return 0;
}
#ifndef UT699
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, uint16_t port) {
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message,
uint16_t port, bool addCrToPreamble) {
this->addCrToPreamble = addCrToPreamble;
this->log_message = set_message;
this->isActive = true;
setp( buf, buf + BUF_SIZE );
@ -55,17 +61,22 @@ void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
}
memcpy(array, begin, length);
for( ; begin != end; begin++){
for(; begin != end; begin++){
printChar(begin);
}
}
#endif
/**
* TODO: This is architecture specific. Maybe there is a better solution
* to move this into the Bsp folder..
*/
#ifdef UT699
#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->isActive = true;
setp( buf, buf + BUF_SIZE );

View File

@ -2,32 +2,36 @@
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#include <iostream>
#include <iosfwd>
#include <sstream>
#include <cstdio>
#include <iomanip>
#ifndef UT699
class ServiceInterfaceBuffer: public std::basic_streambuf<char,
std::char_traits<char> > {
class ServiceInterfaceBuffer:
public std::basic_streambuf<char,std::char_traits<char>> {
friend class ServiceInterfaceStream;
public:
ServiceInterfaceBuffer(std::string set_message, uint16_t port);
ServiceInterfaceBuffer(std::string set_message, uint16_t port,
bool addCrToPreamble);
protected:
bool isActive;
// This is called when buffer becomes full. If
// buffer is not used, then this is called every
// 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,
// for example when std::endl is put to stream.
virtual int sync(void);
int sync(void) override;
private:
// For additional message information
std::string log_message;
// 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;
// Work in buffer mode. It is also possible to work without buffer.
static size_t const BUF_SIZE = 128;
@ -35,6 +39,18 @@ private:
// In this function, the characters are parsed.
void putChars(char const* begin, char const* end);
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

View File

@ -5,7 +5,7 @@ void ServiceInterfaceStream::setActive( bool myActive) {
}
ServiceInterfaceStream::ServiceInterfaceStream(std::string set_message,
uint16_t port) :
std::basic_ostream<char, std::char_traits<char> >(&buf), buf(
set_message, port) {
bool addCrToPreamble, uint16_t port) :
std::basic_ostream<char, std::char_traits<char>>(&buf),
buf(set_message, port, addCrToPreamble) {
}

View File

@ -7,20 +7,22 @@
#include <sstream>
#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.
// Unfortunately, there must be a forward declaration of log_fe
// (MUST be defined in main), to let the system know where to write to.
extern std::ostream debug;
extern std::ostream info;
extern std::ostream warning;
extern std::ostream error;
class ServiceInterfaceStream : public std::basic_ostream< char, std::char_traits< char > > {
class ServiceInterfaceStream :
public std::basic_ostream<char, std::char_traits<char>> {
protected:
ServiceInterfaceBuffer buf;
public:
ServiceInterfaceStream( std::string set_message, uint16_t port = 1234 );
ServiceInterfaceStream( std::string set_message,
bool addCrToPreamble = false, uint16_t port = 1234);
void setActive( bool );
};
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */