Logging and tracing in D

evilrat evilrat666 at gmail.com
Tue Aug 6 10:03:41 PDT 2013


On Tuesday, 6 August 2013 at 04:35:39 UTC, Andre Artus wrote:
> What is the recommended approach for adding logging and tracing 
> to D apps?
> Is there a library for it?

custom very simple yet powerful logging can be achieved with 
templates and debug specifier, something like this:
--------------------
module logger;
import std.datetime;
import std.stdio : writefln;

template log(T) {
  auto timeString = Clock.currTime().toISOExtString();
  writefln("log message from(%s)[+%s]: %s", __FUNCTION__, 
timeString, T);
}
--------------------

then in ur code just place with debug specifier:
--------------------
import logger;

void main {
  debug log!"my stuff";
}
--------------------

and you get pretty formatted message in console(add log file 
writer if needed), and you get "smart" logging for free, in that 
way it will be generated only for debug mode(in release it will 
be skipped because of debug specifier).


More information about the Digitalmars-d-learn mailing list