View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.log4j.chainsaw;
18  
19  import java.awt.Color;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.log4j.helpers.Constants;
25  import org.apache.log4j.rule.Rule;
26  import org.apache.log4j.spi.LoggingEvent;
27  
28  /**
29   * Wrap access to a LoggingEvent.  All property updates need to go through this object and not through the wrapped logging event,
30   * since the properties are shared by two views of the same backing LoggingEvent, and loggingEvent itself creates a copy of passed-in properties..
31   *
32   * Property reads can be made on the actual LoggingEvent.
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    //set to the log4jid value via setId - assumed to never change
44    private int id;
45  
46    private boolean searchMatch = false;
47    //a Map of event fields to Sets of string matches (can be used to render matches differently)
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     if (id != that.id) {
177       return false;
178     }
179 
180     return true;
181   }
182 
183   public int hashCode() {
184     return id;
185   }
186 
187   public String toString() {
188     return "LoggingEventWrapper - id: " + id + " background: " + getBackground() + ", foreground: " + getForeground() + ", msg: " + loggingEvent.getMessage();
189   }
190 }