diff --git a/screenless/bureau/bureau.py b/screenless/bureau/bureau.py index 29256a9..0599187 100644 --- a/screenless/bureau/bureau.py +++ b/screenless/bureau/bureau.py @@ -56,6 +56,19 @@ def add_api(apistr, name=""): return decorator +class LogPrinter(logging.Handler): + """ + LogPrinter prints logs on a receipt printer for screenless debugging. + """ + def emit(self, record): + prn = printer.Usb(0x416, 0x5011, in_ep=0x81, out_ep=0x03) + msg = self.format(record) + text = textwrap.fill(msg, width=48) + text += "\r\n" * 4 + prn.text(text) + prn.cut() + + class Bureau(object): """ Bureau is a base class that implements standard methods for inter-bureau communication, IO, registration and some convenient stuff @@ -85,14 +98,22 @@ class Bureau(object): os.mkdir(basepath) os.chdir(basepath) + self.load_config() + # setup log file - # TODO: make log level globally and bureau-specific via config # TODO: custom handler to print errors to small printer + if hasattr(self.config, "debug"): + if self.config.debug: + log_level = logging.DEBUG + else: + log_level = logging.ERROR + else: + log_level = logging.ERROR logfile = os.path.join(basepath, self.prefix + ".log") - logging.basicConfig(filename=logfile, level=logging.DEBUG) + logging.basicConfig(filename=logfile, level=log_level) self.log = logging.getLogger(self.prefix) - - self.load_config() + log_printer = LogPrinter() + self.log.addHandler(log_printer) # setup a dir to store files and data self.datadir = os.path.join(basepath, self.prefix)