Codex added unified library, all work
This commit is contained in:
parent
18a1d1558c
commit
8370e546ff
25 changed files with 2935 additions and 0 deletions
13
lib/tbeam_logger/library.json
Normal file
13
lib/tbeam_logger/library.json
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "tbeam_logger",
|
||||
"version": "0.1.0",
|
||||
"description": "Print-compatible logger that tees Arduino output to Serial and TBeamStorage.",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "espressif32",
|
||||
"dependencies": [
|
||||
{
|
||||
"name": "tbeam_storage",
|
||||
"version": "0.1.0"
|
||||
}
|
||||
]
|
||||
}
|
||||
78
lib/tbeam_logger/src/TBeamLogger.cpp
Normal file
78
lib/tbeam_logger/src/TBeamLogger.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#include "TBeamLogger.h"
|
||||
|
||||
namespace tbeam {
|
||||
|
||||
bool TBeamLogger::begin(Print& serial, TBeamStorage* storage, const LoggerConfig& config) {
|
||||
serial_ = &serial;
|
||||
storage_ = storage;
|
||||
config_ = config;
|
||||
lastFlushMs_ = millis();
|
||||
return true;
|
||||
}
|
||||
|
||||
void TBeamLogger::update() {
|
||||
if (!config_.autoFlush || !storage_) {
|
||||
return;
|
||||
}
|
||||
const uint32_t now = millis();
|
||||
if ((uint32_t)(now - lastFlushMs_) >= config_.flushIntervalMs) {
|
||||
storage_->flush();
|
||||
lastFlushMs_ = now;
|
||||
}
|
||||
}
|
||||
|
||||
bool TBeamLogger::openLog(const char* path) {
|
||||
return storage_ && storage_->openLog(path);
|
||||
}
|
||||
|
||||
bool TBeamLogger::openUniqueLog(const char* prefix, const char* extension) {
|
||||
if (!storage_) {
|
||||
return false;
|
||||
}
|
||||
char path[128];
|
||||
if (!storage_->makeUniqueLogPath(prefix, extension, path, sizeof(path))) {
|
||||
return false;
|
||||
}
|
||||
return storage_->openLog(path);
|
||||
}
|
||||
|
||||
const char* TBeamLogger::currentLogPath() const {
|
||||
return storage_ ? storage_->currentLogPath() : "";
|
||||
}
|
||||
|
||||
bool TBeamLogger::storageReady() const {
|
||||
return storage_ && storage_->ready() && storage_->isLogOpen();
|
||||
}
|
||||
|
||||
void TBeamLogger::flush() {
|
||||
if (storage_) {
|
||||
storage_->flush();
|
||||
}
|
||||
if (serial_) {
|
||||
serial_->flush();
|
||||
}
|
||||
}
|
||||
|
||||
void TBeamLogger::closeLog() {
|
||||
if (storage_) {
|
||||
storage_->closeLog();
|
||||
}
|
||||
}
|
||||
|
||||
size_t TBeamLogger::write(uint8_t value) {
|
||||
return write(&value, 1);
|
||||
}
|
||||
|
||||
size_t TBeamLogger::write(const uint8_t* buffer, size_t size) {
|
||||
size_t serialWrote = 0;
|
||||
size_t storageWrote = 0;
|
||||
if (config_.echoSerial && serial_) {
|
||||
serialWrote = serial_->write(buffer, size);
|
||||
}
|
||||
if (config_.echoStorage && storage_ && storage_->isLogOpen()) {
|
||||
storageWrote = storage_->write(buffer, size);
|
||||
}
|
||||
return storageWrote > 0 ? storageWrote : serialWrote;
|
||||
}
|
||||
|
||||
} // namespace tbeam
|
||||
39
lib/tbeam_logger/src/TBeamLogger.h
Normal file
39
lib/tbeam_logger/src/TBeamLogger.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <TBeamStorage.h>
|
||||
|
||||
namespace tbeam {
|
||||
|
||||
struct LoggerConfig {
|
||||
bool echoSerial = true;
|
||||
bool echoStorage = true;
|
||||
bool autoFlush = true;
|
||||
uint32_t flushIntervalMs = 2000;
|
||||
};
|
||||
|
||||
class TBeamLogger : public Print {
|
||||
public:
|
||||
TBeamLogger() = default;
|
||||
|
||||
bool begin(Print& serial, TBeamStorage* storage = nullptr, const LoggerConfig& config = LoggerConfig{});
|
||||
void update();
|
||||
|
||||
bool openLog(const char* path);
|
||||
bool openUniqueLog(const char* prefix, const char* extension = ".log");
|
||||
const char* currentLogPath() const;
|
||||
bool storageReady() const;
|
||||
void flush();
|
||||
void closeLog();
|
||||
|
||||
size_t write(uint8_t value) override;
|
||||
size_t write(const uint8_t* buffer, size_t size) override;
|
||||
|
||||
private:
|
||||
Print* serial_ = nullptr;
|
||||
TBeamStorage* storage_ = nullptr;
|
||||
LoggerConfig config_{};
|
||||
uint32_t lastFlushMs_ = 0;
|
||||
};
|
||||
|
||||
} // namespace tbeam
|
||||
Loading…
Add table
Add a link
Reference in a new issue