1 /*******************************************************************************
2 
3         Copyright:
4             Copyright (c) 2004 Kris Bell.
5             Some parts copyright (c) 2009-2016 dunnhumby Germany GmbH.
6             All rights reserved.
7 
8         License:
9             Tango Dual License: 3-Clause BSD License / Academic Free License v3.0.
10             See LICENSE_TANGO.txt for details.
11 
12         Version: Initial release: May 2004
13 
14         Authors: Kris
15 
16 *******************************************************************************/
17 
18 module ocean.util.log.LayoutDate;
19 
20 import ocean.transition;
21 
22 import ocean.text.Util;
23 
24 import ocean.time.Clock,
25        ocean.time.WallClock;
26 
27 import ocean.util.log.Appender;
28 import ocean.util.log.Event;
29 
30 import Integer = ocean.text.convert.Integer_tango;
31 
32 /*******************************************************************************
33 
34         A layout with ISO-8601 date information prefixed to each message
35 
36 *******************************************************************************/
37 
38 public class LayoutDate : Appender.Layout
39 {
40         private bool localTime;
41 
42         /***********************************************************************
43 
44                 Ctor with indicator for local vs UTC time. Default is
45                 local time.
46 
47         ***********************************************************************/
48 
49         this (bool localTime = true)
50         {
51                 this.localTime = localTime;
52         }
53 
54         /***********************************************************************
55 
56                 Subclasses should implement this method to perform the
57                 formatting of the actual message content.
58 
59         ***********************************************************************/
60 
61     public override void format (LogEvent event, scope void delegate(cstring) dg)
62     {
63         auto level = event.levelName;
64 
65         // convert time to field values
66         auto tm = event.time;
67         auto dt = (localTime) ? WallClock.toDate(tm) : Clock.toDate(tm);
68 
69         // format date according to ISO-8601 (lightweight formatter)
70         char[20] tmp = void;
71         char[256] tmp2 = void;
72         dg(layout(tmp2, "%0-%1-%2 %3:%4:%5,%6 %7 [%8] - ",
73                   convert(tmp[0..4],   dt.date.year),
74                   convert(tmp[4..6],   dt.date.month),
75                   convert(tmp[6..8],   dt.date.day),
76                   convert(tmp[8..10],  dt.time.hours),
77                   convert(tmp[10..12], dt.time.minutes),
78                   convert(tmp[12..14], dt.time.seconds),
79                   convert(tmp[14..17], dt.time.millis),
80                   level,
81                   event.name
82                ));
83         dg(event.toString);
84     }
85 
86         /**********************************************************************
87 
88                 Convert an integer to a zero prefixed text representation
89 
90         **********************************************************************/
91 
92         private cstring convert (char[] tmp, long i)
93         {
94                 return Integer.formatter (tmp, i, 'u', '?', 8);
95         }
96 }