2
# Copyright (C) 2010 Canonical
7
# This program is free software; you can redistribute it and/or modify it under
8
# the terms of the GNU General Public License as published by the Free Software
9
# Foundation; version 3.
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
""" setup global logging for software-center """
24
class NullFilterThatWarnsAboutRootLoggerUsage(logging.Filter):
25
""" pass all messages through, but warn about messages that
26
come from the root logger (and not from the softwarecenter root)
28
def filter(self, record):
29
if not record.name.startswith("softwarecenter"):
30
fixme_msg = logging.getLogger("softwarecenter.fixme").findCaller()
31
s = "logs to the root logger: '%s'" % str(fixme_msg)
32
logging.getLogger("softwarecenter.fixme").warn(s)
35
class OrFilter(logging.Filter):
36
""" A filter that can have multiple filter strings and shows
37
the message if any of the filter strings matches
41
def filter(self, record):
42
for (fname,flen) in self.filters:
44
fname == record.name or
45
(len(record.name)>flen and record.name[flen] == ".")):
48
def add(self, log_filter):
49
""" add a log_filter string to the list of OR expressions """
50
self.filters.append((log_filter, len(log_filter)))
52
def add_filters_from_string(long_filter_str):
53
""" take the string passed from e.g. the commandline and create
54
logging.Filters for it. It will prepend "softwarecenter."
55
if that is not passed and will split "," to support mulitple
58
logging.debug("adding filter: '%s'" % long_filter_str)
59
logfilter = OrFilter()
60
for filter_str in long_filter_str.split(","):
61
filter_str = filter_str.strip("")
64
if not (filter_str.startswith("sc") or
65
filter_str.startswith("softwarecenter")):
66
filter_str = "sc.%s" % filter_str
67
if filter_str.startswith("sc"):
68
filter_str = "softwarecenter" + filter_str[2:]
69
logfilter.add(filter_str)
71
handler.addFilter(logfilter)
75
# setup global software-center logging
76
root = logging.getLogger()
77
fmt = logging.Formatter(logging.BASIC_FORMAT, None)
78
handler = logging.StreamHandler()
79
handler.setFormatter(fmt)
80
root.addHandler(handler)
81
handler.addFilter(NullFilterThatWarnsAboutRootLoggerUsage())