www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - debug()

reply Orion- <thierry.passeron gmail.com> writes:
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
next sibling parent reply Jason House <jason.james.house gmail.com> writes:
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
parent reply Orion- <thierry.passeron gmail.com> writes:
Jason House Wrote:

 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.
Thank Jason, could you point me to the/a debug(n) documentation? Regards, Thierry
Feb 27 2008
parent BCS <BCS pathlink.com> writes:
Orion- wrote:
 Jason House Wrote:
 
 Thank Jason, could you point me to the/a debug(n) documentation?
 
 Regards, Thierry
http://www.digitalmars.com/d/1.0/version.html#debug
Feb 27 2008
prev sibling next sibling parent BCS <ao pathlink.com> writes:
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
prev sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
prev sibling parent reply Orion- <thierry.passeron gmail.com> writes:
Bill Baxter Wrote:

 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
Thanks Bill, do you know of any document explaining the debug() D function ? Regards, Thierry
Feb 27 2008
parent Henning Hasemann <hhasemann web.de> writes:
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