"Nick Sabalausky" <[email protected]> wrote in message news:[email protected]... > "Robert Jacques" <[email protected]> wrote in message > news:[email protected]... >> On Mon, 07 Sep 2009 12:41:46 -0400, Byron Heads >> <[email protected]> wrote: >>> Also is there a nice way to replace logging macros? I often have a >>> logger >>> that looks like >>> logmsg( int level, uint line, char* func, char* file, char[] msg, ... ); >>> >>> then I would write some macros like: >>> #define LOGDEBUG( m, ... ) logmsg( LVL_DEBUG, __LINE__, __func__, >>> __FILE__, msg, __VA_ARGS__ ) >>> >>> thus i would only have to do >>> LOGDEBUG( "SOME STRING with formatting", formatting args.. ) >>> > > Templates to the rescue! :) From my "SemiTwist D Tools" ( > http://www.dsource.org/projects/semitwist ) : > > --------------------------------------------------------- > template trace(char[] prefix="") > { > static if(prefix=="") > const char[] trace = > `Stdout.formatln("{}({}): trace", __FILE__, __LINE__);`; > else > const char[] trace = > `Stdout.formatln("{}: {}({}): trace", `~prefix.stringof~`, __FILE__, > __LINE__);`; > //pragma(msg, "trace: " ~ trace); > } > --------------------------------------------------------- > > That can, of course, be adjusted to do whatever logging you need instead > of just tracing to Stdout. >
Also, keep in mind that because of DMD issue #2887 ( http://d.puremagic.com/issues/show_bug.cgi?id=2887 ), __LINE__ needs adjusted when used anywhere after the first line of a multi-line string mixin: template foo() { const char[] foo = `{ auto __foo_line = __LINE__; // must be on first line, and the '{' is needed in this case Stdout.formatln("Foo:"); Stdout.formatln(" File {}", __FILE__); Stdout.formatln(" Line {}", __foo_line); }`; }
