set up per-bureau logging. get rid of print statements

workspace
Brendan Howell 8 years ago
parent 81fb54c14d
commit ffe60c5540

@ -3,6 +3,7 @@ import configparser
import functools
import inspect
import json
import logging
import os.path
import subprocess
import tempfile
@ -85,6 +86,12 @@ class Bureau(object):
os.mkdir(basepath)
os.chdir(basepath)
# setup log file
# TODO: make log level globally and bureau-specific via config
logfile = os.path.join(basepath, self.prefix + ".log")
logging.basicConfig(filename=logfile, level=logging.DEBUG)
self.log = logging.getLogger(self.prefix)
self.config = configparser.ConfigParser()
self.load_config()
@ -101,9 +108,9 @@ class Bureau(object):
self.context = zmq.Context()
self._recv = self.context.socket(zmq.REP)
self._recv.bind("ipc://" + self.prefix + ".ipc")
print(("bureau " + self.name + " waiting for messages"))
print("commands: ")
print(self.commands)
self.log.debug("bureau " + self.name + " waiting for messages")
self.log.debug("commands: ")
self.log.debug(self.commands)
def load_config(self):
"""
@ -152,7 +159,8 @@ class Bureau(object):
# TODO: this may need some better error handling
return resp
else:
print("message sent... timed out after 10 seconds.")
self.log.warning("message" + message +
" sent... timed out after 10 seconds.")
return None
def _publish_methods(self):
@ -201,9 +209,9 @@ class Bureau(object):
else:
self.send("IR", "addapi", api_detail)
print("registered:")
print(self.commands)
print(self.api)
self.log.debug("registered:")
self.log.debug(self.commands)
self.log.debug(self.api)
def print_full(self, template, **kwargs):
"""print a full page (A4) document """
@ -215,7 +223,7 @@ class Bureau(object):
htmlfile = os.fdopen(htmlfile, "w")
# run template with kwargs
templfile = os.path.join(self.mdir, template)
print("using template: ", templfile)
self.log.debug("printing with template: ", templfile)
templ = Template(filename=templfile)
htmlfile.write(templ.render_unicode(**kwargs))
htmlfile.close()
@ -232,7 +240,7 @@ class Bureau(object):
# TODO: make paper size a config variable
pdfpath = tempfile.mkstemp(".pdf")[1]
print("rendering with: ", self.html2pdf + htmlpath + " " + pdfpath)
self.log.debug("rendering with: ", self.html2pdf + htmlpath + " " + pdfpath)
subprocess.call(self.html2pdf + htmlpath + " " + pdfpath +
" A4 1920px", shell=True)
subprocess.call("lpr " + pdfpath, shell=True)
@ -306,22 +314,22 @@ class Bureau(object):
time.sleep(0.05) # don't waste CPU
continue
try:
print("got message:", msg)
self.log.debug("got message:", msg)
dot = msg.find(".")
ref = msg[:dot]
if (dot < len(msg) - 1) and (dot > 0):
print("msg length:", len(msg))
print("dot at", dot)
self.log.debug("msg length:", len(msg))
self.log.debug("dot at", dot)
# TODO: maybe trim off the trailing "." for convenience
data = msg[dot + 1:]
# data = str(data) # force to be a string
else:
data = None
print("data: " + str(data))
self.log.debug("data: " + str(data))
except IndexError as err:
print("invalid message: ", err)
self.log.warning("invalid message: ", err)
continue
print(("got method: " + ref))
self.log.debug(("got method: " + ref))
if (ref in self.commands) or (ref in self.api):
# catch TypeErrors for case of bogus params
@ -346,12 +354,12 @@ class Bureau(object):
# print("invalid data for command '{}': {}".format(ref, data))
# self._recv.send_unicode("Error. Invalid or missing data.")
except KeyError as err:
print(err)
print("You are calling a command as an API or vice-versa.")
self.log.warning(err)
self.log.warning("You are calling a command as an API or vice-versa.")
self._recv.send_unicode(
"Error. Command called as API or API as command.")
else:
print("error! Command/API %s not found", ref)
self.log.warning("error! Command/API %s not found", ref)
self._recv.send_unicode("Error! Command/API not found.")

Loading…
Cancel
Save