author | Richard Taylor <richard.i.taylor@nokia.com> |
Thu, 01 Apr 2010 11:47:09 +0100 | |
branch | wip |
changeset 451 | 153129bf777e |
parent 3 | e1eecf4d390d |
child 553 | 7d4971eaf863 |
permissions | -rw-r--r-- |
3 | 1 |
# |
451
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
2 |
# Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
3 | 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 |
|
451
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
22 |
import re |
3 | 23 |
import filter_interface |
24 |
import pluginbox |
|
25 |
import traceback |
|
26 |
||
27 |
class FilterWriteException(Exception): |
|
28 |
def __init__(self, value): |
|
29 |
self.parameter = value |
|
30 |
def __str__(self): |
|
31 |
return repr(self.parameter) |
|
32 |
||
33 |
class BootstrapFilter(filter_interface.Filter): |
|
34 |
def __init__(self): |
|
35 |
self.out = sys.stdout |
|
36 |
||
37 |
"""Use this until the CLI has supplied some real filters""" |
|
38 |
def open(self, raptor_instance): |
|
39 |
"""Set output to stdout for the various I/O methods to write to.""" |
|
40 |
self.raptor = raptor_instance |
|
41 |
return True |
|
42 |
||
43 |
def write(self, text): |
|
44 |
"""Write errors and warnings to stdout""" |
|
45 |
||
46 |
if text.startswith("<error>"): |
|
47 |
start = text.find(">") |
|
48 |
end = text.rfind("<") |
|
49 |
self.out.write(str(raptor.name) + ": error: %s\n" \ |
|
50 |
% text[(start + 1):end]) |
|
51 |
elif text.startswith("<warning"): |
|
52 |
start = text.find(">") |
|
53 |
end = text.rfind("<") |
|
54 |
self.out.write(str(raptor.name) + ": warning: %s\n" \ |
|
55 |
% text[(start + 1):end]) |
|
56 |
elif "error" in text or "warning" in text: |
|
57 |
self.out.write(text) |
|
58 |
return True |
|
59 |
||
60 |
def summary(self): |
|
61 |
"""Write Summary""" |
|
62 |
return False |
|
63 |
||
64 |
def close(self): |
|
65 |
"""Nothing to do for stdout""" |
|
66 |
return True |
|
67 |
||
451
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
68 |
def SplitList(listString): |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
69 |
"""turn a CLI filter string into a list of (class, param) pairs. |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
70 |
|
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
71 |
for example, "foo[a,b],bar[c,d]" |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
72 |
|
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
73 |
becomes [ ("foo", ["a","b"]) , ("bar", ["c","d"]) ] |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
74 |
""" |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
75 |
matches = re.findall("(\w+)(\[([^\[\]]*)\])?,?", listString) |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
76 |
|
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
77 |
pairs = [] |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
78 |
for m in matches: |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
79 |
classname = m[0] |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
80 |
if len(m[2]) > 0: |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
81 |
pairs.append( (classname, m[2].split(",")) ) |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
82 |
else: |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
83 |
pairs.append( (classname, []) ) |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
84 |
return pairs |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
85 |
|
3 | 86 |
class FilterList(filter_interface.Filter): |
87 |
||
88 |
def __init__(self): |
|
89 |
self.out = [BootstrapFilter()] |
|
90 |
self.filters = [] |
|
91 |
self.pbox = None |
|
92 |
||
93 |
def open(self, raptor_instance, filternames, pbox): |
|
94 |
""" |
|
95 |
Call open function on each filter using raptor parameters provided |
|
96 |
Returns: Boolean: Have the functions succeeded in opening the files? |
|
97 |
""" |
|
98 |
# Find all the filter plugins |
|
99 |
self.pbox = pbox |
|
100 |
possiblefilters = self.pbox.classesof(filter_interface.Filter) |
|
451
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
101 |
# turn "filternames" into a list of (classname, parameters) pairs |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
102 |
filterCalls = SplitList(filternames) |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
103 |
# look for each filter class in the box |
3 | 104 |
unfound = [] |
105 |
self.filters = [] |
|
451
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
106 |
for (f, params) in filterCalls: |
3 | 107 |
unfound.append(f) # unfound unless we find it |
108 |
for pl in possiblefilters: |
|
109 |
if pl.__name__.upper() == f.upper(): |
|
451
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
110 |
if params: |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
111 |
self.filters.append(pl(params)) |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
112 |
else: |
153129bf777e
pass optional parameters to log filters from the command line
Richard Taylor <richard.i.taylor@nokia.com>
parents:
3
diff
changeset
|
113 |
self.filters.append(pl()) |
3 | 114 |
unfound = unfound[:-1] |
115 |
if unfound != []: |
|
116 |
raise ValueError("requested filters not found: %s \ |
|
117 |
\nAvailable filters are: %s" % (str(unfound), self.format_output_list(possiblefilters))) |
|
118 |
||
119 |
if self.filters == []: |
|
120 |
self.out = [BootstrapFilter()] |
|
121 |
else: |
|
122 |
self.out=[] |
|
123 |
for filter in self.filters: |
|
124 |
if filter.open(raptor_instance): |
|
125 |
self.out.append(filter) |
|
126 |
else: |
|
127 |
sys.stderr.write(str(raptor.name) + \ |
|
128 |
": error: Cannot open filter: %s\n" % str(filter)) |
|
129 |
ok = False |
|
130 |
||
131 |
if self.out == []: |
|
132 |
sys.stderr.write(str(raptor.name) + \ |
|
133 |
": warning: All filters failed to open. " + \ |
|
134 |
"Defaulting to 'stdout'\n") |
|
135 |
self.out = [BootstrapFilter()] |
|
136 |
||
137 |
def write(self, text): |
|
138 |
""" |
|
139 |
Iterate through each filter, calling their write function |
|
140 |
""" |
|
141 |
||
142 |
if text is None: |
|
143 |
return |
|
144 |
||
145 |
badfilters = [] |
|
146 |
for filter in self.out: |
|
147 |
try: |
|
148 |
filter.write(text) |
|
149 |
except Exception,e: |
|
150 |
traceback.print_exc(file=sys.stdout) |
|
151 |
sys.stdout.write("Called from: \n") |
|
152 |
traceback.print_stack(file=sys.stdout) |
|
153 |
sys.stdout.write("\n") |
|
154 |
badfilters.append(filter) |
|
155 |
||
156 |
if len(badfilters) > 0: |
|
157 |
for f in badfilters: |
|
158 |
self.out.remove(f) # dump the filter in case it causes repeated exceptions |
|
159 |
sys.stdout.write("Removed filter %s because it generated an exception\n" % type(f)) |
|
160 |
||
161 |
if len(self.out) == 0: |
|
162 |
sys.stdout.write("Falling back to bootstrap filter\n") |
|
163 |
self.out = [BootstrapFilter()] # Try to fall back to something in the worst case |
|
164 |
||
165 |
def summary(self): |
|
166 |
""" |
|
167 |
Run the summaries of all filters (prior to log end) |
|
168 |
""" |
|
169 |
for filter in self.out: |
|
170 |
filter.summary() |
|
171 |
||
172 |
def close(self): |
|
173 |
""" |
|
174 |
Iterate through each filter, calling their close function |
|
175 |
Returns True if all the filters close properly |
|
176 |
""" |
|
177 |
returnVal = True |
|
178 |
||
179 |
for filter in self.out: |
|
180 |
if (filter != sys.stdout) and (filter != sys.stderr): |
|
181 |
returnVal = returnVal and filter.close() |
|
182 |
||
183 |
return returnVal |
|
184 |
||
185 |
def format_output_list(self, possiblefilters): |
|
186 |
""" |
|
187 |
formats available filters |
|
188 |
""" |
|
189 |
filters_formatted = "" |
|
190 |
for pl in possiblefilters: |
|
191 |
filters_formatted += "\n " + pl.__name__ |
|
192 |
return filters_formatted |
|
193 |