this commit re-introduces the changes from #418, which were effectively
reverted with d49481097 (merge commit introducing TaskScheduler).
these adjustments are important to guarantee unmangled log messages and
more importantly, to guarantee that all messages from a particular
component are printed to the web console, which most people use to copy
messages from when reporting issues.
* use dynamic memory to allow handling of arbitrary message lenghts.
* keep a message buffer for every task so no task ever mangles the
message of another task.
* every complete line is written to the serial console and moved to
a line buffer for sending them through the websocket.
* the websocket is always fed complete lines.
* make sure to feed only as many lines as possible to the websocket
handler, so that no lines are dropped.
* lock all MessageOutput state against concurrent access.
* respect HardwareSerial buffer size: the MessageOutput class buffers
whole lines of output printed by any task in order to avoid mangling
of text. that means we hand over full lines to the HardwareSerial
instance, which might be too much in one call to write(buffer, size).
we now check the return value of write(buffer, size) and call the
function again with the part of the message that could not yet be
written by HardwareSerial.
40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#pragma once
|
|
|
|
#include <AsyncWebSocket.h>
|
|
#include <TaskSchedulerDeclarations.h>
|
|
#include <Print.h>
|
|
#include <freertos/task.h>
|
|
#include <mutex>
|
|
#include <vector>
|
|
#include <unordered_map>
|
|
#include <queue>
|
|
|
|
class MessageOutputClass : public Print {
|
|
public:
|
|
void init(Scheduler& scheduler);
|
|
size_t write(uint8_t c) override;
|
|
size_t write(const uint8_t* buffer, size_t size) override;
|
|
void register_ws_output(AsyncWebSocket* output);
|
|
|
|
private:
|
|
void loop();
|
|
|
|
Task _loopTask;
|
|
|
|
using message_t = std::vector<uint8_t>;
|
|
|
|
// we keep a buffer for every task and only write complete lines to the
|
|
// serial output and then move them to be pushed through the websocket.
|
|
// this way we prevent mangling of messages from different contexts.
|
|
std::unordered_map<TaskHandle_t, message_t> _task_messages;
|
|
std::queue<message_t> _lines;
|
|
|
|
AsyncWebSocket* _ws = nullptr;
|
|
|
|
std::mutex _msgLock;
|
|
|
|
void serialWrite(message_t const& m);
|
|
};
|
|
|
|
extern MessageOutputClass MessageOutput; |