Hi all, I was searching here and in the web too what options could I have to debug a wrover E module that run some threads and asyncio coroutines, but sometimes stop on those when the module is the field without me at the serial console.
Im sure I could easily debug it if I would be there all the time with my computer attached to serial console. As is not always possible, I m looking to an alternative way writing a log file with a dup of the console output.
Any hint on how to achieve this, I mean what library I could use to debug /register all current serial console output?
Thanks in advance,
M
Console logging ( write a file for debug/analysis)
-
- Posts: 20
- Joined: Sun Apr 26, 2020 7:30 am
-
- Posts: 20
- Joined: Sun Apr 26, 2020 7:30 am
Re: Console logging ( write a file for debug/analysis)
something similar to this but on last micropython at ESP32:
https://stackoverflow.com/questions/652 ... icropython
import io, os
class logToFile(io.IOBase):
def __init__(self):
pass
def write(self, data):
with open("logfile.txt", mode="a") as f:
f.write(data)
return len(data)
# Begin loging to file
os.dupterm(logToFile())
# Stop loging to file
os.dupterm(None)
https://stackoverflow.com/questions/652 ... icropython
import io, os
class logToFile(io.IOBase):
def __init__(self):
pass
def write(self, data):
with open("logfile.txt", mode="a") as f:
f.write(data)
return len(data)
# Begin loging to file
os.dupterm(logToFile())
# Stop loging to file
os.dupterm(None)
Re: Console logging ( write a file for debug/analysis)
The suggestion from that SO post is on the right track but it's simpler than this because files themselves are streams, and can be dupterm'ed to. Here's a demo of using this at the REPL (using mpremote to later print the file)kindmartin wrote: ↑Wed Jul 28, 2021 6:07 amsomething similar to this but on last micropython at ESP32:
e.g.
Code: Select all
$ mpr
Connected to MicroPython at /dev/ttyUSB0
Use Ctrl-] to exit this shell
>>> f = open('foo.txt', 'w')
>>> import os
>>> os.dupterm(f)
>>> print('hello')
hello
>>> os.dupterm(None)
<io.TextIOWrapper>
>>>
Code: Select all
$ mpr fs cat foo.txt
cat :foo.txt
>>> print('hello')
hello
>>> os.dupterm(None)