Added getSize() to HapOut and changed default behavior to NEVER output anything

Note: getSize() must be called before flush(), which resets byte counter
This commit is contained in:
Gregg 2023-12-30 17:52:28 -06:00
parent 18c74e6f17
commit 83924a6bdf
2 changed files with 18 additions and 15 deletions

View File

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

View File

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