www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can a call to pragma(msg, __FILE__, ...) be mixin templatized?

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I'm using

     pragma(msg, __FILE__, "(", __LINE__, ",1): Debug: ", "A 
useful debug message");

to print compile-time information formatted as standard compiler 
diagnostics.

These are picked up by Emacs Flycheck and overlayed in the editor 
and listen in the *Flycheck errors* buffer. Very convenient. When 
I want to get the type of something at compile-time.

In order to not having to repeat oneself I'm now looking for a 
way to extract this into a `mixin template`. Is this possible 
somehow and still preserve the instantiation site values of 
`__FILE__` and `__LINE__`?
Aug 17 2020
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Monday, 17 August 2020 at 21:18:41 UTC, Per Nordlöw wrote:
 I'm using

     pragma(msg, __FILE__, "(", __LINE__, ",1): Debug: ", "A 
 useful debug message");

 to print compile-time information formatted as standard 
 compiler diagnostics.

 These are picked up by Emacs Flycheck and overlayed in the 
 editor and listen in the *Flycheck errors* buffer. Very 
 convenient. When I want to get the type of something at 
 compile-time.

 In order to not having to repeat oneself I'm now looking for a 
 way to extract this into a `mixin template`. Is this possible 
 somehow and still preserve the instantiation site values of 
 `__FILE__` and `__LINE__`?
mixin template ctLog(string msg, string file = __FILE__, size_t line = __LINE__) { pragma(msg, file, "(", line, "): ", msg); } mixin ctLog!"Module scope"; unittest { mixin ctLog!"function scope"; } struct S { mixin ctLog!"Struct scope"; } This works for me. -- Simen
Aug 17 2020
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 17 August 2020 at 22:37:12 UTC, Simen Kjærås wrote:
 mixin template ctLog(string msg, string file = __FILE__, size_t 
 line = __LINE__) {
     pragma(msg, file, "(", line, "): ", msg);
 }
Thanks. Forgot to mention that I want to support variadic arguments to `ctLog` similar to what is done with void dbg(Args...)(Args args, const string file = __FILE_FULL_PATH__, const uint line = __LINE__, const string fun = __FUNCTION__) safe pure nothrow nogc { import std.stdio : stderr, writeln; try debug stderr.writeln(file, "(", line, ",1):", " Debug: \"", args, "\""); catch (Exception) { } } Is this possible?
Aug 18 2020
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 18 August 2020 at 08:03:04 UTC, Per Nordlöw wrote:
 Forgot to mention that I want to support variadic arguments to 
 `ctLog` similar to what is done with
And these arguments should be of any template argument kind, not only a compile-time string.
Aug 18 2020
parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Tuesday, 18 August 2020 at 08:05:20 UTC, Per Nordlöw wrote:
 On Tuesday, 18 August 2020 at 08:03:04 UTC, Per Nordlöw wrote:
 Forgot to mention that I want to support variadic arguments to 
 `ctLog` similar to what is done with
And these arguments should be of any template argument kind, not only a compile-time string.
I'm not a fan of string mixins (ask Adam how they're the scourge of good programming, a wart on D's behind, and so on :p), but I found no good way to do this without them: string ctLog(Args...)(string file = __FILE__, size_t line = __LINE__) { import std.conv : to; string result = `pragma(msg, "`~file~`(", `~line.to!string~`, "): "`; static foreach (e; Args) { result ~= `, `~e.stringof; } return result~`);`; } mixin(ctLog!("This ", "is ", "module ", "scope.")); unittest { mixin(ctLog!"function scope"); } struct S { mixin(ctLog!"Struct scope"); } -- Simen
Aug 18 2020