2 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
5 package com
.intellij
.diagnostic
.logging
;
7 import com
.intellij
.execution
.process
.ProcessOutputTypes
;
8 import com
.intellij
.execution
.ui
.ConsoleViewContentType
;
9 import com
.intellij
.openapi
.components
.ServiceManager
;
10 import com
.intellij
.openapi
.components
.State
;
11 import com
.intellij
.openapi
.components
.Storage
;
12 import com
.intellij
.openapi
.diagnostic
.Logger
;
13 import com
.intellij
.openapi
.project
.Project
;
14 import com
.intellij
.openapi
.util
.DefaultJDOMExternalizer
;
15 import com
.intellij
.openapi
.util
.InvalidDataException
;
16 import com
.intellij
.openapi
.util
.Key
;
17 import com
.intellij
.openapi
.util
.WriteExternalException
;
18 import com
.intellij
.util
.StringBuilderSpinAllocator
;
19 import org
.jdom
.Element
;
20 import org
.jetbrains
.annotations
.NonNls
;
21 import org
.jetbrains
.annotations
.NotNull
;
22 import org
.jetbrains
.annotations
.Nullable
;
25 import java
.util
.regex
.Pattern
;
35 file
= "$WORKSPACE_FILE$")})
36 @SuppressWarnings({"AssignmentToStaticFieldFromInstanceMethod"})
37 public class LogConsolePreferences
extends LogFilterRegistrar
{
38 private final SortedMap
<LogFilter
, Boolean
> myRegisteredLogFilters
= new TreeMap
<LogFilter
, Boolean
>(new Comparator
<LogFilter
>() {
39 public int compare(final LogFilter o1
, final LogFilter o2
) {
43 @NonNls private static final String FILTER
= "filter";
44 @NonNls private static final String IS_ACTIVE
= "is_active";
46 public boolean FILTER_ERRORS
= false;
47 public boolean FILTER_WARNINGS
= false;
48 public boolean FILTER_INFO
= true;
49 public String CUSTOM_FILTER
= null;
50 @NonNls public static final String ERROR
= "ERROR";
51 @NonNls public static final String WARNING
= "WARNING";
52 @NonNls private static final String WARN
= "WARN";
53 @NonNls public static final String INFO
= "INFO";
54 @NonNls public static final String CUSTOM
= "CUSTOM";
56 public final static Pattern ERROR_PATTERN
= Pattern
.compile(".*" + ERROR
+ ".*");
57 public final static Pattern WARNING_PATTERN
= Pattern
.compile(".*" + WARNING
+ ".*");
58 public final static Pattern WARN_PATTERN
= Pattern
.compile(".*" + WARN
+ ".*");
59 public final static Pattern INFO_PATTERN
= Pattern
.compile(".*" + INFO
+ ".*");
61 private static Pattern ourCustomPattern
= null;
63 @NonNls public final static Pattern EXCEPTION_PATTERN
= Pattern
.compile(".*at .*");
65 private final List
<FilterListener
> myListeners
= new ArrayList
<FilterListener
>();
66 private static final Logger LOG
= Logger
.getInstance("#" + LogConsolePreferences
.class.getName());
68 public static LogConsolePreferences
getInstance(Project project
) {
69 return ServiceManager
.getService(project
, LogConsolePreferences
.class);
72 public void updateCustomFilter(String customFilter
) {
73 CUSTOM_FILTER
= customFilter
;
74 ourCustomPattern
= null;
79 private Pattern
getCustomPattern() {
80 if (ourCustomPattern
== null && CUSTOM_FILTER
!= null) {
81 final StringBuilder buf
= StringBuilderSpinAllocator
.alloc();
83 for (int i
= 0; i
< CUSTOM_FILTER
.length(); i
++) {
84 final char c
= CUSTOM_FILTER
.charAt(i
);
85 if (Character
.isLetterOrDigit(c
)) {
86 buf
.append(Character
.toUpperCase(c
));
89 buf
.append("\\").append(c
);
92 ourCustomPattern
= Pattern
.compile(".*" + buf
+ ".*");
95 StringBuilderSpinAllocator
.dispose(buf
);
98 return ourCustomPattern
;
101 public boolean isApplicable(@NotNull String text
, String prevType
, boolean checkStandartFilters
) {
102 if (CUSTOM_FILTER
!= null) {
103 final Pattern pattern
= getCustomPattern();
104 if (pattern
!= null && !pattern
.matcher(text
.toUpperCase()).matches()) return false;
106 for (LogFilter filter
: myRegisteredLogFilters
.keySet()) {
107 if (myRegisteredLogFilters
.get(filter
).booleanValue() && !filter
.isAcceptable(text
)) return false;
109 if (checkStandartFilters
) {
110 final String type
= getType(text
);
111 boolean selfTyped
= false;
113 if (!isApplicable(type
)) return false;
116 return selfTyped
|| prevType
== null || isApplicable(prevType
);
121 private boolean isApplicable(final String type
) {
122 if (type
.equals(ERROR
)) {
123 return !FILTER_ERRORS
;
125 if (type
.equals(WARNING
)) {
126 return !FILTER_WARNINGS
;
128 if (type
.equals(INFO
)) {
134 public static ConsoleViewContentType
getContentType(String type
) {
135 if (type
.equals(ERROR
)) return ConsoleViewContentType
.ERROR_OUTPUT
;
136 return ConsoleViewContentType
.NORMAL_OUTPUT
;
140 public static String
getType(@NotNull String text
) {
141 if (ERROR_PATTERN
.matcher(text
.toUpperCase()).matches()) return ERROR
;
142 if (WARNING_PATTERN
.matcher(text
.toUpperCase()).matches() || WARN_PATTERN
.matcher(text
.toUpperCase()).matches()) return WARNING
;
143 if (INFO_PATTERN
.matcher(text
.toUpperCase()).matches()) return INFO
;
147 public static Key
getProcessOutputTypes(String type
) {
148 if (type
.equals(ERROR
)) return ProcessOutputTypes
.STDERR
;
149 if (type
.equals(WARNING
) || type
.equals(INFO
)) return ProcessOutputTypes
.STDOUT
;
153 public Element
getState() {
154 @NonNls Element element
= new Element("LogFilters");
156 for (LogFilter filter
: myRegisteredLogFilters
.keySet()) {
157 Element filterElement
= new Element(FILTER
);
158 filterElement
.setAttribute(IS_ACTIVE
, myRegisteredLogFilters
.get(filter
).toString());
159 filter
.writeExternal(filterElement
);
160 element
.addContent(filterElement
);
162 DefaultJDOMExternalizer
.writeExternal(this, element
);
164 catch (WriteExternalException e
) {
170 public void loadState(final Element object
) {
172 final List children
= object
.getChildren(FILTER
);
173 for (Object child
: children
) {
174 Element filterElement
= (Element
)child
;
175 final LogFilter filter
= new LogFilter();
176 filter
.readExternal(filterElement
);
177 setFilterSelected(filter
, Boolean
.parseBoolean(filterElement
.getAttributeValue(IS_ACTIVE
)));
179 DefaultJDOMExternalizer
.readExternal(this, object
);
181 catch (InvalidDataException e
) {
186 public void registerFilter(LogFilter filter
) {
187 myRegisteredLogFilters
.put(filter
, Boolean
.FALSE
);
190 public List
<LogFilter
> getRegisteredLogFilters() {
191 return new ArrayList
<LogFilter
>(myRegisteredLogFilters
.keySet());
194 public boolean isFilterSelected(LogFilter filter
) {
195 final Boolean isSelected
= myRegisteredLogFilters
.get(filter
);
196 if (isSelected
!= null) {
197 return isSelected
.booleanValue();
199 if (filter
instanceof IndependentLogFilter
) {
200 return ((IndependentLogFilter
)filter
).isSelected();
205 public void setFilterSelected(LogFilter filter
, boolean state
) {
206 if (filter
instanceof IndependentLogFilter
) {
207 ((IndependentLogFilter
)filter
).selectFilter();
209 else if (myRegisteredLogFilters
.containsKey(filter
)) {
210 myRegisteredLogFilters
.put(filter
, state
);
212 fireStateChanged(filter
);
215 public void selectOnlyFilter(LogFilter filter
) {
216 for (LogFilter logFilter
: myRegisteredLogFilters
.keySet()) {
217 myRegisteredLogFilters
.put(logFilter
, false);
219 setFilterSelected(filter
, true);
222 private void fireStateChanged(final LogFilter filter
) {
223 for (FilterListener listener
: myListeners
) {
224 listener
.onFilterStateChange(filter
);
228 private void fireStateChanged() {
229 for (FilterListener listener
: myListeners
) {
230 listener
.onTextFilterChange();
234 public void addFilterListener(FilterListener l
) {
238 public void removeFilterListener(FilterListener l
) {
239 myListeners
.remove(l
);
242 public interface FilterListener
{
243 void onFilterStateChange(LogFilter filter
);
245 void onTextFilterChange();