diff --git a/src/lib/utils/rotatefile.cpp b/src/lib/utils/rotatefile.cpp index 0133b61f..af24c3f0 100644 --- a/src/lib/utils/rotatefile.cpp +++ b/src/lib/utils/rotatefile.cpp @@ -71,17 +71,21 @@ void RotateFile::write(const unsigned char* value, const size_t size, const bool for (unsigned int pos = 0; pos < size; pos++) { fprintf(m_stream, "%2.2x ", value[pos]); } + m_fileSize += 25+3*size+1; } else { fwrite(value, 1, size, m_stream); + m_fileSize += size+1; } fprintf(m_stream, "\n"); - m_fileSize += 25+3*size+1; + fflush(m_stream); } else { fwrite(value, (streamsize)size, 1, m_stream); m_fileSize += size; - } - if ((m_fileSize%1024) == 0) { - fflush(m_stream); + m_flushSize += size; + if (m_flushSize>16) { + fflush(m_stream); + m_flushSize = 0; + } } if (m_fileSize >= m_maxSize * 1024LL) { string oldfile = string(m_fileName)+".old"; diff --git a/src/lib/utils/rotatefile.h b/src/lib/utils/rotatefile.h index ee111bf6..67df2a23 100644 --- a/src/lib/utils/rotatefile.h +++ b/src/lib/utils/rotatefile.h @@ -45,7 +45,8 @@ class RotateFile { * @param textMode whether to write each byte with prefixed timestamp and direction as text. */ RotateFile(const string fileName, const unsigned int maxSize, const bool textMode = false) - : m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {} + : m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0), + m_flushSize(0) {} /** * Destructor. @@ -92,8 +93,11 @@ class RotateFile { /** the @a FILE to writing to. */ FILE* m_stream; - /** the number of bytes already written to the @a m_file. */ + /** the number of bytes written to @a m_file. */ uint64_t m_fileSize; + + /** the number of bytes written to @a m_file since the last flush. */ + uint64_t m_flushSize; }; } // namespace ebusd