2014-01-08 21:33:04 +00:00
|
|
|
# Copyright (C) 2000-2014 Bastian Kleineidam
|
2004-07-07 18:01:25 +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.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
|
|
|
|
Output logging support for different formats.
|
|
|
|
|
"""
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2004-08-25 21:32:31 +00:00
|
|
|
import sys
|
2004-10-27 12:22:10 +00:00
|
|
|
import os
|
2004-11-25 16:20:58 +00:00
|
|
|
import datetime
|
2010-11-01 08:58:03 +00:00
|
|
|
import time
|
2010-11-21 19:19:27 +00:00
|
|
|
import codecs
|
2012-06-19 21:27:26 +00:00
|
|
|
import abc
|
2020-05-19 18:56:42 +00:00
|
|
|
|
2010-11-21 19:19:27 +00:00
|
|
|
from .. import log, LOG_CHECK, strformat, dummy, configuration, i18n
|
2004-11-30 14:09:42 +00:00
|
|
|
|
2020-05-30 16:01:36 +00:00
|
|
|
_ = lambda x: x # noqa: E731
|
2004-10-27 19:42:56 +00:00
|
|
|
Fields = dict(
|
2004-10-27 19:31:09 +00:00
|
|
|
realurl=_("Real URL"),
|
|
|
|
|
cachekey=_("Cache key"),
|
|
|
|
|
result=_("Result"),
|
|
|
|
|
base=_("Base"),
|
|
|
|
|
name=_("Name"),
|
|
|
|
|
parenturl=_("Parent URL"),
|
|
|
|
|
extern=_("Extern"),
|
|
|
|
|
info=_("Info"),
|
|
|
|
|
warning=_("Warning"),
|
2010-11-05 11:53:57 +00:00
|
|
|
dltime=_("D/L time"),
|
2009-02-20 13:03:09 +00:00
|
|
|
dlsize=_("Size"),
|
2010-11-05 11:53:57 +00:00
|
|
|
checktime=_("Check time"),
|
2004-10-27 19:31:09 +00:00
|
|
|
url=_("URL"),
|
2011-04-09 08:51:03 +00:00
|
|
|
level=_("Level"),
|
2012-09-18 10:12:00 +00:00
|
|
|
modified=_("Modified"),
|
2004-10-27 19:27:27 +00:00
|
|
|
)
|
2004-10-27 19:31:09 +00:00
|
|
|
del _
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2020-05-30 16:01:36 +00:00
|
|
|
ContentTypes = dict(image=0, text=0, video=0, audio=0, application=0, mail=0, other=0)
|
2010-03-13 07:47:12 +00:00
|
|
|
|
2011-03-11 19:05:27 +00:00
|
|
|
|
2020-04-30 18:57:47 +00:00
|
|
|
class LogStatistics:
|
2010-12-14 19:30:53 +00:00
|
|
|
"""Gather log statistics:
|
|
|
|
|
- number of errors, warnings and valid links
|
2010-12-15 06:55:00 +00:00
|
|
|
- type of contents (image, video, audio, text, ...)
|
|
|
|
|
- URL lengths
|
2010-12-14 19:30:53 +00:00
|
|
|
"""
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def __init__(self):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Initialize log statistics."""
|
2010-12-20 23:35:07 +00:00
|
|
|
self.reset()
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def reset(self):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Reset all log statistics to default values."""
|
2011-12-14 21:54:26 +00:00
|
|
|
# number of logged URLs
|
2010-12-14 19:30:53 +00:00
|
|
|
self.number = 0
|
2011-12-14 21:54:26 +00:00
|
|
|
# number of encountered URL errors
|
2010-12-14 19:30:53 +00:00
|
|
|
self.errors = 0
|
2011-12-14 21:54:26 +00:00
|
|
|
# number of URL errors that were printed
|
2010-12-14 19:30:53 +00:00
|
|
|
self.errors_printed = 0
|
2011-12-14 21:54:26 +00:00
|
|
|
# number of URL warnings
|
2010-12-14 19:30:53 +00:00
|
|
|
self.warnings = 0
|
2011-12-14 21:54:26 +00:00
|
|
|
# number of URL warnings that were printed
|
2010-12-14 19:30:53 +00:00
|
|
|
self.warnings_printed = 0
|
2011-12-14 21:54:26 +00:00
|
|
|
# number of internal errors
|
|
|
|
|
self.internal_errors = 0
|
2012-09-17 13:23:25 +00:00
|
|
|
# link types
|
2010-12-17 19:25:06 +00:00
|
|
|
self.link_types = ContentTypes.copy()
|
2012-09-17 13:23:25 +00:00
|
|
|
# URL length statistics
|
2010-12-15 06:55:00 +00:00
|
|
|
self.max_url_length = 0
|
|
|
|
|
self.min_url_length = 0
|
|
|
|
|
self.avg_url_length = 0.0
|
|
|
|
|
self.avg_number = 0
|
2014-03-14 20:06:10 +00:00
|
|
|
# overall downloaded bytes
|
|
|
|
|
self.downloaded_bytes = None
|
2010-12-14 19:30:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def log_url(self, url_data, do_print):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Log URL statistics."""
|
2010-12-14 19:30:53 +00:00
|
|
|
self.number += 1
|
|
|
|
|
if not url_data.valid:
|
|
|
|
|
self.errors += 1
|
|
|
|
|
if do_print:
|
|
|
|
|
self.errors_printed += 1
|
|
|
|
|
num_warnings = len(url_data.warnings)
|
|
|
|
|
self.warnings += num_warnings
|
|
|
|
|
if do_print:
|
|
|
|
|
self.warnings_printed += num_warnings
|
|
|
|
|
if url_data.content_type:
|
|
|
|
|
key = url_data.content_type.split('/', 1)[0].lower()
|
|
|
|
|
if key not in self.link_types:
|
|
|
|
|
key = "other"
|
2020-04-30 19:11:59 +00:00
|
|
|
elif url_data.url.startswith("mailto:"):
|
2010-12-15 06:55:00 +00:00
|
|
|
key = "mail"
|
2010-12-14 19:30:53 +00:00
|
|
|
else:
|
2010-12-15 06:55:00 +00:00
|
|
|
key = "other"
|
2010-12-14 19:30:53 +00:00
|
|
|
self.link_types[key] += 1
|
2010-12-15 06:55:00 +00:00
|
|
|
if url_data.url:
|
2020-05-30 16:01:36 +00:00
|
|
|
n = len(url_data.url)
|
|
|
|
|
self.max_url_length = max(n, self.max_url_length)
|
2010-12-15 06:55:00 +00:00
|
|
|
if self.min_url_length == 0:
|
2020-05-30 16:01:36 +00:00
|
|
|
self.min_url_length = n
|
2010-12-15 06:55:00 +00:00
|
|
|
else:
|
2020-05-30 16:01:36 +00:00
|
|
|
self.min_url_length = min(n, self.min_url_length)
|
2010-12-15 06:55:00 +00:00
|
|
|
# track average number separately since empty URLs do not count
|
|
|
|
|
self.avg_number += 1
|
|
|
|
|
# calculate running average
|
2020-05-30 16:01:36 +00:00
|
|
|
self.avg_url_length += (n - self.avg_url_length) / self.avg_number
|
2010-12-14 19:30:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def log_internal_error(self):
|
2011-12-14 21:54:26 +00:00
|
|
|
"""Increase internal error count."""
|
|
|
|
|
self.internal_errors += 1
|
|
|
|
|
|
2010-12-14 19:30:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
class _Logger(abc.ABC):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2009-03-06 18:30:58 +00:00
|
|
|
Base class for logging of checked urls. It defines the public API
|
|
|
|
|
(see below) and offers basic functionality for all loggers.
|
|
|
|
|
|
2011-03-09 11:08:03 +00:00
|
|
|
Each logger offers the following functions:
|
2009-03-06 18:30:58 +00:00
|
|
|
|
|
|
|
|
* start_output()
|
|
|
|
|
Initialize and start log output. Most loggers print a comment
|
|
|
|
|
with copyright information.
|
2014-04-30 17:59:43 +00:00
|
|
|
* end_output(**kwargs)
|
2009-03-06 18:30:58 +00:00
|
|
|
Finish log output, possibly flushing buffers. Most loggers also
|
|
|
|
|
print some statistics.
|
2014-04-30 17:59:43 +00:00
|
|
|
Custom keyword arguments can be given for different loggers.
|
2009-03-06 18:30:58 +00:00
|
|
|
* log_filter_url(url_data, do_print)
|
|
|
|
|
Log a checked URL. The url_data object is a transport form of
|
|
|
|
|
the UrlData class. The do_print flag indicates if this URL
|
|
|
|
|
should be logged or just used to update internal statistics.
|
2011-03-09 11:08:03 +00:00
|
|
|
|
|
|
|
|
Each subclassed logger must implement the following functions:
|
|
|
|
|
|
|
|
|
|
* start_output()
|
|
|
|
|
Also call the base class implementation of this.
|
2014-04-30 17:59:43 +00:00
|
|
|
* end_output(**kwargs)
|
2011-03-09 11:08:03 +00:00
|
|
|
See above.
|
|
|
|
|
* log_url(url_data)
|
|
|
|
|
Log a checked URL. Called by log_filter_url if do_print is True.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2013-12-11 17:41:55 +00:00
|
|
|
# A lowercase name for this logger, usable for option values
|
|
|
|
|
LoggerName = None
|
|
|
|
|
|
|
|
|
|
# Default log configuration
|
|
|
|
|
LoggerArgs = {}
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def __init__(self, **args):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-05-08 20:06:40 +00:00
|
|
|
Initialize a logger, looking for part restrictions in kwargs.
|
|
|
|
|
"""
|
2009-06-18 17:51:18 +00:00
|
|
|
if 'parts' in args and "all" not in args['parts']:
|
|
|
|
|
# only log given parts
|
|
|
|
|
self.logparts = args['parts']
|
|
|
|
|
else:
|
|
|
|
|
# log all parts
|
|
|
|
|
self.logparts = None
|
2005-05-08 20:06:40 +00:00
|
|
|
# number of spaces before log parts for alignment
|
2004-08-16 19:20:53 +00:00
|
|
|
self.logspaces = {}
|
2004-08-28 12:24:15 +00:00
|
|
|
# maximum indent of spaces for alignment
|
2004-08-16 19:20:53 +00:00
|
|
|
self.max_indent = 0
|
2010-12-14 19:30:53 +00:00
|
|
|
# log statistics
|
|
|
|
|
self.stats = LogStatistics()
|
2010-11-21 19:19:27 +00:00
|
|
|
# encoding of output
|
|
|
|
|
encoding = args.get("encoding", i18n.default_encoding)
|
|
|
|
|
try:
|
|
|
|
|
encoding = codecs.lookup(encoding).name
|
|
|
|
|
except LookupError:
|
|
|
|
|
encoding = i18n.default_encoding
|
|
|
|
|
self.output_encoding = encoding
|
|
|
|
|
# how to handle codec errors
|
|
|
|
|
self.codec_errors = "replace"
|
2012-09-14 20:36:59 +00:00
|
|
|
# Flag to see if logger is active. Can be deactivated on errors.
|
|
|
|
|
self.is_active = True
|
2010-11-21 19:19:27 +00:00
|
|
|
|
2013-12-11 17:41:55 +00:00
|
|
|
def get_args(self, kwargs):
|
|
|
|
|
"""Construct log configuration from default and user args."""
|
|
|
|
|
args = dict(self.LoggerArgs)
|
|
|
|
|
args.update(kwargs)
|
|
|
|
|
return args
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def get_charset_encoding(self):
|
2010-11-21 19:19:27 +00:00
|
|
|
"""Translate the output encoding to a charset encoding name."""
|
|
|
|
|
if self.output_encoding == "utf-8-sig":
|
|
|
|
|
return "utf-8"
|
|
|
|
|
return self.output_encoding
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def encode(self, s):
|
2010-11-22 06:43:33 +00:00
|
|
|
"""Encode string with output encoding."""
|
2020-05-19 18:56:42 +00:00
|
|
|
assert isinstance(s, str)
|
2010-11-22 06:43:33 +00:00
|
|
|
return s.encode(self.output_encoding, self.codec_errors)
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def init_fileoutput(self, args):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2009-06-18 17:51:18 +00:00
|
|
|
Initialize self.fd file descriptor from args. For file output
|
|
|
|
|
(used when the fileoutput arg is given), the self.fd
|
|
|
|
|
initialization is deferred until the first self.write() call.
|
2010-11-26 20:23:13 +00:00
|
|
|
This avoids creation of an empty file when no output is written.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2006-06-01 13:58:13 +00:00
|
|
|
self.filename = None
|
|
|
|
|
self.close_fd = False
|
|
|
|
|
self.fd = None
|
2004-10-27 12:22:10 +00:00
|
|
|
if args.get('fileoutput'):
|
2006-06-05 20:20:38 +00:00
|
|
|
self.filename = os.path.expanduser(args['filename'])
|
2008-03-19 10:22:57 +00:00
|
|
|
elif 'fd' in args:
|
2004-08-25 20:08:53 +00:00
|
|
|
self.fd = args['fd']
|
|
|
|
|
else:
|
2011-04-06 10:54:58 +00:00
|
|
|
self.fd = self.create_fd()
|
2006-06-01 13:58:13 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def start_fileoutput(self):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Start output to configured file."""
|
2006-06-01 13:58:13 +00:00
|
|
|
path = os.path.dirname(self.filename)
|
2006-11-21 17:10:27 +00:00
|
|
|
try:
|
|
|
|
|
if path and not os.path.isdir(path):
|
|
|
|
|
os.makedirs(path)
|
2010-11-21 19:48:50 +00:00
|
|
|
self.fd = self.create_fd()
|
2006-11-21 17:10:27 +00:00
|
|
|
self.close_fd = True
|
2022-11-08 19:21:29 +00:00
|
|
|
except OSError:
|
2006-11-21 17:10:27 +00:00
|
|
|
msg = sys.exc_info()[1]
|
2020-05-30 16:01:36 +00:00
|
|
|
log.warn(
|
|
|
|
|
LOG_CHECK,
|
2020-05-30 16:01:36 +00:00
|
|
|
"Could not open file %r for writing: %s\n"
|
|
|
|
|
"Disabling log output of %s",
|
2020-05-30 16:01:36 +00:00
|
|
|
self.filename,
|
|
|
|
|
msg,
|
|
|
|
|
self,
|
|
|
|
|
)
|
2008-05-09 06:16:03 +00:00
|
|
|
self.fd = dummy.Dummy()
|
2012-09-14 20:36:59 +00:00
|
|
|
self.is_active = False
|
2006-06-01 13:58:13 +00:00
|
|
|
self.filename = None
|
2004-08-25 20:08:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def create_fd(self):
|
2010-11-21 19:48:50 +00:00
|
|
|
"""Create open file descriptor."""
|
2011-04-06 10:54:58 +00:00
|
|
|
if self.filename is None:
|
2020-05-30 16:01:36 +00:00
|
|
|
return i18n.get_encoded_writer(
|
|
|
|
|
encoding=self.output_encoding, errors=self.codec_errors
|
|
|
|
|
)
|
|
|
|
|
return codecs.open(self.filename, "wb", self.output_encoding, self.codec_errors)
|
2010-11-21 19:48:50 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def close_fileoutput(self):
|
2005-05-06 13:05:16 +00:00
|
|
|
"""
|
|
|
|
|
Flush and close the file output denoted by self.fd.
|
|
|
|
|
"""
|
2006-06-05 20:20:38 +00:00
|
|
|
if self.fd is not None:
|
2012-09-14 20:09:18 +00:00
|
|
|
try:
|
|
|
|
|
self.flush()
|
2022-11-08 19:21:29 +00:00
|
|
|
except OSError:
|
2012-09-14 20:09:18 +00:00
|
|
|
# ignore flush errors
|
|
|
|
|
pass
|
2006-06-05 20:20:38 +00:00
|
|
|
if self.close_fd:
|
2012-09-14 20:09:18 +00:00
|
|
|
try:
|
|
|
|
|
self.fd.close()
|
2022-11-08 19:21:29 +00:00
|
|
|
except OSError:
|
2012-09-14 20:09:18 +00:00
|
|
|
# ignore close errors
|
|
|
|
|
pass
|
2006-06-05 20:20:38 +00:00
|
|
|
self.fd = None
|
2005-05-06 13:05:16 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def check_date(self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Check for special dates.
|
|
|
|
|
"""
|
2004-11-25 16:20:58 +00:00
|
|
|
now = datetime.date.today()
|
|
|
|
|
if now.day == 7 and now.month == 1:
|
|
|
|
|
msg = _("Happy birthday for LinkChecker, I'm %d years old today!")
|
|
|
|
|
self.comment(msg % (now.year - 2000))
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def comment(self, s, **args):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-07-16 07:12:44 +00:00
|
|
|
Write a comment and a newline. This method just prints
|
2005-01-19 14:38:01 +00:00
|
|
|
the given string.
|
|
|
|
|
"""
|
2004-11-25 16:20:58 +00:00
|
|
|
self.writeln(s=s, **args)
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def wrap(self, lines, width):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Return wrapped version of given lines.
|
|
|
|
|
"""
|
2020-05-30 16:01:36 +00:00
|
|
|
sep = os.linesep + os.linesep
|
2004-11-30 14:09:42 +00:00
|
|
|
text = sep.join(lines)
|
2020-05-30 16:01:36 +00:00
|
|
|
kwargs = dict(
|
|
|
|
|
subsequent_indent=" " * self.max_indent,
|
|
|
|
|
initial_indent=" " * self.max_indent,
|
|
|
|
|
break_long_words=False,
|
|
|
|
|
break_on_hyphens=False,
|
|
|
|
|
)
|
2010-03-11 20:50:23 +00:00
|
|
|
return strformat.wrap(text, width, **kwargs).lstrip()
|
2004-11-30 14:09:42 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def write(self, s, **args):
|
2012-10-10 08:54:30 +00:00
|
|
|
"""Write string to output descriptor. Strips control characters
|
|
|
|
|
from string before writing.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2006-06-01 13:58:13 +00:00
|
|
|
if self.filename is not None:
|
|
|
|
|
self.start_fileoutput()
|
2004-10-27 13:58:37 +00:00
|
|
|
if self.fd is None:
|
2007-12-13 12:25:21 +00:00
|
|
|
# Happens when aborting threads times out
|
2022-09-02 09:20:02 +00:00
|
|
|
log.warn(LOG_CHECK, "writing to uninitialized or closed file")
|
2007-12-13 12:25:21 +00:00
|
|
|
else:
|
2012-09-14 20:09:18 +00:00
|
|
|
try:
|
2012-10-10 10:27:08 +00:00
|
|
|
self.fd.write(s, **args)
|
2022-11-08 19:21:29 +00:00
|
|
|
except OSError:
|
2012-09-14 20:09:18 +00:00
|
|
|
msg = sys.exc_info()[1]
|
2020-05-30 16:01:36 +00:00
|
|
|
log.warn(
|
|
|
|
|
LOG_CHECK,
|
2020-05-30 16:01:36 +00:00
|
|
|
"Could not write to output file: %s\n"
|
|
|
|
|
"Disabling log output of %s",
|
2020-05-30 16:01:36 +00:00
|
|
|
msg,
|
|
|
|
|
self,
|
|
|
|
|
)
|
2012-09-14 20:09:18 +00:00
|
|
|
self.close_fileoutput()
|
|
|
|
|
self.fd = dummy.Dummy()
|
2012-09-14 20:36:59 +00:00
|
|
|
self.is_active = False
|
2004-10-27 18:13:20 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def writeln(self, s="", **args):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Write string to output descriptor plus a newline.
|
|
|
|
|
"""
|
2022-11-08 19:21:29 +00:00
|
|
|
self.write(f"{s}{os.linesep}", **args)
|
2004-10-27 13:58:37 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def has_part(self, name):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-05-08 20:06:40 +00:00
|
|
|
See if given part name will be logged.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-05-08 20:06:40 +00:00
|
|
|
if self.logparts is None:
|
|
|
|
|
# log all parts
|
2004-08-16 19:20:53 +00:00
|
|
|
return True
|
2005-05-08 20:06:40 +00:00
|
|
|
return name in self.logparts
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def part(self, name):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-05-08 20:06:40 +00:00
|
|
|
Return translated part name.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2020-04-30 19:11:59 +00:00
|
|
|
return _(Fields.get(name, ""))
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def spaces(self, name):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-05-08 20:06:40 +00:00
|
|
|
Return indent of spaces for given part name.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2004-08-16 19:20:53 +00:00
|
|
|
return self.logspaces[name]
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def start_output(self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Start log output.
|
|
|
|
|
"""
|
2005-05-08 20:06:40 +00:00
|
|
|
# map with spaces between part name and value
|
|
|
|
|
if self.logparts is None:
|
|
|
|
|
parts = Fields.keys()
|
2004-08-16 19:20:53 +00:00
|
|
|
else:
|
2005-05-08 20:06:40 +00:00
|
|
|
parts = self.logparts
|
2008-04-28 00:26:02 +00:00
|
|
|
values = (self.part(x) for x in parts)
|
2005-05-08 20:06:40 +00:00
|
|
|
# maximum indent for localized log part names
|
2020-05-30 16:01:36 +00:00
|
|
|
self.max_indent = max(len(x) for x in values) + 1
|
2005-05-08 20:06:40 +00:00
|
|
|
for key in parts:
|
2020-05-30 16:01:36 +00:00
|
|
|
numspaces = self.max_indent - len(self.part(key))
|
2020-04-30 19:11:59 +00:00
|
|
|
self.logspaces[key] = " " * numspaces
|
2010-12-20 23:35:07 +00:00
|
|
|
self.stats.reset()
|
2010-11-01 08:58:03 +00:00
|
|
|
self.starttime = time.time()
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def log_filter_url(self, url_data, do_print):
|
2005-04-25 14:51:35 +00:00
|
|
|
"""
|
2009-03-02 22:40:36 +00:00
|
|
|
Log a new url with this logger if do_print is True. Else
|
|
|
|
|
only update accounting data.
|
2005-04-25 14:51:35 +00:00
|
|
|
"""
|
2010-12-14 19:30:53 +00:00
|
|
|
self.stats.log_url(url_data, do_print)
|
2005-04-25 14:51:35 +00:00
|
|
|
if do_print:
|
|
|
|
|
self.log_url(url_data)
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def write_intro(self):
|
2010-11-01 08:58:03 +00:00
|
|
|
"""Write intro comments."""
|
2020-05-30 16:01:36 +00:00
|
|
|
self.comment(
|
|
|
|
|
_("created by %(app)s at %(time)s")
|
|
|
|
|
% {"app": configuration.AppName, "time": strformat.strtime(self.starttime)}
|
|
|
|
|
)
|
|
|
|
|
self.comment(
|
2021-12-06 19:36:22 +00:00
|
|
|
_("Read the documentation at %(url)s") % {'url': configuration.Url}
|
2020-05-30 16:01:36 +00:00
|
|
|
)
|
|
|
|
|
self.comment(
|
|
|
|
|
_("Write comments and bugs to %(url)s") % {'url': configuration.SupportUrl}
|
|
|
|
|
)
|
2010-11-01 08:58:03 +00:00
|
|
|
self.check_date()
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def write_outro(self):
|
2010-11-01 08:58:03 +00:00
|
|
|
"""Write outro comments."""
|
|
|
|
|
self.stoptime = time.time()
|
|
|
|
|
duration = self.stoptime - self.starttime
|
2020-05-30 16:01:36 +00:00
|
|
|
self.comment(
|
|
|
|
|
_("Stopped checking at %(time)s (%(duration)s)")
|
|
|
|
|
% {
|
|
|
|
|
"time": strformat.strtime(self.stoptime),
|
|
|
|
|
"duration": strformat.strduration_long(duration),
|
|
|
|
|
}
|
|
|
|
|
)
|
2010-11-01 08:58:03 +00:00
|
|
|
|
2012-06-19 21:27:26 +00:00
|
|
|
@abc.abstractmethod
|
2020-05-16 19:19:42 +00:00
|
|
|
def log_url(self, url_data):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Log a new url with this logger.
|
|
|
|
|
"""
|
2005-12-20 20:17:33 +00:00
|
|
|
pass
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2012-06-19 21:27:26 +00:00
|
|
|
@abc.abstractmethod
|
2020-05-16 19:19:42 +00:00
|
|
|
def end_output(self, **kwargs):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
End of output, used for cleanup (eg output buffer flushing).
|
|
|
|
|
"""
|
2005-12-20 20:17:33 +00:00
|
|
|
pass
|
2004-08-16 19:20:53 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def __str__(self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Return class name.
|
|
|
|
|
"""
|
2004-08-16 19:20:53 +00:00
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def __repr__(self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Return class name.
|
|
|
|
|
"""
|
2004-08-16 19:20:53 +00:00
|
|
|
return repr(self.__class__.__name__)
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def flush(self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
If the logger has internal buffers, flush them.
|
|
|
|
|
Ignore flush I/O errors since we are not responsible for proper
|
|
|
|
|
flushing of log output streams.
|
2004-08-26 18:58:01 +00:00
|
|
|
"""
|
|
|
|
|
if hasattr(self, "fd"):
|
|
|
|
|
try:
|
|
|
|
|
self.fd.flush()
|
2022-11-08 19:21:29 +00:00
|
|
|
except (OSError, AttributeError):
|
2004-08-26 18:58:01 +00:00
|
|
|
pass
|
2008-05-09 06:16:03 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def log_internal_error(self):
|
2011-12-14 21:54:26 +00:00
|
|
|
"""Indicate that an internal error occurred in the program."""
|
|
|
|
|
log.warn(LOG_CHECK, "internal error occurred")
|
|
|
|
|
self.stats.log_internal_error()
|
|
|
|
|
|
2012-09-19 09:05:26 +00:00
|
|
|
def format_modified(self, modified, sep=" "):
|
2013-09-02 22:38:54 +00:00
|
|
|
"""Format modification date in UTC if it's not None.
|
2020-07-25 15:35:48 +00:00
|
|
|
|
2013-09-02 22:38:54 +00:00
|
|
|
@param modified: modification date in UTC
|
2020-07-25 15:35:48 +00:00
|
|
|
@type modified: datetime or None
|
2012-09-19 09:05:26 +00:00
|
|
|
@return: formatted date or empty string
|
|
|
|
|
@rtype: unicode
|
|
|
|
|
"""
|
|
|
|
|
if modified is not None:
|
2022-11-08 19:21:29 +00:00
|
|
|
return modified.strftime(f"%Y-%m-%d{sep}%H:%M:%S.%fZ")
|
2020-04-30 19:11:59 +00:00
|
|
|
return ""
|
2012-09-19 09:05:26 +00:00
|
|
|
|
2020-05-30 16:01:36 +00:00
|
|
|
|
2013-12-11 17:41:55 +00:00
|
|
|
def _get_loggers():
|
|
|
|
|
"""Return list of Logger classes."""
|
|
|
|
|
from .. import loader
|
2020-05-30 16:01:36 +00:00
|
|
|
|
2021-12-30 19:27:04 +00:00
|
|
|
modules = loader.get_package_modules('logger', __path__)
|
2014-02-28 23:12:34 +00:00
|
|
|
return list(loader.get_plugins(modules, [_Logger]))
|
2013-12-11 17:41:55 +00:00
|
|
|
|
2011-12-14 21:54:26 +00:00
|
|
|
|
2013-12-11 17:41:55 +00:00
|
|
|
LoggerClasses = _get_loggers()
|
|
|
|
|
LoggerNames = [x.LoggerName for x in LoggerClasses]
|
|
|
|
|
LoggerKeys = ", ".join(repr(x) for x in LoggerNames)
|