std.experimental.logger: practical observations

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 12 09:17:09 PDT 2014


Am Fri, 12 Sep 2014 09:46:18 +0000
schrieb "Robert burner Schadek" <rburners at gmail.com>:

> On Thursday, 11 September 2014 at 22:10:01 UTC, Marco Leise wrote:
> > Let me clarify. Here is some code from 2015:
> >
> > void main()
> > {
> > 	stdlog = new MyLogger();
> > 	// This call may overflow the stack if
> > 	// 'somethingBadHappened in someFunc():
> > 	error("ERROR!!!");
> > }
> >
> > class MyLogger : Logger
> > {
> > 	override void writeLogMsg(ref LogEntry payload)
> > 	{
> > 		auto bla = someFunc();
> > 		useBlaToLog(bla, payload.msg);
> > 	}
> > }
> >
> > // This is just some helper function unrelated to logging
> > // but it uses the stdlog functionality from Phobos itself
> > // as that is good practice in 2015.
> > auto someFunc()
> > {
> > 	...
> > 	if (somethingBadHappened)
> > 	{
> > 		// Now I must not be used myself in a logger
> > 		// implementation, or I overflow the stack!
> > 		error("something bad in someFunc");
> > 	}
> > 	...
> > }
> 
> well you could set the LogLevel to off and reset it afterwards

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.

> > 3. Exceptions and loggin don't mix.
> > How do you log errors that also throw exceptions ?
> 
> please elaborate. I think I misunderstand

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.

-- 
Marco



More information about the Digitalmars-d mailing list