2006-05-13 18:07:46 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
2014-02-28 23:12:34 +00:00
|
|
|
# Copyright (C) 2006-2014 Bastian Kleineidam
|
2006-05-13 18:07:46 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2006-05-23 22:04:30 +00:00
|
|
|
"""Logger for aggregator instances"""
|
2006-05-13 18:07:46 +00:00
|
|
|
import threading
|
2012-09-14 20:36:59 +00:00
|
|
|
import thread
|
2008-05-09 06:16:03 +00:00
|
|
|
from ..decorators import synchronized
|
2006-05-13 18:07:46 +00:00
|
|
|
_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Logger (object):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Thread safe multi-logger class used by aggregator instances."""
|
2006-05-13 18:53:41 +00:00
|
|
|
|
2006-05-13 18:07:46 +00:00
|
|
|
def __init__ (self, config):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Initialize basic logging variables."""
|
2012-05-10 19:05:33 +00:00
|
|
|
self.loggers = [config['logger']]
|
|
|
|
|
self.loggers.extend(config['fileoutput'])
|
2006-05-13 18:07:46 +00:00
|
|
|
self.verbose = config["verbose"]
|
|
|
|
|
self.warnings = config["warnings"]
|
|
|
|
|
|
|
|
|
|
def start_log_output (self):
|
|
|
|
|
"""
|
|
|
|
|
Start output of all configured loggers.
|
|
|
|
|
"""
|
2012-05-10 19:05:33 +00:00
|
|
|
for logger in self.loggers:
|
2006-05-13 18:07:46 +00:00
|
|
|
logger.start_output()
|
|
|
|
|
|
2014-03-14 20:06:10 +00:00
|
|
|
def end_log_output (self, downloaded_bytes):
|
2006-05-13 18:07:46 +00:00
|
|
|
"""
|
|
|
|
|
End output of all configured loggers.
|
|
|
|
|
"""
|
2012-05-10 19:05:33 +00:00
|
|
|
for logger in self.loggers:
|
2014-03-14 20:06:10 +00:00
|
|
|
logger.end_output(downloaded_bytes)
|
2006-05-13 18:07:46 +00:00
|
|
|
|
2008-11-30 19:35:56 +00:00
|
|
|
def do_print (self, url_data):
|
|
|
|
|
"""Determine if URL entry should be logged or not."""
|
|
|
|
|
if self.verbose:
|
|
|
|
|
return True
|
2012-09-20 10:44:40 +00:00
|
|
|
if self.warnings and url_data.warnings:
|
2008-11-30 19:35:56 +00:00
|
|
|
return True
|
|
|
|
|
return not url_data.valid
|
|
|
|
|
|
|
|
|
|
@synchronized(_lock)
|
|
|
|
|
def log_url (self, url_data):
|
|
|
|
|
"""Send new url to all configured loggers."""
|
2012-09-14 20:36:59 +00:00
|
|
|
self.check_active_loggers()
|
2008-11-30 19:35:56 +00:00
|
|
|
do_print = self.do_print(url_data)
|
2009-03-06 18:30:58 +00:00
|
|
|
# Only send a transport object to the loggers, not the complete
|
|
|
|
|
# object instance.
|
2012-05-10 19:05:33 +00:00
|
|
|
for log in self.loggers:
|
2014-03-03 22:29:45 +00:00
|
|
|
log.log_filter_url(url_data, do_print)
|
2011-12-14 21:54:26 +00:00
|
|
|
|
|
|
|
|
@synchronized(_lock)
|
|
|
|
|
def log_internal_error (self):
|
|
|
|
|
"""Document that an internal error occurred."""
|
2012-05-10 19:05:33 +00:00
|
|
|
for logger in self.loggers:
|
2011-12-14 21:54:26 +00:00
|
|
|
logger.log_internal_error()
|
2012-09-14 20:36:59 +00:00
|
|
|
|
|
|
|
|
def check_active_loggers(self):
|
|
|
|
|
"""Check if all loggers are deactivated due to I/O errors."""
|
|
|
|
|
for logger in self.loggers:
|
|
|
|
|
if logger.is_active:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
thread.interrupt_main()
|