~mvo/software-center/oneconf-lp981536

« back to all changes in this revision

Viewing changes to softwarecenter/log.py

merge with trunk and many fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# Copyright (C) 2010 Canonical
 
3
#
 
4
# Authors:
 
5
#  Geliy Sokolov
 
6
#
 
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.
 
10
#
 
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
 
14
# details.
 
15
#
 
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
 
19
 
 
20
import logging
 
21
 
 
22
""" setup global logging for software-center """
 
23
 
 
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)
 
27
    """
 
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)
 
33
        return 1
 
34
        
 
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
 
38
    """
 
39
    def __init__(self):
 
40
        self.filters = []
 
41
    def filter(self, record):
 
42
        for (fname,flen) in self.filters:
 
43
            if (flen == 0 or 
 
44
                fname == record.name or 
 
45
                (len(record.name)>flen and record.name[flen] == ".")):
 
46
                return 1
 
47
        return 0
 
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)))
 
51
 
 
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
 
56
        filters
 
57
    """
 
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("")
 
62
        if filter_str == "":
 
63
            return
 
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)
 
70
    # attach or filter
 
71
    handler.addFilter(logfilter)
 
72
 
 
73
 
 
74
 
 
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())