Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [C#] User defined debug functions

Credits go to X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
[C#] User defined debug functions
ixilom
Member #7,167
April 2006
avatar

To my dismay C# doesn't support macros (Yeah, I didn't notice that until now).
But, is there a way to write functions that are "stripped" away when you do a release build?
Right now I have to do:

#if DEGUG
Tools.Logger.WriteLine("something very interesting.");
#endif

And the entire Logger class is surrounded with #if #endif.

I know typing #if DEBUG and #endif is just a few keystrokes, but it does clutter the source quite a bit if you have a whole bunch of them :-/

Essentially, what I'd like to do is something like the System.Diagnostics.Debug class ... is that even possible ???

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

X-G
Member #856
December 2000
avatar

Can't you just make the interior of the WriteLine function conditional? Surely the compiler will optimize away the empty function body and call to nothing.

EDIT: Also, there is apparently a function attribute called Conditional you can use: [Conditional("DEBUG")]

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

ixilom
Member #7,167
April 2006
avatar

Ah, cheers m8!

My Logger.cs now looks something like:

#SelectExpand
1#if DEBUG 2using System.Diagnostics; 3 4namespace xxx.Tools 5{ 6 7 public static class Logger 8 { 9 [Conditional("DEBUG")] 10 public static void WriteLine(string line) 11 { 12// stuff 13 } 14 } 15 16} 17#endif

And I can just use:

Tools.Logger.WriteLine("Thank you X-G :D");

Thats exactly what what I wanted 8-)
Cookie for you.

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

X-G
Member #856
December 2000
avatar

Not bad for someone who doesn't know C#.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

ixilom
Member #7,167
April 2006
avatar

I bet your google/msdn-fu is better than mine ;)
I blame the lack of sleep :-X

[Edit]
I actually had to remove the #if DEBUG and #endif from Logger.cs.
The compiler would choke on not finding my Logger class when building a release build for some reason.

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

james_lohr
Member #1,947
February 2002

If you're coming from a C/C++ background, then in terms of language facilities, you can expect C# to have it, and, if not, for a cleaner alternative to exist.

Neil Walker
Member #210
April 2000
avatar

I suspect XG got his information from here, which explains everything really:

http://msdn.microsoft.com/en-us/library/4xssyw96.aspx

and you should think yourself lucky you aren't coding in archaic Java, there is no concept of attributes, conditional compilation, etc.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

ixilom
Member #7,167
April 2006
avatar

Yup, I'm coming from C/C++ background and I have found C# much cleaner, no doubt there.

Java? Wouldn't touch it with a long stick to be honest.

Now, that I've had sleep and read once again on conditional attribute, I still have a question :P
Not that it really matters, but I'm still curious about whether the compiler would be smart enough to "strip" my Logger class all together as its only function (WriteLine) has been removed in a release build?
If it doesn't, I'd have a empty static class that does nothing but waste a few bytes of memory ???;D

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

Kibiz0r
Member #6,203
September 2005
avatar

ixilom said:

I'm still curious about whether the compiler would be smart enough to "strip" my Logger class all together

Maybe if it's internal instead of public. Executables are still assemblies, so they can be referenced by other assemblies. Obviously, it's not going to delete a type that you might want to access from another assembly.

At any rate, I wouldn't worry about it. I don't think it matters.

Neil Walker
Member #210
April 2000
avatar

ildasm is your friend. I'd do it for you, but I can't be bothered. Just check the code generated after a compilation, it's fairly easy to read.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

ixilom
Member #7,167
April 2006
avatar

Kibiz0r said:

Maybe if it's internal instead of public. Executables are still assemblies, so they can be referenced by other assemblies. Obviously, it's not going to delete a type that you might want to access from another assembly.

Ah yes, would be bad to remove Logger in case some other assembly tries to access it.
Actually I don't think WriteLine is removed either because of the same reason, but rather just the calls to it.

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

Go to: