Repository navigation
Expand file tree
/
Copy pathMultiLogger.hpp
More file actions
121 lines (101 loc) · 3.25 KB
/
Copy pathMultiLogger.hpp
File metadata and controls
121 lines (101 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef MULTILOGGER_HPP
#define MULTILOGGER_HPP
#include <Arduino.h>
#include <Print.h>
#include <FS.h>
#include <LittleFS.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "PsramUtils.hpp"
/**
* @brief Thread-safe logging class that forwards output to Serial and stores in a ring buffer.
*
* This class inherits from Print so it can be used as a drop-in replacement for Serial.
* All output is forwarded to both the physical Serial port and a thread-safe ring buffer
* in PSRAM that stores the last N log lines for WebSocket streaming.
*/
class MultiLogger : public Print {
public:
/**
* @brief Constructor
* @param bufferSize Number of log lines to keep in the ring buffer (default: 100)
*/
MultiLogger(size_t bufferSize = 100);
/**
* @brief Destructor
*/
~MultiLogger();
/**
* @brief Write a single byte (required by Print class)
* @param c The byte to write
* @return Number of bytes written (always 1)
*/
size_t write(uint8_t c) override;
/**
* @brief Write multiple bytes (optimized version)
* @param buffer Pointer to data buffer
* @param size Number of bytes to write
* @return Number of bytes written
*/
size_t write(const uint8_t* buffer, size_t size) override;
/**
* @brief Check if there are new log lines available
* @return true if new lines are available since last read
*/
bool hasNewLines();
/**
* @brief Get all new log lines since last call
* @param outLines Output vector to store the log lines
* @return Number of lines retrieved
*/
size_t getNewLines(PsramVector<PsramString>& outLines);
/**
* @brief Get all log lines in the buffer
* @param outLines Output vector to store the log lines
* @return Number of lines retrieved
*/
size_t getAllLines(PsramVector<PsramString>& outLines);
/**
* @brief Clear the ring buffer
*/
void clearBuffer();
/**
* @brief Enable or disable debug file logging
* @param enabled true to enable file logging, false to disable
*/
void setDebugFileEnabled(bool enabled);
/**
* @brief Check if debug file logging is enabled
* @return true if enabled
*/
bool isDebugFileEnabled() const { return _debugFileEnabled; }
private:
// Ring buffer stored in PSRAM
PsramVector<PsramString> _ringBuffer;
size_t _bufferSize;
size_t _writeIndex;
size_t _readIndex;
bool _bufferFull;
// Current line being built
PsramString _currentLine;
// Mutex for thread safety
SemaphoreHandle_t _mutex;
// Debug file logging
bool _debugFileEnabled;
File _debugFile;
const char* DEBUG_FILE_PATH = "/debug.log";
const size_t MAX_DEBUG_FILE_SIZE = 500 * 1024; // 500KB
/**
* @brief Finalize the current line and add it to the ring buffer
*/
void _finalizeLine();
/**
* @brief Write line to debug file if enabled
*/
void _writeToDebugFile(const PsramString& line);
};
// Serial mutex (from main application) - external declaration
extern SemaphoreHandle_t serialMutex;
// Global instance - to be used instead of Serial
extern MultiLogger Log;
#endif // MULTILOGGER_HPP