From: Cameron Patrick Date: Sun, 1 Aug 2004 08:14:34 +0000 (+0000) Subject: do even more logging X-Git-Tag: IMPORT-FROM-UCCCVS~78 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=17c75c889244e81d80a3a4f8aeeb8348c5cb7b71;p=uccvend-vendserver.git do even more logging --- diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index 4d9aae2..a09ac2a 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -25,6 +25,8 @@ DOOR = 1 SWITCH = 2 KEY = 3 +class DispenseDatabaseException(Exception): pass + class DispenseDatabase: def __init__(self, vending_machine, host, name, user, password): self.vending_machine = vending_machine @@ -32,16 +34,15 @@ class DispenseDatabase: self.db.query('LISTEN vend_requests') def process_requests(self): - print 'processing' + logging.debug('database processing') query = 'SELECT request_id, request_slot FROM vend_requests WHERE request_handled = false' try: outstanding = self.db.query(query).getresult() except (pg.error,), db_err: - sys.stderr.write('Failed to query database: %s\n'%(db_err.strip())) - return + raise DispenseDatabaseException('Failed to query database: %s\n'%(db_err.strip())) for (id, slot) in outstanding: (worked, code, string) = self.vending_machine.vend(slot) - print (worked, code, string) + logging.debug (str((worked, code, string))) if worked: query = 'SELECT vend_success(%s)'%id self.db.query(query).getresult() @@ -115,7 +116,7 @@ def verify_user_pin(uid, pin): return None def door_open_mode(v): - print "Entering open door mode" + logging.warning("Entering open door mode") v.display("-FEED ME-") while True: e = v.next_event() @@ -123,6 +124,7 @@ def door_open_mode(v): (event, params) = e if event == DOOR: if params == 1: # door closed + logging.warning('Leaving open door mode') v.display("-YUM YUM!-") sleep(1) return @@ -202,7 +204,7 @@ class MessageKeeper: def run_forever(rfh, wfh, options, cf): v = VendingMachine(rfh, wfh) - print 'PING is', v.ping() + logging.debug('PING is', v.ping()) if USE_DB: db = DispenseDatabase(v, cf.DBServer, cf.DBName, cf.DBUser, cf.DBPassword) cur_user = '' @@ -219,7 +221,11 @@ def run_forever(rfh, wfh, options, cf): last_timeout_refresh = None while True: - if USE_DB: db.handle_events() + if USE_DB: + try: + db.handle_events() + except DispenseDatabaseException, e: + logging.error('Database error: '+str(e)) if time_to_autologout != None: time_left = time_to_autologout - time() @@ -254,7 +260,7 @@ def run_forever(rfh, wfh, options, cf): continue time_to_idle = None (event, params) = e - print e + logging.debug('Got event: ' + repr(e)) if event == DOOR: if params == 0: door_open_mode(v); @@ -396,6 +402,7 @@ def parse_args(): op.add_option('-s', '--syslog', dest='syslog', action='store_true', default=False, help='log output to syslog') op.add_option('-d', '--daemon', dest='daemon', action='store_true', default=False, help='run as a daemon') op.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False, help='spit out lots of debug output') + op.add_option('-q', '--quiet', dest='quiet', action='store_true', default=False, help='only report errors') op.add_option('--pid-file', dest='pid_file', metavar='FILE', default='', help='store daemon\'s pid in the given file') options, args = op.parse_args() @@ -430,7 +437,8 @@ class VendConfigFile: self.__dict__[option] = value except ConfigParser.Error, e: - print "Error reading config file "+config_file+": " + str(e) + logging.critical("Error reading config file "+config_file+": " + str(e)) + logging.critical("Bailing out") sys.exit(1) def create_pid_file(name): @@ -450,9 +458,9 @@ def set_stuff_up(): signal.signal(signal.SIGINT, stop_server) options = parse_args() + set_up_logging(options) config_opts = VendConfigFile(options.config_file, config_options) if options.daemon: become_daemon() - set_up_logging(options) if options.pid_file != '': create_pid_file(options.pid_file) return options, config_opts @@ -483,8 +491,10 @@ def set_up_logging(options): sys_logger = logging.handlers.SysLogHandler('/dev/log', 'daemon') sys_logger.setFormatter(logging.Formatter('vendserver[%d]'%(os.getpid()) + ' %(levelname)s: %(message)s')) logger.addHandler(sys_logger) - - if options.verbose: + + if options.quiet: + logger.setLevel(logging.WARNING) + elif options.verbose: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO)