1 /** 2 * Includes defaults such as the DefaultLogger 3 */ 4 module dlog.defaults; 5 6 import dlog.core : Logger; 7 import dlog.transform : MessageTransform; 8 import dlog.context : Context, CompilationInfo; 9 import std.conv : to; 10 11 /** 12 * DefaultLogger 13 * 14 * The default logger logs to standard output (fd 0) 15 */ 16 public final class DefaultLogger : Logger 17 { 18 this(string multiArgJoiner = " ") 19 { 20 /* Use the DefaultTransform */ 21 super(multiArgJoiner); 22 } 23 24 protected override void logImpl(string message) 25 { 26 import std.stdio : write; 27 write(message); 28 } 29 } 30 31 /** 32 * DefaultTransform 33 * 34 * Provides a transformation of the kind 35 * 36 * [date+time] (srcFile:lineNumber): message `\n` 37 */ 38 public final class DefaultTransform : MessageTransform 39 { 40 /** 41 * Performs the default transformation 42 * 43 * Params: 44 * text = text input to transform 45 * context = the context (if any) 46 * Returns: the transformed text 47 */ 48 public override string transform(string text, Context ctx) 49 { 50 /* Extract the context */ 51 string[] context = ctx.getLineInfo().toArray(); 52 53 string message; 54 55 /* Date and time */ 56 import std.datetime.systime : Clock, SysTime; 57 SysTime currTime = Clock.currTime(); 58 import std.conv : to; 59 string timestamp = to!(string)(currTime); 60 message = "["~timestamp~"]"; 61 62 /* Module information */ 63 message = message ~ "\t("; 64 message = message ~ context[1]~":"~context[2]; 65 message = message ~ "): "~text; 66 67 /* Add trailing newline */ 68 message = message ~ '\n'; 69 70 return message; 71 } 72 }