std.experimental.logger: practical observations

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 11 15:18:28 PDT 2014


Am Thu, 11 Sep 2014 21:32:44 +0000
schrieb "Robert burner Schadek" <rburners at gmail.com>:

> On Thursday, 11 September 2014 at 16:55:32 UTC, Marco Leise wrote:
> > 2. I noticed that as my logger implementation grew more complex
> >    and used functionality from other modules I wrote, that if
> >    these used logging as well I'd easily end up in a recursive
> >    logging situation.
> >
> >    Can recursion checks be added somewhere
> >    before .writeLogMsg()?
> 
> I think I don't follow. Just to clear
> 
> foo() {
>      log(); bar();
> }
> 
> bar() {
>      log(); foo();
> }

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");
	}
	...
}

> > 3. Exceptions and loggin don't mix.
> >    Logging functions expect the file and line to be the one
> >    where the logging function is placed. When I work with C
> >    functions I tend to call them through a template that will
> >    check the error return code. See:
> >    http://dlang.org/phobos/std_exception.html#.errnoEnforce
> >    Such templates pick up file and line numbers from where
> >    they are instantiated and pass them on to the exception
> >    ctor as runtime values.
> >    Now when I use error(), I see no way to pass it runtime
> >    file and line variables to make the log file reflect the
> >    actual file and line where the error occured, instead of
> >    some line in the template or where ever I caught the
> >    exception.
> >    Not all errors/exceptions are fatal and we might just want
> >    to log an exception and continue with execution.
> 
> hm, I think adding template function as requested by dicebot 
> would solve that problem, as it would take line and friends as 
> function parameters

How do you log errors that also throw exceptions ?

-- 
Marco



More information about the Digitalmars-d mailing list