February 02, 2019 Re: Convenient debug printing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Schluter | On 2019-02-02 13:08, Patrick Schluter wrote: > What's wrong with `debug log(<whatever>);` from `std.experimental.logger;`? > > Works like a charm in my project. The message is prepended with a time stamp and location in file/function/line. I think what's missing is the variable name not being automatically added to the message printed. -- /Jacob Carlborg |
February 02, 2019 Re: Convenient debug printing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Friday, 1 February 2019 at 15:49:24 UTC, Per Nordlöw wrote: > [...] Alright, after quite some work I finally managed to do the following code printing also nested variable names from classes & structs. I am not sure it's quite the same as the Rust AST macro for that, but I guess it's still interesting: import std.stdio; template dbg(args...) { alias dbgImpl!(args).print dbg; } template dbgImpl(args...) { import std.traits; void print(string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__) { static foreach (_; args) { static if(isBuiltinType!(typeof(_))) debug stderr.writefln("[%s:%s (%s)] %s = %s", file, line, fun, __traits(identifier, _), _); static if(isAggregateType!(typeof(_))) { debug stderr.writefln("[%s:%s (%s)] %s = %s", file, line, fun, __traits(identifier, _), toDbgString(_)); } } } string toDbgString(Arg)(Arg o) { string dbgstr = "("; import std.format; static foreach(f; FieldNameTuple!(typeof(o))) { static if(isBuiltinType!(typeof(__traits(getMember, o, f)))) { dbgstr ~= format("%s:%s, ", f, __traits(getMember, o, f)); } static if(isAggregateType!(typeof(__traits(getMember, o, f)))) { dbgstr ~= format("%s = %s, ", f, toDbgString(__traits(getMember, o, f))); } } return dbgstr[0..$-2] ~ ")"; } } struct Foo{ int s = 2; bool b = false; Bar bar;} struct Bar{ auto c = 'c';} class FooBar{ int t; Foo f; } void main() { int i; float f = 3.14; string s = "some string"; Foo foo; Bar bar; FooBar fb = new FooBar; dbg!(i, f, s, foo, 1+3, foo, bar, fb); // prints: // [dbg.d:54 (dbg.main)] i = 0 // [dbg.d:54 (dbg.main)] f = 3.14 // [dbg.d:54 (dbg.main)] s = some string // [dbg.d:54 (dbg.main)] foo = (s:2, b:false, bar = (c:c)) // [dbg.d:54 (dbg.main)] _ = 4 // [dbg.d:54 (dbg.main)] foo = (s:2, b:false, bar = (c:c)) // [dbg.d:54 (dbg.main)] bar = (c:c) // [dbg.d:54 (dbg.main)] fb = (t:0, f = (s:2, b:false, bar = (c:c))) } |
February 02, 2019 Re: Convenient debug printing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Schluter | On Saturday, 2 February 2019 at 12:08:02 UTC, Patrick Schluter wrote:
> On Friday, 1 February 2019 at 15:49:24 UTC, Per Nordlöw wrote:
>> Rust just added a standard macro `dbg` for debug printing
>>
>> https://blog.rust-lang.org/2019/01/17/Rust-1.32.0.html#the-dbg-macro
>>
>> [...]
>
> What's wrong with `debug log(<whatever>);` from `std.experimental.logger;`?
>
> Works like a charm in my project. The message is prepended with a time stamp and location in file/function/line.
This!
I think everyone here has forgotten about the debug keyword and compiler switch.
|
February 02, 2019 Re: Convenient debug printing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry | On Sat, 02 Feb 2019 07:24:45 +0000, Dmitry wrote:
> On Saturday, 2 February 2019 at 05:20:50 UTC, Neia Neutuladh wrote:
>> An old DIP <https://wiki.dlang.org/DIP50> for AST macros in D
> It looks interesting. Why didn't it go?
It looks incomplete. It doesn't even begin to explain what sort of object model the compiler should expose, and it has a block-to-string feature that it doesn't really talk about in any detail.
|
February 03, 2019 Re: Convenient debug printing | ||||
---|---|---|---|---|
| ||||
Posted in reply to ezneh | On Saturday, 2 February 2019 at 14:58:42 UTC, ezneh wrote:
> import std.stdio;
>
> template dbg(args...)
> {
> alias dbgImpl!(args).print dbg;
> ...
Great! Close enough for me!
Thanks!
|
February 04, 2019 Re: Convenient debug printing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | On 2019-02-02 18:23, Neia Neutuladh wrote: > It looks incomplete. It doesn't even begin to explain what sort of object > model the compiler should expose, and it has a block-to-string feature > that it doesn't really talk about in any detail. It's far from complete. As I mentioned elsewhere, the DIPs on the wiki are from a different time with not as high standards. It was also never formally reviewed, it was more that I wrote down what I though AST macros should be like to avoiding having to repeat myself on the forums. -- /Jacob Carlborg |
November 17, 2019 Re: Convenient debug printing | ||||
---|---|---|---|---|
| ||||
Posted in reply to ezneh | On 2019-02-02 14:58:42 +0000, ezneh said: > On Friday, 1 February 2019 at 15:49:24 UTC, Per Nordlöw wrote: >> [...] > > Alright, after quite some work I finally managed to do the following code printing also nested variable names from classes & structs. I am not sure it's quite the same as the Rust AST macro for that, but I guess it's still interesting: > ... This looks great, but I get: Error: value of this is not known at compile time for the static foreach() line in dbgImpl. So, wondering what I'm doing wrong... -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
Copyright © 1999-2021 by the D Language Foundation