digitalmars.D - debug()
- Orion- (21/21) Feb 26 2008 Hello people,
- Jason House (11/16) Feb 26 2008 I can't answer that part authoritatively, but...
- Orion- (3/24) Feb 27 2008 Thank Jason, could you point me to the/a debug(n) documentation?
- BCS (2/7) Feb 27 2008 http://www.digitalmars.com/d/1.0/version.html#debug
- BCS (6/8) Feb 26 2008 some runtime reflection that looks at the return pointer of the stack fr...
- Bill Baxter (33/58) Feb 26 2008 There is a way to get the __LINE__ thing to work automatically, without
- Jarrett Billingsley (3/6) Feb 26 2008 It does seem to. Whew! I was going to have to join you if it didn't.
- Orion- (3/69) Feb 27 2008 Thanks Bill, do you know of any document explaining the debug() D functi...
- Henning Hasemann (7/9) Feb 27 2008 Well, I'm not Bill, but I guess that won't hinder you looking here:
Hello people, I'm just starting D programming. I'd like to make a printf like function to print some debuging informations. The synopsis could be dprintf(char[] format, ...) and as an example: dprintf("creating object at %a", self); It would print something like: debug: class1.d(13): creating object at 0XFFB8 where class1.d is the __FILE__ name and 13 is the __LINE__ where it was called. So, here are my 2 questions: * How do you know 'self' inside a class member function in D? * How would you do to have __LINE__ being the line where dprintf was called not the line where it is implemented ? Here is my dprintf function so far: void dprintf(...) { TypeInfo[] infos; void *data; void putc(dchar c) { fputc(c, stderr); } Box[] box1 = boxArray("debug: "); Box[] box2 = boxArray(_arguments, cast(void *)_argptr); boxArrayToArguments(box1 ~ box2, infos, data); std.format.doFormat(&putc, infos, cast(char *)data); } Regards, Thierry
Feb 26 2008
Orion- wrote:So, here are my 2 questions: * How do you know 'self' inside a class member function in D?Use 'this'* How would you do to have __LINE__ being the line where dprintf was called not the line where it is implemented ?I can't answer that part authoritatively, but... Are you doing dprintf(...) instead of debug printf(...) ? If so, I recommend trying out the d style. Using debug(n), where n is an int, can be very useful for looking at differing levels of output. I personally have mostly moved away from that and started using debug(ident) (possibly with debug=ident at the top of the file or adding -debug=ident to the dmd call). The nice thing with that is it becomes extremely easy to leave all your debug stuff in your source. When chasing down a bug later on, it can be helpful.
Feb 26 2008
Jason House Wrote:Orion- wrote:Thank Jason, could you point me to the/a debug(n) documentation? Regards, ThierrySo, here are my 2 questions: * How do you know 'self' inside a class member function in D?Use 'this'* How would you do to have __LINE__ being the line where dprintf was called not the line where it is implemented ?I can't answer that part authoritatively, but... Are you doing dprintf(...) instead of debug printf(...) ? If so, I recommend trying out the d style. Using debug(n), where n is an int, can be very useful for looking at differing levels of output. I personally have mostly moved away from that and started using debug(ident) (possibly with debug=ident at the top of the file or adding -debug=ident to the dmd call). The nice thing with that is it becomes extremely easy to leave all your debug stuff in your source. When chasing down a bug later on, it can be helpful.
Feb 27 2008
Orion- wrote:Jason House Wrote: Thank Jason, could you point me to the/a debug(n) documentation? Regards, Thierryhttp://www.digitalmars.com/d/1.0/version.html#debug
Feb 27 2008
Reply to Orion-,* How would you do to have __LINE__ being the line where dprintf was called not the line where it is implemented ?some runtime reflection that looks at the return pointer of the stack frame would do it, but Ouch. The best solution I know of passes __FILE__ and __LINE__ as args but to make that nice you then need to run the whole file through a preprocessor of some fort to auto insert them. In short, you can't do it without a lot of work.
Feb 26 2008
Orion- wrote:Hello people, I'm just starting D programming. I'd like to make a printf like function to print some debuging informations. The synopsis could be dprintf(char[] format, ...) and as an example: dprintf("creating object at %a", self); It would print something like: debug: class1.d(13): creating object at 0XFFB8 where class1.d is the __FILE__ name and 13 is the __LINE__ where it was called. So, here are my 2 questions: * How do you know 'self' inside a class member function in D? * How would you do to have __LINE__ being the line where dprintf was called not the line where it is implemented ? Here is my dprintf function so far: void dprintf(...) { TypeInfo[] infos; void *data; void putc(dchar c) { fputc(c, stderr); } Box[] box1 = boxArray("debug: "); Box[] box2 = boxArray(_arguments, cast(void *)_argptr); boxArrayToArguments(box1 ~ box2, infos, data); std.format.doFormat(&putc, infos, cast(char *)data); }There is a way to get the __LINE__ thing to work automatically, without typing __LINE__, but the cure is almost worse than the disease. For simplicity I'll just use a non-formatted string to demonstrate. string dprintf(string msg) { return `writefln("%s(%s): ", __FILE__,__LINE__, msg);`; } ... mixin( dprintf("Hey you!")); At least I think that works. I don't use it because I'm not about to go around typing mixin everywhere to do debug output. That's the kind of thing that should some day be doable with macros, though. Then you'll be able to call it without the "mixin" part. What I actually use is more like what Jason said. debug(ver) {}. I don't bother with __FILE__ and __LINE__ stuff. ----- module MyModule; import std.stdio; private{ debug(MyModule) { alias writefln debugfln; alias writef debugf; } else { void debugfln(...) {} void debugf(...) {} } } And I hope that the compiler is smart enough to inline an empty function. If it's not, I'm getting on the next plane to Seattle to whack Walter 50 times with a wet noodle. --bb
Feb 26 2008
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:fq2ccd$kbk$1 digitalmars.com...And I hope that the compiler is smart enough to inline an empty function. If it's not, I'm getting on the next plane to Seattle to whack Walter 50 times with a wet noodle.It does seem to. Whew! I was going to have to join you if it didn't.
Feb 26 2008
Bill Baxter Wrote:Orion- wrote:Thanks Bill, do you know of any document explaining the debug() D function ? Regards, ThierryHello people, I'm just starting D programming. I'd like to make a printf like function to print some debuging informations. The synopsis could be dprintf(char[] format, ...) and as an example: dprintf("creating object at %a", self); It would print something like: debug: class1.d(13): creating object at 0XFFB8 where class1.d is the __FILE__ name and 13 is the __LINE__ where it was called. So, here are my 2 questions: * How do you know 'self' inside a class member function in D? * How would you do to have __LINE__ being the line where dprintf was called not the line where it is implemented ? Here is my dprintf function so far: void dprintf(...) { TypeInfo[] infos; void *data; void putc(dchar c) { fputc(c, stderr); } Box[] box1 = boxArray("debug: "); Box[] box2 = boxArray(_arguments, cast(void *)_argptr); boxArrayToArguments(box1 ~ box2, infos, data); std.format.doFormat(&putc, infos, cast(char *)data); }There is a way to get the __LINE__ thing to work automatically, without typing __LINE__, but the cure is almost worse than the disease. For simplicity I'll just use a non-formatted string to demonstrate. string dprintf(string msg) { return `writefln("%s(%s): ", __FILE__,__LINE__, msg);`; } ... mixin( dprintf("Hey you!")); At least I think that works. I don't use it because I'm not about to go around typing mixin everywhere to do debug output. That's the kind of thing that should some day be doable with macros, though. Then you'll be able to call it without the "mixin" part. What I actually use is more like what Jason said. debug(ver) {}. I don't bother with __FILE__ and __LINE__ stuff. ----- module MyModule; import std.stdio; private{ debug(MyModule) { alias writefln debugfln; alias writef debugf; } else { void debugfln(...) {} void debugf(...) {} } } And I hope that the compiler is smart enough to inline an empty function. If it's not, I'm getting on the next plane to Seattle to whack Walter 50 times with a wet noodle. --bb
Feb 27 2008
Orion- <thierry.passeron gmail.com> wrote:Thanks Bill, do you know of any document explaining the debug() D function ?Well, I'm not Bill, but I guess that won't hinder you looking here: http://www.digitalmars.com/d/1.0/version.html Henning -- GPG Public Key: http://gpg-keyserver.de/pks/lookup?op=get&search=0xDDD6D36D41911851
Feb 27 2008