Console logging ( write a file for debug/analysis)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
kindmartin
Posts: 20
Joined: Sun Apr 26, 2020 7:30 am

Console logging ( write a file for debug/analysis)

Post by kindmartin » Wed Jul 28, 2021 4:34 am

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

kindmartin
Posts: 20
Joined: Sun Apr 26, 2020 7:30 am

Re: Console logging ( write a file for debug/analysis)

Post by kindmartin » Wed Jul 28, 2021 6:07 am

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)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Console logging ( write a file for debug/analysis)

Post by jimmo » Wed Aug 04, 2021 5:16 am

kindmartin wrote:
Wed Jul 28, 2021 6:07 am
something similar to this but on last micropython at ESP32:
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)

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)
This is going to become even simpler soon as you'll be able to modify sys.stdout (like in regular Python) -- see https://github.com/micropython/micropython/pull/7591

Post Reply