Log entire REPL prompt to SD

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kbrenner
Posts: 31
Joined: Mon Jan 20, 2020 8:05 pm

Log entire REPL prompt to SD

Post by kbrenner » Tue Nov 03, 2020 2:10 pm

Is there a way to log the entire REPL prompt to SD? I want to log the entire prompt rather than just an individual event/error. I am using the PYBD-SF6W with an SD card.

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

Re: Log entire REPL prompt to SD

Post by jimmo » Tue Nov 03, 2020 10:27 pm

Can you explain what you mean by "log the entire REPL prompt".

If what you mean is something like: create a text file, the contents of which look exactly like the interactive REPL session, then yes you can do this with dupterm.

I haven't tested this, but the rough idea is to make a stream that you pass to os.dupterm, and every time MicroPython writes to stdout, it also will be sent to your stream. See https://github.com/micropython/micropyt ... rt_repl.py for an example of how we use this to make a REPL over BLE.

This is untested, but this is a sketch of a stream that only handles writes and could log all the writes to a file, and how to register it with dupterm.

Code: Select all

import os

class LoggingStream(io.IOBase):
    def __init__(self, path):
        # open file

    def read(self, sz=None):
        return None

    def readinto(self, buf):
        return 0

    def ioctl(self, op, arg):
        return 0

    def write(self, buf):
        # write buf to file

os.dupterm(LoggingStream(path))

kbrenner
Posts: 31
Joined: Mon Jan 20, 2020 8:05 pm

Re: Log entire REPL prompt to SD

Post by kbrenner » Wed Nov 04, 2020 7:42 pm

Thank you providing this information. I am a bit novice with respect to micropython, so I was wondering if you might be able to show me how this could be implemented? I read through the link you attached, but I'm not sure how much of that code would translate to my issue.

kbrenner
Posts: 31
Joined: Mon Jan 20, 2020 8:05 pm

Re: Log entire REPL prompt to SD

Post by kbrenner » Wed Nov 04, 2020 10:34 pm

Basically, I am experiencing errors with my device (which is being used to control a couple of Dynamixel motors) exclusively when I am running from an external power supply and not connected to my PC via the microUSB port. So when an error/failure occurs, I cannot view it in real time since I cannot see the REPL prompt. So, I want to record/store on SD everything that would have been printed to the REPL prompt had I been connected via microUSB to the PC.

kbrenner
Posts: 31
Joined: Mon Jan 20, 2020 8:05 pm

Re: Log entire REPL prompt to SD

Post by kbrenner » Wed Nov 04, 2020 11:08 pm

Is there the ability to view the REPL prompt wirelessly on the PYBD-SF6W? I am noticing that an error gets returned on my PYBD-SF6W saying that os module has no module dupterm. I've tried both os and uos. I noticed that others had a similar issue on ESP32 boards: viewtopic.php?t=3997.

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

Re: Log entire REPL prompt to SD

Post by jimmo » Fri Nov 06, 2020 2:28 am

kbrenner wrote:
Wed Nov 04, 2020 11:08 pm
Is there the ability to view the REPL prompt wirelessly on the PYBD-SF6W?
With WebREPL, yes.
kbrenner wrote:
Wed Nov 04, 2020 11:08 pm
I am noticing that an error gets returned on my PYBD-SF6W saying that os module has no module dupterm. I've tried both os and uos. I noticed that others had a similar issue on ESP32 boards: viewtopic.php?t=3997.
That's weird. Can you check what firmware version you have?

If you connect to the REPL, you should see something like (except likely a different build of v1.13).

Code: Select all

MPY: sync filesystems
MPY: soft reboot
MicroPython v1.13-160-ga9ed9e58d-dirty on 2020-11-04; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>> import os
>>> os.dupterm(None)
kbrenner wrote:
Wed Nov 04, 2020 7:42 pm
Basically, I am experiencing errors with my device (which is being used to control a couple of Dynamixel motors) exclusively when I am running from an external power supply and not connected to my PC via the microUSB port. So when an error/failure occurs, I cannot view it in real time since I cannot see the REPL prompt. So, I want to record/store on SD everything that would have been printed to the REPL prompt had I been connected via microUSB to the PC.
Yeah, this sounds like a good idea and definitely should be possible.

The other simpler option is just to use try/except handlers and write any exception details directly to a file:

Code: Select all

try:
  ...
except ...:
  with open('error.log', 'w') as f:
    f.write(...)
kbrenner wrote:
Wed Nov 04, 2020 7:42 pm
Thank you providing this information. I am a bit novice with respect to micropython, so I was wondering if you might be able to show me how this could be implemented? I read through the link you attached, but I'm not sure how much of that code would translate to my issue.
Here's a full example: https://gist.github.com/jimmo/a96f8cde2 ... 15c85e25dd

Code: Select all

>>> 
MPY: sync filesystems
MPY: soft reboot
MicroPython v1.13-160-ga9ed9e58d-dirty on 2020-11-04; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>> 1+1
2
>>> import log_sd
>>> 2+2
4
>>> log_sd.enable_logging('stdout.log')
>>> 3+3
6
>>> print('hello')
hello
>>> log_sd.disable_logging()
>>> 
And then if I inspect the contents of stdout.log I see just the output after the call to enable_logging.

Code: Select all

$ pyboard --device /dev/ttyACM0 -f cat stdout.log
cat :stdout.log
>>> 3+3
6
>>> print('hello')
hello
>>> log_sd.disable_logging()

User avatar
dhylands
Posts: 3516
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Log entire REPL prompt to SD

Post by dhylands » Fri Nov 06, 2020 4:50 am

What I normally do in a scenario like this is to log to a UART, which can also be done by using os.dupterm

kbrenner
Posts: 31
Joined: Mon Jan 20, 2020 8:05 pm

Re: Log entire REPL prompt to SD

Post by kbrenner » Fri Nov 06, 2020 8:22 pm

Thanks. I'm able to access the REPL prompt from WebREPL and write various python commands that I then see show up on the local REPL prompt; however, I do not see it working in the opposite direction. For example, I have a main script that looks like:

import network
ap = network.WLAN(network.AP_IF)
ap.active(True)

count = 0
while(False):
count = count + 1
if count == 100000:
print('hello world')
count = 0

However, I do not see the print outs of 'hello world' on the WebREPL. I do see them on my local REPL.

Also, I am running v1.12.

Additionally, I have written try/except handlers; however, occasionally the Pyboard will show errors in the REPL prompt that don't come from the try/except such as:

failed 0 3 4 c (or some other combination of numbers/letters)
some memoryview error

Post Reply