3
|
1 |
#
|
|
2 |
# Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
# Class to control array of defined logging filters
|
|
16 |
# This class will act as a switch, controlling the list of filters iteratively
|
|
17 |
#
|
|
18 |
|
|
19 |
import os
|
|
20 |
import sys
|
|
21 |
import raptor
|
|
22 |
import filter_interface
|
|
23 |
import pluginbox
|
|
24 |
import traceback
|
|
25 |
|
|
26 |
class FilterWriteException(Exception):
|
|
27 |
def __init__(self, value):
|
|
28 |
self.parameter = value
|
|
29 |
def __str__(self):
|
|
30 |
return repr(self.parameter)
|
|
31 |
|
|
32 |
class BootstrapFilter(filter_interface.Filter):
|
|
33 |
def __init__(self):
|
|
34 |
self.out = sys.stdout
|
|
35 |
|
|
36 |
"""Use this until the CLI has supplied some real filters"""
|
|
37 |
def open(self, raptor_instance):
|
|
38 |
"""Set output to stdout for the various I/O methods to write to."""
|
|
39 |
self.raptor = raptor_instance
|
|
40 |
return True
|
|
41 |
|
|
42 |
def write(self, text):
|
|
43 |
"""Write errors and warnings to stdout"""
|
|
44 |
|
|
45 |
if text.startswith("<error>"):
|
|
46 |
start = text.find(">")
|
|
47 |
end = text.rfind("<")
|
|
48 |
self.out.write(str(raptor.name) + ": error: %s\n" \
|
|
49 |
% text[(start + 1):end])
|
|
50 |
elif text.startswith("<warning"):
|
|
51 |
start = text.find(">")
|
|
52 |
end = text.rfind("<")
|
|
53 |
self.out.write(str(raptor.name) + ": warning: %s\n" \
|
|
54 |
% text[(start + 1):end])
|
|
55 |
elif "error" in text or "warning" in text:
|
|
56 |
self.out.write(text)
|
|
57 |
return True
|
|
58 |
|
|
59 |
def summary(self):
|
|
60 |
"""Write Summary"""
|
|
61 |
return False
|
|
62 |
|
|
63 |
def close(self):
|
|
64 |
"""Nothing to do for stdout"""
|
|
65 |
return True
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
class FilterList(filter_interface.Filter):
|
|
70 |
|
|
71 |
def __init__(self):
|
|
72 |
self.out = [BootstrapFilter()]
|
|
73 |
self.filters = []
|
|
74 |
self.pbox = None
|
|
75 |
|
|
76 |
def open(self, raptor_instance, filternames, pbox):
|
|
77 |
"""
|
|
78 |
Call open function on each filter using raptor parameters provided
|
|
79 |
Returns: Boolean: Have the functions succeeded in opening the files?
|
|
80 |
"""
|
|
81 |
# Find all the filter plugins
|
|
82 |
self.pbox = pbox
|
|
83 |
possiblefilters = self.pbox.classesof(filter_interface.Filter)
|
|
84 |
unfound = []
|
|
85 |
self.filters = []
|
|
86 |
for f in filternames:
|
|
87 |
unfound.append(f) # unfound unless we find it
|
|
88 |
for pl in possiblefilters:
|
|
89 |
if pl.__name__.upper() == f.upper():
|
|
90 |
self.filters.append(pl())
|
|
91 |
unfound = unfound[:-1]
|
|
92 |
if unfound != []:
|
|
93 |
raise ValueError("requested filters not found: %s \
|
|
94 |
\nAvailable filters are: %s" % (str(unfound), self.format_output_list(possiblefilters)))
|
|
95 |
|
|
96 |
if self.filters == []:
|
|
97 |
self.out = [BootstrapFilter()]
|
|
98 |
else:
|
|
99 |
self.out=[]
|
|
100 |
for filter in self.filters:
|
|
101 |
if filter.open(raptor_instance):
|
|
102 |
self.out.append(filter)
|
|
103 |
else:
|
|
104 |
sys.stderr.write(str(raptor.name) + \
|
|
105 |
": error: Cannot open filter: %s\n" % str(filter))
|
|
106 |
ok = False
|
|
107 |
|
|
108 |
if self.out == []:
|
|
109 |
sys.stderr.write(str(raptor.name) + \
|
|
110 |
": warning: All filters failed to open. " + \
|
|
111 |
"Defaulting to 'stdout'\n")
|
|
112 |
self.out = [BootstrapFilter()]
|
|
113 |
|
|
114 |
def write(self, text):
|
|
115 |
"""
|
|
116 |
Iterate through each filter, calling their write function
|
|
117 |
"""
|
|
118 |
|
|
119 |
if text is None:
|
|
120 |
return
|
|
121 |
|
|
122 |
badfilters = []
|
|
123 |
for filter in self.out:
|
|
124 |
try:
|
|
125 |
filter.write(text)
|
|
126 |
except Exception,e:
|
|
127 |
traceback.print_exc(file=sys.stdout)
|
|
128 |
sys.stdout.write("Called from: \n")
|
|
129 |
traceback.print_stack(file=sys.stdout)
|
|
130 |
sys.stdout.write("\n")
|
|
131 |
badfilters.append(filter)
|
|
132 |
|
|
133 |
if len(badfilters) > 0:
|
|
134 |
for f in badfilters:
|
|
135 |
self.out.remove(f) # dump the filter in case it causes repeated exceptions
|
|
136 |
sys.stdout.write("Removed filter %s because it generated an exception\n" % type(f))
|
|
137 |
|
|
138 |
if len(self.out) == 0:
|
|
139 |
sys.stdout.write("Falling back to bootstrap filter\n")
|
|
140 |
self.out = [BootstrapFilter()] # Try to fall back to something in the worst case
|
|
141 |
|
|
142 |
def summary(self):
|
|
143 |
"""
|
|
144 |
Run the summaries of all filters (prior to log end)
|
|
145 |
"""
|
|
146 |
for filter in self.out:
|
|
147 |
filter.summary()
|
|
148 |
|
|
149 |
def close(self):
|
|
150 |
"""
|
|
151 |
Iterate through each filter, calling their close function
|
|
152 |
Returns True if all the filters close properly
|
|
153 |
"""
|
|
154 |
returnVal = True
|
|
155 |
|
|
156 |
for filter in self.out:
|
|
157 |
if (filter != sys.stdout) and (filter != sys.stderr):
|
|
158 |
returnVal = returnVal and filter.close()
|
|
159 |
|
|
160 |
return returnVal
|
|
161 |
|
|
162 |
def format_output_list(self, possiblefilters):
|
|
163 |
"""
|
|
164 |
formats available filters
|
|
165 |
"""
|
|
166 |
filters_formatted = ""
|
|
167 |
for pl in possiblefilters:
|
|
168 |
filters_formatted += "\n " + pl.__name__
|
|
169 |
return filters_formatted
|
|
170 |
|