From 3a573c1b4c460cb19163d8bb9f319b501657f1e8 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Thu, 4 Jun 2020 19:37:33 +0200 Subject: [PATCH] no run-time dyn memory allocation now --- serviceinterface/ServiceInterfaceBuffer.cpp | 8 +++++- serviceinterface/ServiceInterfaceBuffer.h | 28 ++---------------- serviceinterface/ServiceInterfaceStream.cpp | 13 ++++----- serviceinterface/ServiceInterfaceStream.h | 32 ++++++++++----------- 4 files changed, 31 insertions(+), 50 deletions(-) diff --git a/serviceinterface/ServiceInterfaceBuffer.cpp b/serviceinterface/ServiceInterfaceBuffer.cpp index 30149ce0b..f1332637c 100644 --- a/serviceinterface/ServiceInterfaceBuffer.cpp +++ b/serviceinterface/ServiceInterfaceBuffer.cpp @@ -68,12 +68,15 @@ int ServiceInterfaceBuffer::overflow(int c) { } int ServiceInterfaceBuffer::sync(void) { - if(not this->isActive) { + if(not this->isActive and not buffered) { if(not buffered) { setp(buf, buf + BUF_SIZE - 1); } return 0; } + if(not buffered) { + return 0; + } size_t preambleSize = 0; auto preamble = getPreamble(&preambleSize); @@ -88,6 +91,9 @@ int ServiceInterfaceBuffer::sync(void) { return 0; } +bool ServiceInterfaceBuffer::isBuffered() const { + return buffered; +} std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) { Clock::TimeOfDay_t loggerTime; diff --git a/serviceinterface/ServiceInterfaceBuffer.h b/serviceinterface/ServiceInterfaceBuffer.h index 9a4ae4798..00a8e74a3 100644 --- a/serviceinterface/ServiceInterfaceBuffer.h +++ b/serviceinterface/ServiceInterfaceBuffer.h @@ -34,6 +34,7 @@ protected: //! for example when std::endl is put to stream. int sync(void) override; + bool isBuffered() const; private: //! For additional message information std::string logMessage; @@ -43,10 +44,10 @@ private: //! This is useful for some terminal programs which do not have //! implicit carriage return with newline characters. bool addCrToPreamble; - bool buffered; + //! This specifies to print to stderr and work in unbuffered mode. bool errStream; - + bool buffered; // Work in buffer mode. It is also possible to work without buffer. static size_t const BUF_SIZE = 128; char buf[BUF_SIZE]; @@ -55,29 +56,6 @@ private: void putChars(char const* begin, char const* end); std::string getPreamble(size_t * preambleSize = nullptr); - - /** - * This helper function returns the zero padded string version of a number. - * The type is deduced automatically. - * TODO: This uses dynamic memory allocation, so we should provide - * a custom streambuf class to use it (which takes maxSize as argument) - * Then we would propably - * @tparam T - * @param num - * @param width - * @return - */ - template - ReturnValue_t zeroPadded(std::string stringToFill, const T& num, - uint8_t width) { - auto numString = std::to_string(num); - uint8_t i = 0; - for(i = 0; i < width; i++) { - stringToFill[i] = '0'; - } - numString.copy(stringToFill.data() + i, numString.size()); - return HasReturnvaluesIF::RETURN_OK; - } }; #endif diff --git a/serviceinterface/ServiceInterfaceStream.cpp b/serviceinterface/ServiceInterfaceStream.cpp index bef8136e1..76481ed12 100644 --- a/serviceinterface/ServiceInterfaceStream.cpp +++ b/serviceinterface/ServiceInterfaceStream.cpp @@ -2,21 +2,20 @@ ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage, bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) : - std::ostream(&buf), - buf(setMessage, addCrToPreamble, buffered, errStream, port) { -} + std::ostream(&streambuf), + streambuf(setMessage, addCrToPreamble, buffered, errStream, port) {} void ServiceInterfaceStream::setActive( bool myActive) { - this->buf.isActive = myActive; + this->streambuf.isActive = myActive; } std::string ServiceInterfaceStream::getPreamble() { - return buf.getPreamble(); + return streambuf.getPreamble(); } void ServiceInterfaceStream::print(std::string error, bool withPreamble, bool withNewline, bool flush) { - if(not buffered and withPreamble) { + if(not streambuf.isBuffered() and withPreamble) { *this << getPreamble() << error; } else { @@ -27,7 +26,7 @@ void ServiceInterfaceStream::print(std::string error, *this << "\n"; } // if mode is non-buffered, no need to flush. - if(flush and buffered) { + if(flush and streambuf.isBuffered()) { this->flush(); } } diff --git a/serviceinterface/ServiceInterfaceStream.h b/serviceinterface/ServiceInterfaceStream.h index 9b8b5b6ed..9e19c228c 100644 --- a/serviceinterface/ServiceInterfaceStream.h +++ b/serviceinterface/ServiceInterfaceStream.h @@ -10,25 +10,22 @@ * std::cerr but has additional capability. Add preamble and timestamp * to output. Can be run in buffered or unbuffered mode. */ -class ServiceInterfaceStream : - public std::ostream { -protected: - ServiceInterfaceBuffer buf; +class ServiceInterfaceStream : public std::ostream { public: - /** - * 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 setMessage message of preamble. - * @param addCrToPreamble Useful for applications like Puttty. - * @param buffered specify whether to use buffered mode. - * @param errStream specify which output stream to use (stderr or stdout). - */ - ServiceInterfaceStream(std::string setMessage, - bool addCrToPreamble = false, bool buffered = true, + /** + * 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 setMessage message of preamble. + * @param addCrToPreamble Useful for applications like Puttty. + * @param buffered specify whether to use buffered mode. + * @param errStream specify which output stream to use (stderr or stdout). + */ + ServiceInterfaceStream(std::string setMessage, + bool addCrToPreamble = false, bool buffered = true, bool errStream = false, uint16_t port = 1234); - //! An inactive stream will not print anything. + //! An inactive stream will not print anything. void setActive( bool ); /** @@ -45,7 +42,8 @@ public: void print(std::string error, bool withPreamble = true, bool withNewline = true, bool flush = true); - bool buffered = false; +protected: + ServiceInterfaceBuffer streambuf; }; // Forward declaration of interface streams. These should be instantiated in