From eb2df3d88cc3bc2c70bda89208bf4e392bbef860 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Sun, 12 Apr 2020 23:06:57 +0200 Subject: [PATCH] Using C++ to implement preamble. adding optional flag for carriage return --- datapool/PoolRawAccessHelper.h | 2 +- osal/linux/FixedTimeslotTask.cpp | 4 ++- serviceinterface/ServiceInterfaceBuffer.cpp | 32 ++++++++++++--------- serviceinterface/ServiceInterfaceBuffer.h | 22 ++++++++++++-- serviceinterface/ServiceInterfaceStream.cpp | 4 +-- serviceinterface/ServiceInterfaceStream.h | 5 ++-- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/datapool/PoolRawAccessHelper.h b/datapool/PoolRawAccessHelper.h index 89974ea4f..7d9b9b487 100644 --- a/datapool/PoolRawAccessHelper.h +++ b/datapool/PoolRawAccessHelper.h @@ -69,7 +69,7 @@ private: struct SerializationArgs { uint8_t ** buffer; size_t * size; - const uint32_t max_size; + const size_t max_size; bool bigEndian; }; /** diff --git a/osal/linux/FixedTimeslotTask.cpp b/osal/linux/FixedTimeslotTask.cpp index 21040e6ef..7f117301a 100644 --- a/osal/linux/FixedTimeslotTask.cpp +++ b/osal/linux/FixedTimeslotTask.cpp @@ -9,7 +9,9 @@ uint32_t FixedTimeslotTask::deadlineMissedCount = 0; const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = PTHREAD_STACK_MIN; -FixedTimeslotTask::FixedTimeslotTask(const char* name_, int priority_, size_t stackSize_, uint32_t periodMs_):PosixThread(name_,priority_,stackSize_),pst(periodMs_),started(false) { +FixedTimeslotTask::FixedTimeslotTask(const char* name_, int priority_, + size_t stackSize_, uint32_t periodMs_): + PosixThread(name_,priority_,stackSize_),pst(periodMs_),started(false) { } FixedTimeslotTask::~FixedTimeslotTask() { diff --git a/serviceinterface/ServiceInterfaceBuffer.cpp b/serviceinterface/ServiceInterfaceBuffer.cpp index 527db07b1..a2365d000 100644 --- a/serviceinterface/ServiceInterfaceBuffer.cpp +++ b/serviceinterface/ServiceInterfaceBuffer.cpp @@ -1,13 +1,10 @@ #include #include #include -#include // to be implemented by bsp extern "C" void printChar(const char*); - - int ServiceInterfaceBuffer::overflow(int c) { // Handle output putChars(pbase(), pptr()); @@ -26,15 +23,16 @@ int ServiceInterfaceBuffer::sync(void) { if (this->isActive) { Clock::TimeOfDay_t loggerTime; Clock::getDateAndTime(&loggerTime); - char preamble[96] = { 0 }; - sprintf(preamble, "\r%s: | %" PRIu32 ":%02" PRIu32 ":%02" PRIu32 - ".%03" PRIu32 " | ", this->log_message.c_str(), - loggerTime.hour, - loggerTime.minute, - loggerTime.second, - 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()); } @@ -43,10 +41,13 @@ int ServiceInterfaceBuffer::sync(void) { return 0; } + + #ifndef UT699 ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string set_message, - uint16_t port) { + uint16_t port, bool addCrToPreamble) { + this->addCrToPreamble = addCrToPreamble; this->log_message = set_message; this->isActive = true; setp( buf, buf + BUF_SIZE ); @@ -67,10 +68,15 @@ void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) { } #endif +/** + * TODO: This is architecture specific. Maybe there is a better solution + * to move this into the Bsp folder.. + */ #ifdef UT699 #include -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 ); diff --git a/serviceinterface/ServiceInterfaceBuffer.h b/serviceinterface/ServiceInterfaceBuffer.h index b4756d417..a2bc4f4b1 100644 --- a/serviceinterface/ServiceInterfaceBuffer.h +++ b/serviceinterface/ServiceInterfaceBuffer.h @@ -2,16 +2,17 @@ #define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ #include -#include #include #include +#include #ifndef UT699 class ServiceInterfaceBuffer: public std::basic_streambuf> { 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 @@ -28,13 +29,28 @@ private: std::string log_message; // For EOF detection typedef std::char_traits 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 = 150; + static size_t const BUF_SIZE = 128; char buf[BUF_SIZE]; // In this function, the characters are parsed. void putChars(char const* begin, char const* end); + + template + 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 diff --git a/serviceinterface/ServiceInterfaceStream.cpp b/serviceinterface/ServiceInterfaceStream.cpp index f52413a44..40f52f1fe 100644 --- a/serviceinterface/ServiceInterfaceStream.cpp +++ b/serviceinterface/ServiceInterfaceStream.cpp @@ -5,7 +5,7 @@ void ServiceInterfaceStream::setActive( bool myActive) { } ServiceInterfaceStream::ServiceInterfaceStream(std::string set_message, - uint16_t port) : + bool addCrToPreamble, uint16_t port) : std::basic_ostream>(&buf), - buf(set_message, port) { + buf(set_message, port, addCrToPreamble) { } diff --git a/serviceinterface/ServiceInterfaceStream.h b/serviceinterface/ServiceInterfaceStream.h index 4a5b709fb..5e223f0eb 100644 --- a/serviceinterface/ServiceInterfaceStream.h +++ b/serviceinterface/ServiceInterfaceStream.h @@ -7,7 +7,7 @@ #include #include -//Unfortunately, there must be a forward declaration of log_fe +// 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; @@ -19,7 +19,8 @@ class ServiceInterfaceStream : 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 ); };