132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: May 2004
|
|
8
|
|
9 author: Kris
|
|
10 Anders F Bjorklund (Darwin patches)
|
|
11
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module tango.util.log.Event;
|
|
15
|
|
16 version = UseEventFreeList;
|
|
17
|
|
18 private import tango.time.Clock;
|
|
19
|
|
20 private import tango.sys.Common;
|
|
21
|
|
22 private import tango.core.Exception;
|
|
23
|
|
24 private import tango.util.log.model.ILevel,
|
|
25 tango.util.log.model.IHierarchy;
|
|
26
|
|
27
|
|
28 version (Win32)
|
|
29 {
|
|
30 extern(Windows) int QueryPerformanceCounter(ulong *count);
|
|
31 extern(Windows) int QueryPerformanceFrequency(ulong *frequency);
|
|
32 }
|
|
33
|
|
34
|
|
35 /*******************************************************************************
|
|
36
|
|
37 Contains all information about a logging event, and is passed around
|
|
38 between methods once it has been determined that the invoking logger
|
|
39 is enabled for output.
|
|
40
|
|
41 Note that Event instances are maintained in a freelist rather than
|
|
42 being allocated each time, and they include a scratchpad area for
|
|
43 EventLayout formatters to use.
|
|
44
|
|
45 *******************************************************************************/
|
|
46
|
|
47 public class Event : ILevel
|
|
48 {
|
|
49 // primary event attributes
|
|
50 private char[] msg,
|
|
51 name;
|
|
52 private Time time;
|
|
53 private Level level;
|
|
54 private IHierarchy hierarchy;
|
|
55
|
|
56 // timestamps
|
|
57 private static Time beginTime;
|
|
58
|
|
59 // scratch buffer for constructing output strings
|
|
60 struct Scratch
|
|
61 {
|
|
62 uint length;
|
|
63 char[256] content;
|
|
64 }
|
|
65 package Scratch scratch;
|
|
66
|
|
67
|
|
68 // logging-level names
|
|
69 package static char[][] LevelNames =
|
|
70 [
|
|
71 "Trace ", "Info ", "Warn ", "Error ", "Fatal ", "None "
|
|
72 ];
|
|
73
|
|
74 version (Win32)
|
|
75 {
|
|
76 private static double multiplier;
|
|
77 private static ulong timerStart;
|
|
78 }
|
|
79
|
|
80 /***********************************************************************
|
|
81
|
|
82 Support for free-list
|
|
83
|
|
84 ***********************************************************************/
|
|
85
|
|
86 version (UseEventFreeList)
|
|
87 {
|
|
88 /***************************************************************
|
|
89
|
|
90 Instance variables for free-list support
|
|
91
|
|
92 ***************************************************************/
|
|
93
|
|
94 private Event next;
|
|
95 private static Event freelist;
|
|
96
|
|
97 /***************************************************************
|
|
98
|
|
99 Allocate an Event from a list rather than
|
|
100 creating a new one
|
|
101
|
|
102 ***************************************************************/
|
|
103
|
|
104 static final synchronized Event allocate ()
|
|
105 {
|
|
106 Event e;
|
|
107
|
|
108 if (freelist)
|
|
109 {
|
|
110 e = freelist;
|
|
111 freelist = e.next;
|
|
112 }
|
|
113 else
|
|
114 e = new Event ();
|
|
115 return e;
|
|
116 }
|
|
117
|
|
118 /***************************************************************
|
|
119
|
|
120 Return this Event to the free-list
|
|
121
|
|
122 ***************************************************************/
|
|
123
|
|
124 static final synchronized void deallocate (Event e)
|
|
125 {
|
|
126 e.next = freelist;
|
|
127 freelist = e;
|
|
128
|
|
129 version (EventReset)
|
|
130 e.reset;
|
|
131 }
|
|
132 }
|
|
133
|
|
134 /***********************************************************************
|
|
135
|
|
136 Setup timing information for later use
|
|
137
|
|
138 ***********************************************************************/
|
|
139
|
|
140 package static void initialize ()
|
|
141 {
|
|
142 version (Posix)
|
|
143 {
|
|
144 beginTime = Clock.now;
|
|
145 }
|
|
146
|
|
147 version (Win32)
|
|
148 {
|
|
149 ulong freq;
|
|
150
|
|
151 if (! QueryPerformanceFrequency (&freq))
|
|
152 throw new PlatformException ("high-resolution timer is not available");
|
|
153
|
|
154 QueryPerformanceCounter (&timerStart);
|
|
155 multiplier = cast(double) TimeSpan.TicksPerSecond / freq;
|
|
156 beginTime = Clock.now;
|
|
157
|
|
158 }
|
|
159 }
|
|
160
|
|
161 /***********************************************************************
|
|
162
|
|
163 Return time when the executable started
|
|
164
|
|
165 ***********************************************************************/
|
|
166
|
|
167 final static Time startedAt ()
|
|
168 {
|
|
169 return beginTime;
|
|
170 }
|
|
171
|
|
172 /***********************************************************************
|
|
173
|
|
174 Return the current time
|
|
175
|
|
176 ***********************************************************************/
|
|
177
|
|
178 final static Time timer ()
|
|
179 {
|
|
180 version (Posix)
|
|
181 {
|
|
182 return Clock.now;
|
|
183 }
|
|
184
|
|
185 version (Win32)
|
|
186 {
|
|
187 ulong now;
|
|
188
|
|
189 QueryPerformanceCounter (&now);
|
|
190 return beginTime + TimeSpan(cast(long)((now - timerStart) * multiplier));
|
|
191 }
|
|
192 }
|
|
193
|
|
194 /***********************************************************************
|
|
195
|
|
196 Set the various attributes of this event.
|
|
197
|
|
198 ***********************************************************************/
|
|
199
|
|
200 final void set (IHierarchy hierarchy, Level level, char[] msg, char[] name)
|
|
201 {
|
|
202 this.hierarchy = hierarchy;
|
|
203 this.time = timer();
|
|
204 this.level = level;
|
|
205 this.name = name;
|
|
206 this.msg = msg;
|
|
207 }
|
|
208
|
|
209 version (EventReset)
|
|
210 {
|
|
211 /***************************************************************
|
|
212
|
|
213 Reset this event
|
|
214
|
|
215 ***************************************************************/
|
|
216
|
|
217 final void reset ()
|
|
218 {
|
|
219 time = 0;
|
|
220 msg = null;
|
|
221 name = null;
|
|
222 level = Level.None;
|
|
223 }
|
|
224 }
|
|
225
|
|
226 /***********************************************************************
|
|
227
|
|
228 Return the message attached to this event.
|
|
229
|
|
230 ***********************************************************************/
|
|
231
|
|
232 final override char[] toString ()
|
|
233 {
|
|
234 return msg;
|
|
235 }
|
|
236
|
|
237 /***********************************************************************
|
|
238
|
|
239 Return the name of the logger which produced this event
|
|
240
|
|
241 ***********************************************************************/
|
|
242
|
|
243 final char[] getName ()
|
|
244 {
|
|
245 return name;
|
|
246 }
|
|
247
|
|
248 /***********************************************************************
|
|
249
|
|
250 Return the scratch buffer for formatting. This is a thread
|
|
251 safe place to format data within, without allocating any
|
|
252 memory.
|
|
253
|
|
254 ***********************************************************************/
|
|
255
|
|
256 final char[] getContent ()
|
|
257 {
|
|
258 return scratch.content [0..scratch.length];
|
|
259 }
|
|
260
|
|
261 /***********************************************************************
|
|
262
|
|
263 Return the logger level of this event.
|
|
264
|
|
265 ***********************************************************************/
|
|
266
|
|
267 final Level getLevel ()
|
|
268 {
|
|
269 return level;
|
|
270 }
|
|
271
|
|
272 /***********************************************************************
|
|
273
|
|
274 Return the logger level name of this event.
|
|
275
|
|
276 ***********************************************************************/
|
|
277
|
|
278 final char[] getLevelName ()
|
|
279 {
|
|
280 return LevelNames[level];
|
|
281 }
|
|
282
|
|
283 /***********************************************************************
|
|
284
|
|
285 Return the hierarchy where the event was produced from
|
|
286
|
|
287 ***********************************************************************/
|
|
288
|
|
289 final IHierarchy getHierarchy ()
|
|
290 {
|
|
291 return hierarchy;
|
|
292 }
|
|
293
|
|
294 /***********************************************************************
|
|
295
|
|
296 Return the time this event was produced, relative to the
|
|
297 start of this executable
|
|
298
|
|
299 ***********************************************************************/
|
|
300
|
|
301 final TimeSpan getSpan ()
|
|
302 {
|
|
303 return time - beginTime;
|
|
304 }
|
|
305
|
|
306 /***********************************************************************
|
|
307
|
|
308 Return the time this event was produced relative to Epoch
|
|
309
|
|
310 ***********************************************************************/
|
|
311
|
|
312 final Time getTime ()
|
|
313 {
|
|
314 return time;
|
|
315 }
|
|
316
|
|
317 /***********************************************************************
|
|
318
|
|
319 Append some content to the scratch buffer. This is limited
|
|
320 to the size of said buffer, and will not expand further.
|
|
321
|
|
322 ***********************************************************************/
|
|
323
|
|
324 final Event append (char[] x)
|
|
325 {
|
|
326 uint addition = x.length;
|
|
327 uint newLength = scratch.length + x.length;
|
|
328
|
|
329 if (newLength < scratch.content.length)
|
|
330 {
|
|
331 scratch.content [scratch.length..newLength] = x[0..addition];
|
|
332 scratch.length = newLength;
|
|
333 }
|
|
334 return this;
|
|
335 }
|
|
336 }
|