forked from hoylabs/OpenDTU-OnBattery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redirect ESP log messages into virtual debug console
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <stdarg.h> | ||
#include <TaskSchedulerDeclarations.h> | ||
#include <mutex> | ||
|
||
#define BUFFER_SIZE 500 | ||
|
||
class ESPLogClass { | ||
public: | ||
ESPLogClass(); | ||
void init(Scheduler& scheduler); | ||
int vprintf(const char *format, va_list ap); | ||
|
||
private: | ||
void loop(); | ||
|
||
Task _loopTask; | ||
|
||
uint8_t _buffer[BUFFER_SIZE]; | ||
uint16_t _buff_pos = 0; | ||
std::mutex _lock; | ||
}; | ||
|
||
extern ESPLogClass ESPLog; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Copyright (C) 2022-2024 Thomas Basler and others | ||
*/ | ||
#include "ESPLog.h" | ||
#include "MessageOutput.h" | ||
|
||
#include <Arduino.h> | ||
|
||
ESPLogClass ESPLog; | ||
|
||
namespace { | ||
|
||
static int extern "C" wrap_vprintf(const char *format, va_list ap) | ||
{ | ||
return ESPLog.vprintf(format, ap); | ||
} | ||
|
||
}; // namespace | ||
|
||
ESPLogClass::ESPLogClass() | ||
: _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&ESPLogClass::loop, this)) | ||
{ | ||
} | ||
|
||
void ESPLogClass::init(Scheduler& scheduler) | ||
{ | ||
scheduler.addTask(_loopTask); | ||
_loopTask.enable(); | ||
esp_log_set_vprintf(wrap_vprintf); | ||
} | ||
|
||
void ESPLogClass::loop() | ||
{ | ||
_lock.lock(); | ||
int front = 0; | ||
int used = _buff_pos; | ||
while (used > front) { | ||
_lock.unlock(); | ||
MessageOutput.write(&_buffer[front], used); | ||
_lock.lock(); | ||
|
||
front = used; | ||
used = _buff_pos; | ||
}; | ||
_buff_pos = 0; | ||
_lock.unlock(); | ||
} | ||
|
||
int ESPLogClass::vprintf(const char *format, va_list ap) | ||
{ | ||
std::lock_guard<std::mutex> lock(_lock); | ||
char *buf = reinterpret_cast<char*>(&_buffer[_buff_pos]); | ||
size_t remain = sizeof(_buffer) - _buff_pos; | ||
|
||
if (remain == 0) { | ||
return 0; | ||
} | ||
|
||
int printed = vsnprintf(buf, remain, format, ap); | ||
_buff_pos += printed; | ||
return printed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters