bicycle-statistics: Add logger

Signed-off-by: Thomas Klaehn <thomas.klaehn@u-blox.com>
This commit is contained in:
Thomas Klaehn 2018-07-30 12:22:04 +02:00
parent b4d4d844fd
commit 629794d5f2
2 changed files with 38 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import argparse import argparse
import logging
import sys import sys
import threading import threading
import time import time
@ -8,13 +9,24 @@ from watchdog.observers import Observer
from gpx2html import Gpx2Html from gpx2html import Gpx2Html
from input_observer import InputObserver 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(): def parse_args():
'''Shell argument parser.''' '''Shell argument parser.'''
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('infolder', help='Specify the in folder.') parser.add_argument('infolder', help='Specify the in folder.')
parser.add_argument('outfolder', help='Specify the out 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() return parser.parse_args()
class myLoop(threading.Thread): class myLoop(threading.Thread):
def __init__(self, infolder, outfolder): def __init__(self, infolder, outfolder):
super(myLoop, self).__init__() super(myLoop, self).__init__()
@ -24,6 +36,7 @@ class myLoop(threading.Thread):
self.observer = Observer() self.observer = Observer()
self.in_obs = InputObserver(patterns=["*.gpx"]) self.in_obs = InputObserver(patterns=["*.gpx"])
def run(self): def run(self):
gpx2html = Gpx2Html(self.infolder, self.outfolder) gpx2html = Gpx2Html(self.infolder, self.outfolder)
gpx2html.process() gpx2html.process()
@ -32,9 +45,11 @@ class myLoop(threading.Thread):
self.observer.start() self.observer.start()
while self.run_condition is True: while self.run_condition is True:
new_file = self.in_obs.get_new_file() new_file = self.in_obs.get_new_file()
log.info("new file triggered: {}".format(new_file))
if new_file: if new_file:
gpx2html.process() gpx2html.process()
print "processed" log.info("new file processed: {}".format(new_file))
def stop(self): def stop(self):
self.run_condition = False self.run_condition = False
@ -43,9 +58,29 @@ class myLoop(threading.Thread):
self.observer.join() 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(): def main():
args = parse_args() args = parse_args()
if args.loglevel:
set_log_level(args.loglevel)
my_loop = myLoop(args.infolder, args.outfolder) my_loop = myLoop(args.infolder, args.outfolder)
my_loop.start() my_loop.start()

View File

@ -14,7 +14,8 @@ class InputObserver(PatternMatchingEventHandler):
self.run_condition = True 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.new_filename = event.src_path
self.lock.release() self.lock.release()