Skip to content

Instantly share code, notes, and snippets.

@jimmo
Created November 6, 2020 02:25
Show Gist options
  • Save jimmo/a96f8cde24013c63e1355615c85e25dd to your computer and use it in GitHub Desktop.
Save jimmo/a96f8cde24013c63e1355615c85e25dd to your computer and use it in GitHub Desktop.
import os
import io
MP_EWOULDBLOCK = const(11)
class LoggingStream(io.IOBase):
def __init__(self, path):
self._f = open(path, "a")
def read(self, sz=None):
return None
def readinto(self, buf):
return -MP_EWOULDBLOCK
def ioctl(self, op, arg):
return 0
def write(self, buf):
self._f.write(buf)
# optionally add:
# self._f.flush()
# if you don't expect to be able to call disable_logging
# but this will slow down your program considerably
def close(self):
self._f.close()
stream = None
def enable_logging(path):
global stream
stream = LoggingStream(path)
os.dupterm(stream)
def disable_logging():
os.dupterm(None)
stream.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment