std.experimental.logger: practical observations
Marco Leise via Digitalmars-d
digitalmars-d at puremagic.com
Sun Sep 14 00:31:18 PDT 2014
Am Sat, 13 Sep 2014 14:34:16 +0000
schrieb "Robert burner Schadek" <rburners at gmail.com>:
> On Friday, 12 September 2014 at 16:08:42 UTC, Marco Leise wrote:
> >
> > Remember that the stdlog is __gshared? Imagine we set the
> > LogLevel to off and while executing writeLogMsg ...
> >
> > * a different thread wants to log a warning to stdlog
> > * a different thread wants to inspect/set the log level
> >
> > It is your design to have loggers shared between threads.
> > You should go all the way to make them thread safe.
> >
> > * catch recursive calls from within the same thread,
> > while not affecting other threads' logging
> > * make Logger a shared class and work with atomicLoad/Store,
> > a synchronized class or use the built-in monitor field
> > through synchronized(this) blocks.
>
> hm, I don't know of any magic pill for that. I guess this would
> require some dataflow analysis.
Why so complicated? In general - not specific to std.logger -
I'd wrap those calls in some function that acquires a mutex
and then check a recursion flag to abort the logging if this
thread has already been here.
synchronized(loggingMutex) {
if (isRecursion) return;
isRecursion = true;
scope(exit) isRecursion = false;
logger.writeLogMsg(...);
}
> > I know when to throw an exception, but I never used logging
> > much. If some function throws, would I also log the same
> > message with error() one line before the throw statement?
> > Or would I log at the place where I catch the exception?
> > What to do about the stack trace when I only have one line per
> > log entry?
> > You see, I am a total newbie when it comes to logging and from
> > the question that arose in my head I figured exceptions and
> > logging don't really mix. Maybe only info() and debug() should
> > be used and actual problems left to exception handling alone.
>
> that is depended on what your program requires. You can write
> more than one line, just indent it by a tab or two. again no
> magic pill as far as I know
Ok, I'll experiment a bit and see what works best.
--
Marco
More information about the Digitalmars-d
mailing list