no run-time dyn memory allocation now

This commit is contained in:
Robin Müller 2020-06-04 19:37:33 +02:00
parent c0808e71d9
commit 3a573c1b4c
4 changed files with 31 additions and 50 deletions

View File

@ -68,12 +68,15 @@ int ServiceInterfaceBuffer::overflow(int c) {
} }
int ServiceInterfaceBuffer::sync(void) { int ServiceInterfaceBuffer::sync(void) {
if(not this->isActive) { if(not this->isActive and not buffered) {
if(not buffered) { if(not buffered) {
setp(buf, buf + BUF_SIZE - 1); setp(buf, buf + BUF_SIZE - 1);
} }
return 0; return 0;
} }
if(not buffered) {
return 0;
}
size_t preambleSize = 0; size_t preambleSize = 0;
auto preamble = getPreamble(&preambleSize); auto preamble = getPreamble(&preambleSize);
@ -88,6 +91,9 @@ int ServiceInterfaceBuffer::sync(void) {
return 0; return 0;
} }
bool ServiceInterfaceBuffer::isBuffered() const {
return buffered;
}
std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) { std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
Clock::TimeOfDay_t loggerTime; Clock::TimeOfDay_t loggerTime;

View File

@ -34,6 +34,7 @@ protected:
//! 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;
bool isBuffered() const;
private: private:
//! For additional message information //! For additional message information
std::string logMessage; std::string logMessage;
@ -43,10 +44,10 @@ private:
//! 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;
bool buffered;
// 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];
@ -55,29 +56,6 @@ private:
void putChars(char const* begin, char const* end); void putChars(char const* begin, char const* end);
std::string getPreamble(size_t * preambleSize = nullptr); 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<typename T>
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 #endif

View File

@ -2,21 +2,20 @@
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage, ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) : bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) :
std::ostream(&buf), std::ostream(&streambuf),
buf(setMessage, addCrToPreamble, buffered, errStream, port) { streambuf(setMessage, addCrToPreamble, buffered, errStream, port) {}
}
void ServiceInterfaceStream::setActive( bool myActive) { void ServiceInterfaceStream::setActive( bool myActive) {
this->buf.isActive = myActive; this->streambuf.isActive = myActive;
} }
std::string ServiceInterfaceStream::getPreamble() { std::string ServiceInterfaceStream::getPreamble() {
return buf.getPreamble(); return streambuf.getPreamble();
} }
void ServiceInterfaceStream::print(std::string error, void ServiceInterfaceStream::print(std::string error,
bool withPreamble, bool withNewline, bool flush) { bool withPreamble, bool withNewline, bool flush) {
if(not buffered and withPreamble) { if(not streambuf.isBuffered() and withPreamble) {
*this << getPreamble() << error; *this << getPreamble() << error;
} }
else { else {
@ -27,7 +26,7 @@ void ServiceInterfaceStream::print(std::string error,
*this << "\n"; *this << "\n";
} }
// if mode is non-buffered, no need to flush. // if mode is non-buffered, no need to flush.
if(flush and buffered) { if(flush and streambuf.isBuffered()) {
this->flush(); this->flush();
} }
} }

View File

@ -10,25 +10,22 @@
* std::cerr but has additional capability. Add preamble and timestamp * std::cerr but has additional capability. Add preamble and timestamp
* to output. Can be run in buffered or unbuffered mode. * to output. Can be run in buffered or unbuffered mode.
*/ */
class ServiceInterfaceStream : class ServiceInterfaceStream : public std::ostream {
public std::ostream {
protected:
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 setMessage message of preamble. * @param setMessage message of preamble.
* @param addCrToPreamble Useful for applications like Puttty. * @param addCrToPreamble Useful for applications like Puttty.
* @param buffered specify whether to use buffered mode. * @param buffered specify whether to use buffered mode.
* @param errStream specify which output stream to use (stderr or stdout). * @param errStream specify which output stream to use (stderr or stdout).
*/ */
ServiceInterfaceStream(std::string setMessage, ServiceInterfaceStream(std::string setMessage,
bool addCrToPreamble = false, bool buffered = true, bool addCrToPreamble = false, bool buffered = true,
bool errStream = false, 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,7 +42,8 @@ public:
void print(std::string error, bool withPreamble = true, void print(std::string error, bool withPreamble = true,
bool withNewline = true, bool flush = true); bool withNewline = true, bool flush = true);
bool buffered = false; protected:
ServiceInterfaceBuffer streambuf;
}; };
// Forward declaration of interface streams. These should be instantiated in // Forward declaration of interface streams. These should be instantiated in