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.LayoutDate;
14 
15 private import  tango.text.Util;
16 
17 private import  tango.time.Clock,
18                 tango.time.WallClock;
19 
20 private import  tango.util.log.Log;
21 
22 private import  Integer = tango.text.convert.Integer;
23 
24 /*******************************************************************************
25 
26         A layout with ISO-8601 date information prefixed to each message
27        
28 *******************************************************************************/
29 
30 public class LayoutDate : Appender.Layout
31 {
32         private bool localTime;
33 
34         /***********************************************************************
35         
36                 Ctor with indicator for local vs UTC time. Default is 
37                 local time.
38                         
39         ***********************************************************************/
40 
41         this (bool localTime = true)
42         {
43                 this.localTime = localTime;
44         }
45 
46         /***********************************************************************
47                 
48                 Subclasses should implement this method to perform the
49                 formatting of the actual message content.
50 
51         ***********************************************************************/
52 
53         void format (LogEvent event, scope size_t delegate(const(void)[]) dg)
54         {
55                 const(char)[] level = event.levelName;
56                 
57                 // convert time to field values
58                 auto tm = event.time;
59                 auto dt = (localTime) ? WallClock.toDate(tm) : Clock.toDate(tm);
60                                 
61                 // format date according to ISO-8601 (lightweight formatter)
62                 char[20] tmp = void;
63                 char[256] tmp2 = void;
64                 dg (layout (tmp2, "%0-%1-%2 %3:%4:%5,%6 %7 [%8] - ", 
65                             convert (tmp[0..4],   dt.date.year),
66                             convert (tmp[4..6],   dt.date.month),
67                             convert (tmp[6..8],   dt.date.day),
68                             convert (tmp[8..10],  dt.time.hours),
69                             convert (tmp[10..12], dt.time.minutes),
70                             convert (tmp[12..14], dt.time.seconds),
71                             convert (tmp[14..17], dt.time.millis),
72                             level,
73                             event.name
74                             ));
75                 dg (event.toString());
76         }
77 
78         /**********************************************************************
79 
80                 Convert an integer to a zero prefixed text representation
81 
82         **********************************************************************/
83 
84         private char[] convert (char[] tmp, long i)
85         {
86                 return Integer.formatter (tmp, i, 'u', '?', 8);
87         }
88 }