1 /*
2 * Copyright (c) 1998-2005 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18 package jgroup.util.log;
19
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.Serializable;
23 import java.net.InetAddress;
24
25 import jgroup.util.Network;
26
27 /**
28 * A general event that can be logged. This class is designed to be
29 * subclassed, and it should be noted that subclassing requires that
30 * the subclass be serializable to be written to disk. Since this class
31 * does implement <code>Serializable</code> the subclasses do not need
32 * to do that.
33 *
34 * @author Rune Vestvik
35 * @author Hein Meling
36 * @since Jgroup 3.0
37 */
38 public class Event
39 implements Comparable<Event>, Serializable
40 {
41
42 ////////////////////////////////////////////////////////////////////////////////////////////
43 // Constants
44 ////////////////////////////////////////////////////////////////////////////////////////////
45
46 private static final long serialVersionUID = 4121134753270542643L;
47
48 /**
49 * The time of the event to which other events should be relative to.
50 * If not set, the output generated for the events will be the actual
51 * time values of the logged events.
52 */
53 private static long timeOfRelativeEvent = 0;
54
55 /** The length of the longest hostname ever recorded */
56 private static int longestHostname = 0;
57
58
59 ////////////////////////////////////////////////////////////////////////////////////////////
60 // Event fields
61 ////////////////////////////////////////////////////////////////////////////////////////////
62
63 protected long time;
64
65 protected EventType type;
66
67 protected String description;
68
69 private String hostname;
70
71 protected InetAddress eventHost;
72
73
74 ////////////////////////////////////////////////////////////////////////////////////////////
75 // Constructors
76 ////////////////////////////////////////////////////////////////////////////////////////////
77
78 /** Super constructor for sub events with late initialization */
79 public Event()
80 {
81 }
82
83 public Event(EventType type, String description)
84 {
85 this(System.currentTimeMillis(), type, description);
86 }
87
88 public Event(long time, EventType type, String description)
89 {
90 this.time = time;
91 this.type = type;
92 this.description = description;
93 this.hostname = Network.getLocalMachineName();
94 this.eventHost = Network.getLocalHost();
95 }
96
97
98 ////////////////////////////////////////////////////////////////////////////////////////////
99 // Access methods
100 ////////////////////////////////////////////////////////////////////////////////////////////
101
102 /**
103 * @return Returns the occurrance time of this event
104 */
105 public long getTime()
106 {
107 return time;
108 }
109
110 /**
111 * Set the time value of this event; used to adjust the time for events that
112 * was logged on machines with incorrect clock.
113 *
114 * @param time the occurrance time of this event
115 */
116 public void setTime(long time)
117 {
118 this.time = time;
119 }
120
121 /**
122 * @return Returns the type of this event
123 */
124 public EventType getType()
125 {
126 return type;
127 }
128
129 /**
130 * @return Returns the description of this event
131 */
132 public String getDescription()
133 {
134 return description;
135 }
136
137 /**
138 * @return Returns the hostname.
139 */
140 public String getHostname()
141 {
142 return hostname;
143 }
144
145 /**
146 * @return Returns the host on which this event was generated.
147 */
148 public InetAddress getHost()
149 {
150 return eventHost;
151 }
152
153 /**
154 * Returns true if the given type is the same as the type of this instance.
155 */
156 public boolean isType(EventType theType)
157 {
158 return theType == type;
159 }
160
161 /**
162 * This method must be overriden by the event type which will be used to indicate
163 * that it is the last event.
164 *
165 * @return Always returns false.
166 */
167 public boolean isLastEvent()
168 {
169 return false;
170 }
171
172 /**
173 * Set the event whose time will be the used to compute relative time
174 * of consecutive events. Typically, the first event should be the
175 * relative event.
176 *
177 * @param relativeEvent
178 */
179 public static void setRelativeTo(Event relativeEvent)
180 {
181 timeOfRelativeEvent = relativeEvent.time;
182 }
183
184
185 ////////////////////////////////////////////////////////////////////////////////////////////
186 // Methods from Object
187 ////////////////////////////////////////////////////////////////////////////////////////////
188
189 public String toString()
190 {
191 StringBuilder buf = commonToString(true);
192 return buf.toString();
193 }
194
195 protected StringBuilder commonToString(boolean includeDescription)
196 {
197 StringBuilder buf = new StringBuilder();
198 buf.append(String.format("%6d", time - timeOfRelativeEvent));
199 buf.append(" (");
200 buf.append(hostname);
201 int pad = longestHostname - hostname.length();
202 for (int i = 0; i < pad; i++) {
203 buf.append(' ');
204 }
205 buf.append("): ");
206 if (type != null) {
207 buf.append(type);
208 }
209 if (includeDescription && description.length() > 0) {
210 buf.append(' ');
211 buf.append(description);
212 }
213 return buf;
214 }
215
216
217 ////////////////////////////////////////////////////////////////////////////////////////////
218 // Methods from Comparable
219 ////////////////////////////////////////////////////////////////////////////////////////////
220
221 /**
222 * Allows the system to compare two events to determine which are first
223 * in some ordering; hence making it simple to sort the events.
224 */
225 public int compareTo(Event event)
226 {
227 if (time > event.time)
228 return 1;
229 else if (time < event.time)
230 return -1;
231 return 0;
232 }
233
234
235 ////////////////////////////////////////////////////////////////////////////////////////////
236 // Serialization Methods
237 ////////////////////////////////////////////////////////////////////////////////////////////
238
239 /**
240 * Override readObject to allow us to compute the length of longest hostname so that
241 * we can do pretty printing.
242 */
243 private void readObject(ObjectInputStream in)
244 throws IOException, ClassNotFoundException
245 {
246 in.defaultReadObject();
247 if (hostname != null) {
248 int hostnameLen = hostname.length();
249 if (longestHostname < hostnameLen)
250 longestHostname = hostnameLen;
251 }
252 }
253
254 } // END Event