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