@@ -15,10 +15,41 @@ body{
15
15
font-family: monospace;
16
16
font-size: 0.7rem;
17
17
}
18
+
19
+ #conLed {
20
+ width: 10px;
21
+ height: 10px;
22
+ background-color: lightGray;
23
+ border-radius: 10px;
24
+
25
+ }
26
+ #conLed.on {
27
+ background-color: lightGreen;
28
+ border: 1px solid #4CAF50;
29
+ }
18
30
</style>
19
31
<body>
32
+ <table><tr>
33
+ <td><div id="conLed"> </td>
34
+ <td><span id="wsStatus">Disconnected</span></td>
35
+ </tr>
36
+ </table>
37
+
20
38
<input type="text" id="logLine" style="Display: None;">
21
39
<span id="logText"></span>
40
+
41
+ <script>
42
+ document.getElementById("wsStatus").addEventListener("change", (event) => {
43
+ conLed = document.getElementById("conLed")
44
+ if(event.target.innerHTML.startsWith("Connected: ")){
45
+ conLed.classList.add("on");
46
+ }else{
47
+ conLed.classList.remove("on");
48
+ }
49
+ conLed.title = event.target.innerHTML
50
+ });
51
+ </script>
52
+
22
53
</body>
23
54
)=====" ;
24
55
@@ -74,9 +105,20 @@ const toColor = (line) => {
74
105
)=====" ;
75
106
76
107
#include < ControlAssist.h>
77
-
78
- static class RemoteLogViewer *pLogView = NULL ; // Pointer to instance of log print viewer class
79
- #define MAX_LOG_BUFFER_LEN 8192 // Maximum size of log buffer to store when disconnected
108
+ // Log print arguments
109
+ #define MAX_LOG_BUFFER_LEN 8192 // Maximum size of log buffer to store when disconnected
110
+ #define MAX_LOG_FMT 256 // Maximum size of log format
111
+ static char fmtBuf[MAX_LOG_FMT]; // Format buffer
112
+ static char outBuf[512 ]; // Output buffer
113
+ static va_list arglist;
114
+ static File log_file;
115
+ // Logger file open/close
116
+ // Must called before LOG_I function to write to file and
117
+ // closed on application shutdown
118
+ #define LOGGER_OPEN_LOG () if (!log_file) log_file = STORAGE.open(LOGGER_LOG_FILENAME, " a+" )
119
+ #define LOGGER_CLOSE_LOG () if (log_file) log_file.close()
120
+ static class RemoteLogViewer *_pLogView = NULL ; // Pointer to instance of log print viewer class
121
+ void wsStatusChange (); // Handles connect / disconnect
80
122
81
123
// Remote log print viewer class
82
124
class RemoteLogViewer : public ControlAssist {
@@ -87,35 +129,84 @@ class RemoteLogViewer: public ControlAssist{
87
129
RemoteLogViewer (uint16_t port) { ControlAssist::setPort (port); _logBuffer = " " ; }
88
130
virtual ~RemoteLogViewer () { }
89
131
public:
90
- void setup (){
132
+ void init (){
91
133
// Must not initalized
92
- if (pLogView ){
93
- LOG_E (" Already initialized with addr: %p\n " , pLogView );
134
+ if (_pLogView ){
135
+ LOG_E (" Already initialized with addr: %p\n " , _pLogView );
94
136
return ;
95
137
}
96
138
// Store this instance pointer to global
97
- pLogView = this ;
139
+ _pLogView = this ;
98
140
// Setup control assist
99
141
ControlAssist::setHtmlBody (LOGGER_VIEWER_HTML_BODY);
100
142
ControlAssist::setHtmlFooter (LOGGER_VIEWER_HTML_SCRIPT);
143
+ ControlAssist::bind (" wsStatus" ," Disconnected" , wsStatusChange);
101
144
ControlAssist::bind (" logLine" );
102
145
}
146
+ // Delete the log file
147
+ void resetLogFile (){
148
+ if (!STORAGE.exists (LOGGER_LOG_FILENAME)) return ;
149
+ LOG_W (" Reseting log\n " );
150
+ LOGGER_CLOSE_LOG ();
151
+ STORAGE.remove (LOGGER_LOG_FILENAME);
152
+ // Reopen log file to store log lines
153
+ LOGGER_OPEN_LOG ();
154
+ }
155
+
156
+ // Send server responce on /log
157
+ void handleLog (WEB_SERVER &server){
158
+ String res = " " ;
159
+ while ( getHtmlChunk (res) ){
160
+ server.sendContent (res);
161
+ }
162
+ server.sendContent (" " );
163
+ }
164
+ // Reset or display log file
165
+ void handleLogFile (WEB_SERVER &server){
166
+ server.setContentLength (CONTENT_LENGTH_UNKNOWN);
167
+ if (server.hasArg (" reset" )){
168
+ resetLogFile ();
169
+ server.sendContent (" Reseted log" );
170
+ return ;
171
+ }
172
+ if (!STORAGE.exists (LOGGER_LOG_FILENAME)){
173
+ server.sendContent (" Logging file not exists" );
174
+ return ;
175
+ }
176
+ LOGGER_CLOSE_LOG ();
177
+ // Open for reading
178
+ File file = STORAGE.open (LOGGER_LOG_FILENAME, " r" );
179
+ // Send log file contents
180
+ String res = " " ;
181
+ while ( file.available () ){
182
+ res = file.readStringUntil (' \n ' ) + " \n " ;
183
+ server.sendContent (res);
184
+ }
185
+ file.close ();
186
+ server.sendContent (" " );
187
+ // Reopen log file to store log lines
188
+ LOGGER_OPEN_LOG ();
189
+ }
190
+ void sendBuffer (){
191
+ if (_logBuffer == " " ) return ;
192
+ String logLine = " " ;
193
+ int l = 0 ;
194
+ // Send log buffer lines
195
+ while (_logBuffer.length () > 0 ){
196
+ l = _logBuffer.indexOf (" \n " , 0 );
197
+ if (l < 0 ) break ;
198
+ logLine = _logBuffer.substring (0 , l );
199
+ ControlAssist::put (" logLine" , logLine );
200
+ _logBuffer = _logBuffer.substring (l + 1 , _logBuffer.length () );
201
+ }
202
+ }
103
203
104
204
// Custom log print function
105
205
void print (String line){
106
206
// Are clients connected?
107
207
if (ControlAssist::getClientsNum ()>0 ){
108
208
if (_logBuffer != " " ){
109
- String logLine = " " ;
110
- int l = 0 ;
111
- // Send log buffer lines
112
- while (_logBuffer.length () > 0 ){
113
- l = _logBuffer.indexOf (" \n " , 0 );
114
- if (l < 0 ) break ;
115
- logLine = _logBuffer.substring (0 , l );
116
- ControlAssist::put (" logLine" , logLine );
117
- _logBuffer = _logBuffer.substring (l + 1 , _logBuffer.length () );
118
- }
209
+ sendBuffer ();
119
210
_logBuffer = " " ;
120
211
}
121
212
// Send current line
@@ -128,33 +219,33 @@ class RemoteLogViewer: public ControlAssist{
128
219
if (_logBuffer.length () > MAX_LOG_BUFFER_LEN){
129
220
int l = _logBuffer.indexOf (" \n " , 0 );
130
221
if (l >= 0 )
131
- _logBuffer = _logBuffer.substring (l + 1 , _logBuffer.length () - 1 );
222
+ _logBuffer = _logBuffer.substring (l + 1 , _logBuffer.length ());
132
223
}
133
224
}
134
225
public:
135
226
String _logBuffer; // String buffer to hold log lines
136
227
};
137
228
138
- // Log print arguments
139
- #define MAX_LOG_FMT 256 // Maximum size of log format
140
- static char fmtBuf[MAX_LOG_FMT]; // Format buffer
141
- static char outBuf[512 ]; // Output buffer
142
- static va_list arglist;
143
- static File log_file;
144
-
145
- // Custom log print function.. Prints on pLogView instance
229
+ // Custom log print function.. Prints on _pLogView instance
146
230
void _log_printf (const char *format, ...){
147
231
strncpy (fmtBuf, format, MAX_LOG_FMT);
148
232
fmtBuf[MAX_LOG_FMT - 1 ] = 0 ;
149
233
va_start (arglist, format);
150
234
vsnprintf (outBuf, MAX_LOG_FMT, fmtBuf, arglist);
151
235
va_end (arglist);
152
236
Serial.print (outBuf);
153
- if (pLogView) pLogView ->print (outBuf);
237
+ if (_pLogView) _pLogView ->print (outBuf);
154
238
if (log_file){
155
239
log_file.print (outBuf);
156
240
log_file.flush ();
157
241
}
158
242
}
243
+ // Handle websocket connections
244
+ void wsStatusChange (){
245
+ if (!_pLogView) return ;
246
+ String wsStatus = _pLogView->getVal (" wsStatus" );
247
+ if (wsStatus.startsWith (" Connected:" ))
248
+ _pLogView->sendBuffer ();
249
+ }
159
250
160
251
#endif
0 commit comments