Skip to content

Fix file flushing issue on ESP32 - implement configurable smart flushing policy#22

Merged
jibrilsharafi merged 4 commits intodevelopmentfrom
copilot/fix-9c1560a2-5a0c-4979-9fa3-b894695d8b20
Sep 29, 2025
Merged

Fix file flushing issue on ESP32 - implement configurable smart flushing policy#22
jibrilsharafi merged 4 commits intodevelopmentfrom
copilot/fix-9c1560a2-5a0c-4979-9fa3-b894695d8b20

Conversation

Copy link
Contributor

Copilot AI commented Sep 29, 2025

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 with flush = false by 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:

// Custom flush interval (default: 5000ms)
#define ADVANCED_LOGGER_FLUSH_INTERVAL_MS 10000
#include "AdvancedLogger.h"

// Set custom log level for immediate flushing (default: ERROR)
#define ADVANCED_LOGGER_FLUSH_LOG_LEVEL LogLevel::WARNING
#include "AdvancedLogger.h"

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

  • Added _lastFlushTime tracking for periodic flush intervals
  • Enhanced _save() function with time-based flush logic
  • Modified _processLogEntry() to force flush on configurable log level
  • Implemented overflow-safe time comparison using unsigned arithmetic
  • Added comprehensive documentation for new configuration options
  • Made flush trigger level configurable via ADVANCED_LOGGER_FLUSH_LOG_LEVEL define
  • Simplified configuration API by using single log level define instead of separate enable/disable flag

Testing

Validated with comprehensive tests covering:

  • Normal operation scenarios
  • Critical error handling
  • Edge cases including millis() overflow
  • Real-world ESP32 usage patterns
  • Performance impact assessment
  • Configurable flush level validation

The fix ensures log data persistence while maintaining the library's high-performance characteristics through intelligent and configurable flush timing.

Original prompt

Fix issue #21 regarding the file flushing


💡 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.

Co-authored-by: jibrilsharafi <83494924+jibrilsharafi@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue #21 regarding the file flushing Fix file flushing issue on ESP32 - implement smart flushing policy Sep 29, 2025
Copilot AI requested a review from jibrilsharafi September 29, 2025 21:07
Copy link
Owner

@jibrilsharafi jibrilsharafi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good. Fix according to the comments, and add also a very small and concise part about this in the README.md.

if (entry.level >= _saveLevel) {
// Determine if immediate flush is needed based on log level
bool forceFlush = false;
#if ADVANCED_LOGGER_FLUSH_ON_ERROR
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Contributor Author

Copilot AI commented Sep 29, 2025

Very good. Fix according to the comments, and add also a very small and concise part about this in the README.md.

Added a concise File Flushing section to README.md in the Configuration area, documenting the new flush configuration options and their purpose.

Commit: 7d5eb4c

Copilot AI changed the title Fix file flushing issue on ESP32 - implement smart flushing policy Fix file flushing issue on ESP32 - implement configurable smart flushing policy Sep 29, 2025
Copilot AI requested a review from jibrilsharafi September 29, 2025 21:19
Co-authored-by: jibrilsharafi <83494924+jibrilsharafi@users.noreply.github.com>
@jibrilsharafi jibrilsharafi marked this pull request as ready for review September 29, 2025 21:31
Copy link
Owner

@jibrilsharafi jibrilsharafi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful job. We make a good team :)

@jibrilsharafi jibrilsharafi merged commit 1c3f5a3 into development Sep 29, 2025
3 checks passed
@jibrilsharafi jibrilsharafi deleted the copilot/fix-9c1560a2-5a0c-4979-9fa3-b894695d8b20 branch September 29, 2025 21:32
@jibrilsharafi
Copy link
Owner

Closes #21

jibrilsharafi added a commit that referenced this pull request Sep 29, 2025
Fix file flushing issue on ESP32 - implement configurable smart flushing policy (#22)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants