From 7eb9572c25190ab4079374756eb75319d6497f74 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Sat, 11 Apr 2009 16:27:54 +0000 Subject: [PATCH] add a logger to DeFuzz --- deefuzz-deamon => deefuzz-deamon.sh | 2 +- deefuzz.py | 33 ++++++++++++++++------------- example/myfuzz.xml | 1 + example/test_mp3.xml | 1 + install.py | 1 + tools/__init__.py | 1 + tools/logger.py | 19 +++++++++++++++++ 7 files changed, 42 insertions(+), 16 deletions(-) rename deefuzz-deamon => deefuzz-deamon.sh (79%) create mode 100644 tools/logger.py diff --git a/deefuzz-deamon b/deefuzz-deamon.sh similarity index 79% rename from deefuzz-deamon rename to deefuzz-deamon.sh index 6d1c0cd..0406cff 100644 --- a/deefuzz-deamon +++ b/deefuzz-deamon.sh @@ -5,6 +5,6 @@ log_file=/tmp/deefuzz.log ulimit -c unlimited while true; do - deefuzz $1 >> $log_file + deefuzz $1 > /dev/null sleep 3 done diff --git a/deefuzz.py b/deefuzz.py index 6c82a91..476ec31 100755 --- a/deefuzz.py +++ b/deefuzz.py @@ -107,16 +107,13 @@ class DeeFuzz: dict = xmltodict(conf_xml,'utf-8') return dict - def get_station_names(self): - return self.conf['station']['name'] - def run(self): if isinstance(self.conf['deefuzz']['station'], dict): # Fix wrong type data from xmltodict when one station (*) nb_stations = 1 else: nb_stations = len(self.conf['deefuzz']['station']) - print 'Number of stations : ' + str(nb_stations) + #print 'Number of stations : ' + str(nb_stations) # Create a Queue q = Queue.Queue(1) @@ -127,21 +124,23 @@ class DeeFuzz: # Define the buffer_size self.buffer_size = 65536/nb_stations - print 'Buffer size per station = ' + str(self.buffer_size) + #print 'Buffer size per station = ' + str(self.buffer_size) + + # Set the deefuzz logger + self.logger = Logger(self.conf['deefuzz']['log']) - # Start the stations + # Start the Stations s = [] for i in range(0,nb_stations): if isinstance(self.conf['deefuzz']['station'], dict): station = self.conf['deefuzz']['station'] else: station = self.conf['deefuzz']['station'][i] - name = station['infos']['name'] + # Create a Station - s.append(Station(station, q, self.buffer_size)) + s.append(Station(station, q, self.buffer_size, self.logger)) for i in range(0,nb_stations): - # Start the Stations s[i].start() @@ -164,11 +163,12 @@ class Producer(Thread): class Station(Thread): """a DeeFuzz shouting station thread""" - def __init__(self, station, q, buffer_size): + def __init__(self, station, q, buffer_size, logger): Thread.__init__(self) self.station = station self.q = q self.buffer_size = buffer_size + self.logger = logger self.channel = shout.Shout() self.id = 999999 self.counter = 0 @@ -209,8 +209,12 @@ class Station(Thread): #print self.playlist self.lp = len(self.playlist) self.channel.open() - print 'Opening ' + self.short_name + ' - ' + self.channel.name + \ - ' (' + str(self.lp) + ' tracks)...' + + # Logging + self.logger.write('DeeFuzz v' + version) + self.logger.write('Using libshout version %s' % shout.version()) + self.logger.write('Opening ' + self.short_name + ' - ' + self.channel.name + \ + ' (' + str(self.lp) + ' tracks)...') def update_rss(self, media_list, rss_file): rss_item_list = [] @@ -367,7 +371,8 @@ class Station(Thread): self.channel.set_metadata({'song': str(title)}) self.update_rss(self.current_media_obj, self.rss_current_file) file_name, file_title, file_ext = self.get_file_info(media) - print 'DeeFuzzing this file on %s : id = %s, name = %s' % (self.short_name, self.id, file_name) + self.logger.write('DeeFuzzing this file on %s : id = %s, name = %s' \ + % (self.short_name, self.id, file_name)) stream = self.core_process_read(media) q.task_done() @@ -383,8 +388,6 @@ class Station(Thread): def main(): if len(sys.argv) == 2: - print "DeeFuzz v"+version - print "Using libshout version %s" % shout.version() d = DeeFuzz(sys.argv[1]) d.run() else: diff --git a/example/myfuzz.xml b/example/myfuzz.xml index 8cd3e4f..266cdbc 100644 --- a/example/myfuzz.xml +++ b/example/myfuzz.xml @@ -1,4 +1,5 @@ + /tmp/deefuzz.log My_Station_1 diff --git a/example/test_mp3.xml b/example/test_mp3.xml index f6b759f..7a5e0b4 100644 --- a/example/test_mp3.xml +++ b/example/test_mp3.xml @@ -1,4 +1,5 @@ + /tmp/deefuzz.log My_Station_1 diff --git a/install.py b/install.py index c211ebe..e09bdfa 100644 --- a/install.py +++ b/install.py @@ -57,6 +57,7 @@ if os.path.exists('/usr/bin/deefuzz'): os.system('rm -r /usr/bin/deefuzz') os.system('ln -s '+install_dir+os.sep+'deefuzz.py '+'/usr/bin/deefuzz') +os.system('ln -s '+install_dir+os.sep+'deefuzz-deamon.sh '+'/usr/bin/deefuzz-deamon') print """ Installation successfull ! diff --git a/tools/__init__.py b/tools/__init__.py index 89f1866..3f9d101 100644 --- a/tools/__init__.py +++ b/tools/__init__.py @@ -3,3 +3,4 @@ from xmltodict import * from PyRSS2Gen import * from mp3 import * from ogg import * +from logger import * \ No newline at end of file diff --git a/tools/logger.py b/tools/logger.py new file mode 100644 index 0000000..65a386f --- /dev/null +++ b/tools/logger.py @@ -0,0 +1,19 @@ +#!/usr/bin/python + +import logging + +class Logger: + + def __init__(self, file): + self.logger = logging.getLogger('myapp') + self.hdlr = logging.FileHandler(file) + self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') + self.hdlr.setFormatter(self.formatter) + self.logger.addHandler(self.hdlr) + self.logger.setLevel(logging.INFO) + + def write(self, message): + self.logger.info(message) + + + \ No newline at end of file -- 2.39.5