53 current_num_ = find_next_file_number();
56 if (current_num_ >= cfg_.max_files) {
58 current_num_ = find_next_file_number();
61 return open_file(current_num_);
72 size_t space = BUF_SIZE - pos_;
78 if (pos_ >= BUF_SIZE) {
89 if (!open_ || pos_ == 0) {
94 if (cfg_.millis && cfg_.flush_interval_ms > 0) {
96 uint32_t elapsed = now - last_flush_ms_;
97 time_to_flush = (elapsed >= cfg_.flush_interval_ms) || (pos_ >= BUF_SIZE * 3 / 4);
107 if (!open_ || pos_ == 0) {
123 static bool is_open() {
return open_; }
126 static constexpr size_t BUF_SIZE = 512;
127 static constexpr size_t NAME_BUF = 16;
132 const char*
p = cfg_.prefix;
133 while (*
p &&
i < 4) {
137 buf[
i++] =
'0' + (
num / 100) % 10;
138 buf[
i++] =
'0' + (
num / 10) % 10;
147 static uint16_t find_next_file_number() {
152 make_filename(name,
n);
157 return cfg_.max_files;
160 static void delete_oldest() {
165 make_filename(name,
n);
175 make_filename(name,
num);
185 static bool rotate_file() {
191 if (current_num_ >= cfg_.max_files) {
193 current_num_ = find_next_file_number();
196 return open_file(current_num_);
199 static void flush_buffer() {
205 if (cfg_.max_file_bytes > 0 && file_bytes_ + pos_ > cfg_.max_file_bytes) {
206 if (!rotate_file()) {
219 last_flush_ms_ = cfg_.millis();
223 static inline SdLogConfig cfg_;
224 static inline FIL fil_;
225 static inline char buf_[BUF_SIZE];
226 static inline size_t pos_ = 0;
227 static inline size_t file_bytes_ = 0;
228 static inline uint16_t current_num_ = 0;
229 static inline uint32_t last_flush_ms_ = 0;
230 static inline bool open_ =
false;