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 org
.jdom
.Element
;
19 import org
.jetbrains
.annotations
.NonNls
;
20 import org
.jetbrains
.annotations
.NotNull
;
21 import org
.jetbrains
.annotations
.Nullable
;
24 import java
.util
.regex
.Pattern
;
34 file
= "$WORKSPACE_FILE$")})
35 @SuppressWarnings({"AssignmentToStaticFieldFromInstanceMethod"})
36 public class LogConsolePreferences
extends LogFilterRegistrar
{
37 private final SortedMap
<LogFilter
, Boolean
> myRegisteredLogFilters
= new TreeMap
<LogFilter
, Boolean
>(new Comparator
<LogFilter
>() {
38 public int compare(final LogFilter o1
, final LogFilter o2
) {
42 @NonNls private static final String FILTER
= "filter";
43 @NonNls private static final String IS_ACTIVE
= "is_active";
45 public boolean FILTER_ERRORS
= false;
46 public boolean FILTER_WARNINGS
= false;
47 public boolean FILTER_INFO
= true;
48 public String CUSTOM_FILTER
= null;
49 @NonNls public static final String ERROR
= "ERROR";
50 @NonNls public static final String WARNING
= "WARNING";
51 @NonNls private static final String WARN
= "WARN";
52 @NonNls public static final String INFO
= "INFO";
53 @NonNls public static final String CUSTOM
= "CUSTOM";
55 public final static Pattern ERROR_PATTERN
= Pattern
.compile(".*" + ERROR
+ ".*");
56 public final static Pattern WARNING_PATTERN
= Pattern
.compile(".*" + WARNING
+ ".*");
57 public final static Pattern WARN_PATTERN
= Pattern
.compile(".*" + WARN
+ ".*");
58 public final static Pattern INFO_PATTERN
= Pattern
.compile(".*" + INFO
+ ".*");
60 @NonNls public final static Pattern EXCEPTION_PATTERN
= Pattern
.compile(".*at .*");
62 private final List
<LogFilterListener
> myListeners
= new ArrayList
<LogFilterListener
>();
63 private static final Logger LOG
= Logger
.getInstance("#" + LogConsolePreferences
.class.getName());
65 public static LogConsolePreferences
getInstance(Project project
) {
66 return ServiceManager
.getService(project
, LogConsolePreferences
.class);
69 public void updateCustomFilter(String customFilter
) {
70 CUSTOM_FILTER
= customFilter
;
75 public boolean isApplicable(@NotNull String text
, String prevType
, boolean checkStandartFilters
) {
76 for (LogFilter filter
: myRegisteredLogFilters
.keySet()) {
77 if (myRegisteredLogFilters
.get(filter
).booleanValue() && !filter
.isAcceptable(text
)) return false;
79 if (checkStandartFilters
) {
80 final String type
= getType(text
);
81 boolean selfTyped
= false;
83 if (!isApplicable(type
)) return false;
86 return selfTyped
|| prevType
== null || isApplicable(prevType
);
91 private boolean isApplicable(final String type
) {
92 if (type
.equals(ERROR
)) {
93 return !FILTER_ERRORS
;
95 if (type
.equals(WARNING
)) {
96 return !FILTER_WARNINGS
;
98 if (type
.equals(INFO
)) {
104 public static ConsoleViewContentType
getContentType(String type
) {
105 if (type
.equals(ERROR
)) return ConsoleViewContentType
.ERROR_OUTPUT
;
106 return ConsoleViewContentType
.NORMAL_OUTPUT
;
110 public static String
getType(@NotNull String text
) {
111 if (ERROR_PATTERN
.matcher(text
.toUpperCase()).matches()) return ERROR
;
112 if (WARNING_PATTERN
.matcher(text
.toUpperCase()).matches() || WARN_PATTERN
.matcher(text
.toUpperCase()).matches()) return WARNING
;
113 if (INFO_PATTERN
.matcher(text
.toUpperCase()).matches()) return INFO
;
117 public static Key
getProcessOutputTypes(String type
) {
118 if (type
.equals(ERROR
)) return ProcessOutputTypes
.STDERR
;
119 if (type
.equals(WARNING
) || type
.equals(INFO
)) return ProcessOutputTypes
.STDOUT
;
123 public Element
getState() {
124 @NonNls Element element
= new Element("LogFilters");
126 for (LogFilter filter
: myRegisteredLogFilters
.keySet()) {
127 Element filterElement
= new Element(FILTER
);
128 filterElement
.setAttribute(IS_ACTIVE
, myRegisteredLogFilters
.get(filter
).toString());
129 filter
.writeExternal(filterElement
);
130 element
.addContent(filterElement
);
132 DefaultJDOMExternalizer
.writeExternal(this, element
);
134 catch (WriteExternalException e
) {
140 public void loadState(final Element object
) {
142 final List children
= object
.getChildren(FILTER
);
143 for (Object child
: children
) {
144 Element filterElement
= (Element
)child
;
145 final LogFilter filter
= new LogFilter();
146 filter
.readExternal(filterElement
);
147 setFilterSelected(filter
, Boolean
.parseBoolean(filterElement
.getAttributeValue(IS_ACTIVE
)));
149 DefaultJDOMExternalizer
.readExternal(this, object
);
151 catch (InvalidDataException e
) {
156 public void registerFilter(LogFilter filter
) {
157 myRegisteredLogFilters
.put(filter
, Boolean
.FALSE
);
160 public List
<LogFilter
> getRegisteredLogFilters() {
161 return new ArrayList
<LogFilter
>(myRegisteredLogFilters
.keySet());
164 public boolean isFilterSelected(LogFilter filter
) {
165 final Boolean isSelected
= myRegisteredLogFilters
.get(filter
);
166 if (isSelected
!= null) {
167 return isSelected
.booleanValue();
169 if (filter
instanceof IndependentLogFilter
) {
170 return ((IndependentLogFilter
)filter
).isSelected();
175 public void setFilterSelected(LogFilter filter
, boolean state
) {
176 if (filter
instanceof IndependentLogFilter
) {
177 ((IndependentLogFilter
)filter
).selectFilter();
179 else if (myRegisteredLogFilters
.containsKey(filter
)) {
180 myRegisteredLogFilters
.put(filter
, state
);
182 fireStateChanged(filter
);
185 public void selectOnlyFilter(LogFilter filter
) {
186 for (LogFilter logFilter
: myRegisteredLogFilters
.keySet()) {
187 myRegisteredLogFilters
.put(logFilter
, false);
189 setFilterSelected(filter
, true);
192 private void fireStateChanged(final LogFilter filter
) {
193 for (LogFilterListener listener
: myListeners
) {
194 listener
.onFilterStateChange(filter
);
198 private void fireStateChanged() {
199 for (LogFilterListener listener
: myListeners
) {
200 listener
.onTextFilterChange();
204 public void addFilterListener(LogFilterListener l
) {
208 public void removeFilterListener(LogFilterListener l
) {
209 myListeners
.remove(l
);