diff --git a/bicycle_statistics/__main__.py b/bicycle_statistics/__main__.py index e661c61..9a0811b 100644 --- a/bicycle_statistics/__main__.py +++ b/bicycle_statistics/__main__.py @@ -1,4 +1,5 @@ import argparse +import logging import sys import threading import time @@ -8,13 +9,24 @@ from watchdog.observers import Observer from gpx2html import Gpx2Html from input_observer import InputObserver +log_level = logging.INFO +LOG_FILE = "/var/log/bicycle-statistics.log" +LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" + +logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE) +log = logging.getLogger('bicycle-statistics') + def parse_args(): '''Shell argument parser.''' parser = argparse.ArgumentParser() parser.add_argument('infolder', help='Specify the in folder.') parser.add_argument('outfolder', help='Specify the out folder.') + parser.add_argument('-l', '--loglevel', help='Set log level to one of \ + "CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET". Default is \ + "INFO".') return parser.parse_args() + class myLoop(threading.Thread): def __init__(self, infolder, outfolder): super(myLoop, self).__init__() @@ -24,6 +36,7 @@ class myLoop(threading.Thread): self.observer = Observer() self.in_obs = InputObserver(patterns=["*.gpx"]) + def run(self): gpx2html = Gpx2Html(self.infolder, self.outfolder) gpx2html.process() @@ -32,9 +45,11 @@ class myLoop(threading.Thread): self.observer.start() while self.run_condition is True: new_file = self.in_obs.get_new_file() + log.info("new file triggered: {}".format(new_file)) if new_file: gpx2html.process() - print "processed" + log.info("new file processed: {}".format(new_file)) + def stop(self): self.run_condition = False @@ -43,9 +58,29 @@ class myLoop(threading.Thread): self.observer.join() +def set_log_level(level): + global log_level + if level == 'CRITICAL': + log_level = logging.CRITICAL + if level == 'ERROR': + log_level = logging.ERROR + if level == 'WARNING': + log_level = logging.WARNING + if level == 'INFO': + log_level = logging.INFO + if level == 'DEBUG': + log_level = logging.DEBUG + if level == 'NOTSET': + log_level = logging.NOTSET + log.setLevel(level=log_level) + + def main(): args = parse_args() + if args.loglevel: + set_log_level(args.loglevel) + my_loop = myLoop(args.infolder, args.outfolder) my_loop.start() diff --git a/input_observer/__init__.py b/input_observer/__init__.py index 5e9438b..b645b41 100644 --- a/input_observer/__init__.py +++ b/input_observer/__init__.py @@ -14,7 +14,8 @@ class InputObserver(PatternMatchingEventHandler): self.run_condition = True - def on_created(self, event): +# def on_created(self, event): + def on_any_event(self, event): self.new_filename = event.src_path self.lock.release()