Logging function name, line, file...
Bill Baxter
dnewsgroup at billbaxter.com
Mon Mar 24 08:00:07 PDT 2008
Bruno Medeiros wrote:
> 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?
>
Maybe this should go in a FAQ before it gets asked & answered another 5
times?
--bb
More information about the Digitalmars-d
mailing list