Implementing std.log

Jonathan M Davis jmdavisProg at gmx.com
Sat May 14 18:57:29 PDT 2011


On 2011-05-14 10:04, Andrei Alexandrescu wrote:
> On 5/9/11 1:52 AM, Andrei Alexandrescu wrote:
> [snip]
> 
> I updated my std.log draft. Added a lot of features including formatted
> writing, delayed logging, and a variety of configuration options.
> Replaced the redundant log.xyz with logXyz. The implementation is
> getting close to reviewable form.
> 
> Documentation:
> 
> http://d-programming-language.org/phobos-prerelease/std_log.html
> 
> Source:
> 
> https://github.com/andralex/phobos
> 
> Feedback welcome.

A minor note on efficiency. You're currently calling hour, minute, second, 
etc. on the result of Clock.currTime() (which is a SysTime) and doing so in a 
loop. Every one of those calls has to convert the internal representation in 
hnsecs to the units your asking for. It would be more efficient to take the 
result of Clock.currTime() and then save a DateTime and FracSec with the 
values from that SysTime. For example, instead of

auto t = Clock.currTime();
...
formattedWrite(writer, prefix, level,
                    t.month, t.day, t.hour, t.minute, t.second,
                    t.fracSec.usecs, tid, fdir, fname, n);

you could do

auto t = Clock.currTime();
auto dt = cast(DateTime)t;
auto fs = t.fracSec;
...
formattedWrite(writer, prefix, level,
                    dt.month, dt.day, dt.hour, dt.minute, dt.second,
                    fs.fracSec.usecs, tid, fdir, fname, n);

It's not a big deal. SysTime has all of the functions on it, and it works the 
way that you're doing it. It's just less efficienty since it has to do more 
calculations, whereas if you get a DateTime and FracSec from it, there are far 
fewer calcuations to do. Actually, I should probably add a note about that to 
SysTime's documentation. It should be fairly obvious if you sit down and think 
about it, but obviously people aren't usually going to be doing that, so it's 
likely unreasonable to expect people to figure that out on their own short 
actually needing to find ways to improve the efficiency of their code date-
time.

In any case, I just thought that I'd point that out. I'm not sure if it will 
really have much of an effect (especially when I/O is going to be the 
bottlneck for logging, not stray date-time calculations), but logging does 
need to be efficient.

- Jonathan M Davis


More information about the Digitalmars-d mailing list