views:

161

answers:

3

I try to make a useful macro for logging. But I find that NSLog and all other sort of macros that carry textual information with them simply distract a lot from the code.

Is there any way to "hack" Xcode in a way that it will interpret something like

/*** do this because of that ***/

as a macro call, which results in calling NSLog, for example? I want those logs to "feel" like comments. Anything else that looks like critical code is just distracting and reduces productivity at the end, and then there's no real benefit from logging what's going on.

+1  A: 

Not that I know of (though I may be wrong!)

I think that if you want it to look different, a macro is probably the best that you can hope for - at least it will be highlighted a different color :)

deanWombourne
but still the texts are in angry red. I dropped everything out of my code although it helped _a lot_ in debugging. when starting to develop again it just distracts so much that it doesnt feel worth it. There are as many log lines as code lines... like 5000 of them, or so.
Another Registered User
Maybe you need to ask yourself why you're logging so much? A log for each line seems really excessive.
Jasarien
+2  A: 

You can enclose one or more lines of NSLog in curly braces and then use Xcode's folding option to hide them.

Example:

{
    NSLog(@"<#label#>");
    NSLog(@"<#label#>");
}

when folded look like:

{...}

The braces will also indent the statements when unfolded making them more visually distinct.

I think you should reconsider your use of log statements. If you have as many logs statements as lines of code something is wrong. You should be using the debugger to print most values and messages. If you have that many log statements you reach a point where mistakes in the log statements produce more bugs than the code. You also have a big problem cleaning up the code for release.

TechZen
+7  A: 

Is it possible to define a macro that looks almost like a comment?

Why do you want to make your code less readable?

The answer is no, and that's a good thing.

Is there any way to "hack" Xcode in a way that it will interpret something like

/*** do this because of that ***/

as a macro call…

Probably, but that's useless. All that would do is make the syntax coloring incorrect (coloring this comment as if it were a function call). The compiler (either GCC or Clang) would still see this as the comment that it is.

Making the compiler think it's a function call or macro invocation is what would actually achieve log output at run time, but even if you could do that, it's still a bad idea because you would have a function call or macro invocation disguised as a comment.

If you want your program to log messages, write logging code:

NSLog(@"Do %@ because of %@.", foo, bar);

This code is explicitly code that does something at runtime. No mystery. No surprises. That's why it's better.

Peter Hosey