Fix file flushing issue on ESP32 - implement configurable smart flushing policy#22
Conversation
Co-authored-by: jibrilsharafi <83494924+jibrilsharafi@users.noreply.github.com>
jibrilsharafi
left a comment
There was a problem hiding this comment.
Very good. Fix according to the comments, and add also a very small and concise part about this in the README.md.
src/AdvancedLogger.cpp
Outdated
| if (entry.level >= _saveLevel) { | ||
| // Determine if immediate flush is needed based on log level | ||
| bool forceFlush = false; | ||
| #if ADVANCED_LOGGER_FLUSH_ON_ERROR |
There was a problem hiding this comment.
@copilot, modify this variable to be a default log level of error that can be overridden. In such a way, a user could decide to flush on warnings or debug, depending on their use case.
There was a problem hiding this comment.
Changed the hardcoded LogLevel::ERROR to use a configurable ADVANCED_LOGGER_FLUSH_LOG_LEVEL define. Users can now set it to any log level (WARNING, DEBUG, etc.) based on their use case. Default remains ERROR for backward compatibility.
Commit: 7d5eb4c
There was a problem hiding this comment.
@copilot you are close, but remove the ADVANCED_LOGGER_FLUSH_ON_ERROR and only keep the log level with ERROR as default. No need for two defines.
There was a problem hiding this comment.
Removed the ADVANCED_LOGGER_FLUSH_ON_ERROR define and simplified the configuration to use only ADVANCED_LOGGER_FLUSH_LOG_LEVEL with ERROR as default. The logic now directly compares the log level without needing an enable/disable flag.
Commit: fe6b0d2
Co-authored-by: jibrilsharafi <83494924+jibrilsharafi@users.noreply.github.com>
Added a concise File Flushing section to README.md in the Configuration area, documenting the new flush configuration options and their purpose. Commit: 7d5eb4c |
Co-authored-by: jibrilsharafi <83494924+jibrilsharafi@users.noreply.github.com>
jibrilsharafi
left a comment
There was a problem hiding this comment.
Wonderful job. We make a good team :)
|
Closes #21 |
Fix file flushing issue on ESP32 - implement configurable smart flushing policy (#22)
Fixes #21 - Log files were not being flushed to flash storage on ESP32, resulting in empty log files even when logs were being written.
Problem
In version 2.0.0, the
_save()function was called withflush = falseby default, causing log data to remain in memory buffers and potentially be lost during power cycles or system crashes. This was particularly problematic on ESP32 devices where users reported empty log files despite active logging.Solution
Implemented a smart flushing policy that balances performance with data persistence:
Automatic Periodic Flushing
Files are automatically flushed every 5 seconds to ensure regular data persistence without excessive I/O overhead.
Configurable Critical Level Flushing
Configurable log level triggers immediate flushes to ensure critical messages are never lost. Defaults to ERROR level but can be customized to WARNING, DEBUG, or any other level based on user requirements.
Safe File Operations
Added explicit flush before file close operations to guarantee data integrity.
Configuration Options
The flushing behavior is fully configurable via preprocessor defines:
Documentation
Added concise documentation in README.md explaining the file flushing configuration options and their usage.
Backward Compatibility
All existing code continues to work without modification. The smart flushing policy is applied automatically with sensible defaults that resolve the original issue.
Technical Details
_lastFlushTimetracking for periodic flush intervals_save()function with time-based flush logic_processLogEntry()to force flush on configurable log levelADVANCED_LOGGER_FLUSH_LOG_LEVELdefineTesting
Validated with comprehensive tests covering:
millis()overflowThe fix ensures log data persistence while maintaining the library's high-performance characteristics through intelligent and configurable flush timing.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.