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
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.util.log.DateLayout;
|
|
14
|
|
15 private import tango.text.Util;
|
|
16
|
|
17 private import tango.time.Clock,
|
|
18 tango.time.WallClock;
|
|
19
|
|
20 private import tango.text.convert.Integer;
|
|
21
|
|
22 private import tango.util.log.Event,
|
|
23 tango.util.log.EventLayout;
|
|
24
|
|
25 /*******************************************************************************
|
|
26
|
|
27 A layout with ISO-8601 date information prefixed to each message
|
|
28
|
|
29 *******************************************************************************/
|
|
30
|
|
31 public class DateLayout : EventLayout
|
|
32 {
|
|
33 private bool localTime;
|
|
34
|
|
35 private static char[6] spaces = ' ';
|
|
36
|
|
37 /***********************************************************************
|
|
38
|
|
39 Ctor with indicator for local vs UTC time. Default is
|
|
40 local time.
|
|
41
|
|
42 ***********************************************************************/
|
|
43
|
|
44 this (bool localTime = true)
|
|
45 {
|
|
46 this.localTime = localTime;
|
|
47 }
|
|
48
|
|
49 /***********************************************************************
|
|
50
|
|
51 Format message attributes into an output buffer and return
|
|
52 the populated portion.
|
|
53
|
|
54 ***********************************************************************/
|
|
55
|
|
56 char[] header (Event event)
|
|
57 {
|
|
58 char[] level = event.getLevelName;
|
|
59
|
|
60 // convert time to field values
|
|
61 auto tm = event.getTime;
|
|
62 auto dt = (localTime) ? WallClock.toDate(tm) : Clock.toDate(tm);
|
|
63
|
|
64 // format date according to ISO-8601 (lightweight formatter)
|
|
65 char[20] tmp = void;
|
|
66 return layout (event.scratch.content, "%0-%1-%2 %3:%4:%5,%6 %7%8 %9 - ",
|
|
67 convert (tmp[0..4], dt.date.year),
|
|
68 convert (tmp[4..6], dt.date.month),
|
|
69 convert (tmp[6..8], dt.date.day),
|
|
70 convert (tmp[8..10], dt.time.hours),
|
|
71 convert (tmp[10..12], dt.time.minutes),
|
|
72 convert (tmp[12..14], dt.time.seconds),
|
|
73 convert (tmp[14..17], dt.time.millis),
|
|
74 spaces [0 .. $-level.length],
|
|
75 level,
|
|
76 event.getName
|
|
77 );
|
|
78 }
|
|
79
|
|
80 /**********************************************************************
|
|
81
|
|
82 Convert an integer to a zero prefixed text representation
|
|
83
|
|
84 **********************************************************************/
|
|
85
|
|
86 private char[] convert (char[] tmp, int i)
|
|
87 {
|
|
88 return format (tmp, cast(long) i, Style.Unsigned, Flags.Zero);
|
|
89 }
|
|
90 }
|