Logging function name, line, file...

Bruno Medeiros brunodomedeiros+spam at com.gmail
Mon Mar 24 07:17:21 PDT 2008


Jaro wrote:
> Is it possible to log file name, line, and function name in D like it was in C++ when we could hide __FILE__, __LINE__, __FUNCTION__ in a new logging macro? That way this information was printed in log file, and was extremely useful.

You can use mixins to achieve that. Not as direct as C++ macros, but 
it's the cleanest you get:

-----  -----
import std.stdio;
import std.string;


void log(string msg) {
	writefln("LOG: " ~ msg);
}

string logm(string msg) {
	return `log(__FILE__ ~":"~ toString(__LINE__) ~ " `~msg~`");`;
}

void main()
{
  	mixin(logm("entering main!"));
	auto numbers = [4, 8, 15, 16, 23, 42];
	mixin(logm("exiting main!"));
}

-----  -----
Outputs:

LOG: src\test.d:15 entering main!
LOG: src\test.d:17 exiting main!

Is this what you intended?

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list