1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.chainsaw;
18
19 import org.apache.log4j.helpers.Constants;
20 import org.apache.log4j.rule.Rule;
21 import org.apache.log4j.spi.LoggingEvent;
22
23 import java.awt.*;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27
28
29
30
31
32
33
34 public class LoggingEventWrapper {
35 private final LoggingEvent loggingEvent;
36 private static final int DEFAULT_HEIGHT = -1;
37
38 private Color colorRuleBackground = ChainsawConstants.COLOR_DEFAULT_BACKGROUND;
39 private Color colorRuleForeground = ChainsawConstants.COLOR_DEFAULT_FOREGROUND;
40 private int markerHeight = DEFAULT_HEIGHT;
41 private int msgHeight = DEFAULT_HEIGHT;
42
43
44 private int id;
45
46 private boolean searchMatch = false;
47
48 Map eventMatches = new HashMap();
49 private LoggingEventWrapper syncWrapper;
50 private boolean displayed;
51
52 public LoggingEventWrapper(LoggingEvent loggingEvent) {
53 this.loggingEvent = loggingEvent;
54 }
55
56 public LoggingEventWrapper(LoggingEventWrapper loggingEventWrapper) {
57 this.loggingEvent = loggingEventWrapper.getLoggingEvent();
58 this.id = loggingEventWrapper.id;
59 this.syncWrapper = loggingEventWrapper;
60 loggingEventWrapper.syncWrapper = this;
61 }
62
63 public LoggingEvent getLoggingEvent() {
64 return loggingEvent;
65 }
66
67 public void setProperty(String propName, String propValue) {
68 loggingEvent.setProperty(propName, propValue);
69 if (id == 0 && propName.equals(Constants.LOG4J_ID_KEY)) {
70 id = Integer.parseInt(propValue);
71 }
72 if (syncWrapper != null && !propName.equals(ChainsawConstants.MILLIS_DELTA_COL_NAME_LOWERCASE)) {
73 syncWrapper.getLoggingEvent().setProperty(propName, propValue);
74 }
75 }
76
77 public Object removeProperty(String propName) {
78 Object result = loggingEvent.removeProperty(propName);
79 if (syncWrapper != null && !propName.equals(ChainsawConstants.MILLIS_DELTA_COL_NAME_LOWERCASE)) {
80 syncWrapper.getLoggingEvent().removeProperty(propName);
81 }
82 return result;
83 }
84
85 public Set getPropertyKeySet() {
86 return loggingEvent.getPropertyKeySet();
87 }
88
89 public void updateColorRuleColors(Color backgroundColor, Color foregroundColor) {
90 if (backgroundColor != null && foregroundColor != null) {
91 this.colorRuleBackground = backgroundColor;
92 this.colorRuleForeground = foregroundColor;
93 if (syncWrapper != null) {
94 syncWrapper.colorRuleBackground = this.colorRuleBackground;
95 syncWrapper.colorRuleForeground = this.colorRuleForeground;
96 }
97 } else {
98 this.colorRuleBackground = ChainsawConstants.COLOR_DEFAULT_BACKGROUND;
99 this.colorRuleForeground = ChainsawConstants.COLOR_DEFAULT_FOREGROUND;
100 if (syncWrapper != null) {
101 syncWrapper.colorRuleBackground = this.colorRuleBackground;
102 syncWrapper.colorRuleForeground = this.colorRuleForeground;
103 }
104 }
105 }
106
107 public void evaluateSearchRule(Rule searchRule) {
108 eventMatches.clear();
109 searchMatch = searchRule != null && searchRule.evaluate(loggingEvent, eventMatches);
110 }
111
112 public Map getSearchMatches() {
113 return eventMatches;
114 }
115
116 public Color getForeground() {
117 return colorRuleForeground;
118 }
119
120 public Color getBackground() {
121 return colorRuleBackground;
122 }
123
124 public Color getColorRuleBackground() {
125 return colorRuleBackground;
126 }
127
128 public Color getColorRuleForeground() {
129 return colorRuleForeground;
130 }
131
132 public boolean isSearchMatch() {
133 return searchMatch;
134 }
135
136 public void setMarkerHeight(int markerHeight) {
137 this.markerHeight = markerHeight;
138 }
139
140 public int getMarkerHeight() {
141 return markerHeight;
142 }
143
144 public void setMsgHeight(int msgHeight) {
145 this.msgHeight = msgHeight;
146 }
147
148 public int getMsgHeight() {
149 return msgHeight;
150 }
151
152 public void setDisplayed(boolean b) {
153 markerHeight = DEFAULT_HEIGHT;
154 msgHeight = DEFAULT_HEIGHT;
155 displayed = b;
156 }
157
158 public void setPreviousDisplayedEventTimestamp(long previousDisplayedEventTimeStamp) {
159 setProperty(ChainsawConstants.MILLIS_DELTA_COL_NAME_LOWERCASE, String.valueOf(loggingEvent.getTimeStamp() - previousDisplayedEventTimeStamp));
160 }
161
162 public boolean isDisplayed() {
163 return displayed;
164 }
165
166 public boolean equals(Object o) {
167 if (this == o) {
168 return true;
169 }
170 if (o == null || getClass() != o.getClass()) {
171 return false;
172 }
173
174 LoggingEventWrapper that = (LoggingEventWrapper) o;
175
176 return id == that.id;
177 }
178
179 public int hashCode() {
180 return id;
181 }
182
183 public String toString() {
184 return "LoggingEventWrapper - id: " + id + " background: " + getBackground() + ", foreground: " + getForeground() + ", msg: " + loggingEvent.getMessage();
185 }
186 }