www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - faking a C logging macro

reply Tydr Schnubbis <fake address.dude> writes:
Can I get something like this in D, using mixins or whatever?

#define log(s) logprintf(__FILE__ ": " __FUNCTION__ ": %s", s)
Apr 20 2006
parent reply BCS <BCS_member pathlink.com> writes:
Tydr Schnubbis wrote:
 Can I get something like this in D, using mixins or whatever?
 
 #define log(s) logprintf(__FILE__ ": " __FUNCTION__ ": %s", s)
sed "/log\(/logprintf(__FILE__ `: ` __FUNCTION__ `: %s`, /" in.d -o out.d ??? No but seriously folks, I have wanted something like this a few times myself. Particularly, some way to get the __FILE__, etc. set of constants of the line where a template is instanced. Something like the following would be great. template log( char[] s, char[] fi = __FILE__, int li = __LINE__, char[] fn = __FUNCTION__) // const defaults come from called location { const char[] log = ... } main.d int i; void main() { pragma(msg, log!("this is broken!!!"); } compile time output: main.d:4 this is broken!!!
Apr 20 2006
parent clayasaurus <clayasaurus gmail.com> writes:
According to the D change log   version .116 this was included.
"Added __FILE__, __LINE__, __DATE__, __TIME__, __TIMESTAMP__."

Maybe ask Walter to add __FUNCTION__ ?

You might want to check out build's preprocessor stuff as well. 
http://www.dsource.org/forums/viewtopic.php?t=624


BCS wrote:
 Tydr Schnubbis wrote:
 Can I get something like this in D, using mixins or whatever?

 #define log(s) logprintf(__FILE__ ": " __FUNCTION__ ": %s", s)
sed "/log\(/logprintf(__FILE__ `: ` __FUNCTION__ `: %s`, /" in.d -o out.d ??? No but seriously folks, I have wanted something like this a few times myself. Particularly, some way to get the __FILE__, etc. set of constants of the line where a template is instanced. Something like the following would be great. template log( char[] s, char[] fi = __FILE__, int li = __LINE__, char[] fn = __FUNCTION__) // const defaults come from called location { const char[] log = ... } main.d int i; void main() { pragma(msg, log!("this is broken!!!"); } compile time output: main.d:4 this is broken!!!
Apr 20 2006