From 83924a6bdfb5c53fdce0d6771ce1354e19997e00 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 30 Dec 2023 17:52:28 -0600 Subject: [PATCH] Added getSize() to HapOut and changed default behavior to NEVER output anything Note: getSize() must be called before flush(), which resets byte counter --- src/HAP.cpp | 23 +++++++++++------------ src/HAP.h | 10 +++++++--- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 6b7e38e..f4d2657 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1638,7 +1638,7 @@ void Nonce::inc(){ HapOut::HapStreamBuffer::HapStreamBuffer(){ - buffer=(char *)HS_MALLOC(bufSize+1); // add 1 for optional null terminator when printing text + buffer=(char *)HS_MALLOC(bufSize+1); // add 1 for adding null terminator when printing text setp(buffer, buffer+bufSize-1); } @@ -1652,9 +1652,12 @@ HapOut::HapStreamBuffer::~HapStreamBuffer(){ ////////////////////////////////////// -int HapOut::HapStreamBuffer::flushBuffer(){ +void HapOut::HapStreamBuffer::flushBuffer(){ + int num=pptr()-pbase(); + byteCount+=num; + if(logLevel<=homeSpan.getLogLevel()){ buffer[num]='\0'; Serial.print(buffer); @@ -1662,12 +1665,10 @@ int HapOut::HapStreamBuffer::flushBuffer(){ if(hapClient!=NULL){ hapClient->client.write(buffer,num); + delay(1); } - delay(1); - pbump(-num); - return(num); } ////////////////////////////////////// @@ -1679,8 +1680,7 @@ std::streambuf::int_type HapOut::HapStreamBuffer::overflow(std::streambuf::int_t pbump(1); } - if(flushBuffer()==EOF) - return(EOF); + flushBuffer(); return(c); } @@ -1688,14 +1688,13 @@ std::streambuf::int_type HapOut::HapStreamBuffer::overflow(std::streambuf::int_t int HapOut::HapStreamBuffer::sync(){ - int_type c=flushBuffer(); + flushBuffer(); - enablePrettyPrint=false; - logLevel=0; + logLevel=255; hapClient=NULL; + enablePrettyPrint=false; + byteCount=0; - if(c==EOF) - return(-1); return(0); } diff --git a/src/HAP.h b/src/HAP.h index e690cc0..ae766d0 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -191,12 +191,14 @@ class HapOut : public std::ostream { const size_t bufSize=1024; // max allowed for HAP encrypted records char *buffer; HAPClient *hapClient=NULL; - int logLevel=0; + int logLevel=255; // default is NOT to print anything boolean enablePrettyPrint=false; + size_t byteCount=0; - int flushBuffer() ; + void flushBuffer(); int_type overflow(int_type c) override; int sync() override; + size_t getSize(){return(byteCount+pptr()-pbase());} HapStreamBuffer(); ~HapStreamBuffer(); @@ -210,7 +212,9 @@ class HapOut : public std::ostream { HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);} HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);} - HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;return(*this);} + HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;hapBuffer.logLevel=0;return(*this);} + + size_t getSize(){return(hapBuffer.getSize());} }; /////////////////////////////////////////////////