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) {
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;

View File

@ -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<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

View File

@ -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();
}
}

View File

@ -10,10 +10,7 @@
* 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.
@ -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