views:

328

answers:

4

When I use

NSLog(@"fooBar")

it prints out a lot of stuff I don't want:

2009-09-03 13:46:34.531 MyApp[3703:20b] fooBar

Is there a way to print something to the console without this big prefix? I want to draw a table and some other things in the console so that space is crucial...

A: 

NSLog just prints to strerr. Use fprintf instead.

fprintf(stderr, "foobar");
FigBug
-1 This answer is incomplete because printf and fprintf don't handle Objective-C strings w/o conversion. The moment an NSString gets passed in, the program will crash. Plus, other answers make it possible to change the output location in one place.
Quinn Taylor
`NSLog()` doesn't just print to `stderr`. It also logs to the system log at `ASL_LEVEL_WARNING` with a facility of `com.apple.console`. You can do the same using the functions discussed in the `asl(`3`)` manpage.That said, if you're drawing an ASCII table, you probably really *don't* want to be logging to the system log. Just write to `stdout`/`stderr` as appropriate.
Jeremy W. Sherman
Bogus answer; fprintf can't deal w/objects, as previously stated.
bbum
+9  A: 

This is from Mark Dalrymple at borkware.com

http://borkware.com/quickies/single?id=261

A Quieter NSLog (General->Hacks) [permalink]

// NSLog() writes out entirely too much stuff.  Most of the time I'm
// not interested in the program name, process ID, and current time
// down to the subsecond level.
// This takes an NSString with printf-style format, and outputs it.
// regular old printf can't be used instead because it doesn't
// support the '%@' format option.

void QuietLog (NSString *format, ...)
{
  va_list argList;
  va_start (argList, format);
  NSString *message = [[[NSString alloc] initWithFormat: format
                                              arguments: argList] autorelease];
  printf ("%s", [message UTF8String]);
  va_end  (argList);

} // QuietLog
jrbj
Please correct me if I get that wrong, but: Can I really write ...) at the end of the function definition to indicate "how many more args as you wish"???
HelloMoon
You can. It's valid C.Go ahead and try it.
jrbj
This is probably the most elegant solution, with fprintf being the quickest. Note that if you use fprintf, you have to make sure that everything that goes into it is a C string, but QuietLog will take the exact same arguments as NSLog.
Cinder6
That'll work... but printf("%s", ...) is inefficient. And you'll probably want to avoid autorelease if you call this a bunch (release message at the end). See:http://stackoverflow.com/questions/1354728/in-xcode-is-there-a-way-to-disable-the-timestamps-that-appear-in-the-debugger-co/1354736#1354736
bbum
And if you want to make this super-elegant, add a compiler flag to this method that lets you turn off printing with a switch, so you don't fill your release app up with a bunch of garbage console messages.
kubi
The printf("%s",...) is to avoid the cases where the string may have a % in it, which would be invalid in printf's arguments. For example, if I have an NSString("50%off") then you get an error because the %o is expecting an argument. Note that the same argument is also used for NSLog("@%", s) instead of NSLog(s) for much the same reason.
AlBlue
+3  A: 

This is a variation on the borkware quickies: http://cocoaheads.byu.edu/wiki/a-different-nslog . It prints the file and line number of where the log takes place. I use it all the time.

Dave DeLong
Also, since the asker is trying to "draw a table" with text output, note that each call to QuietLog() ends with a newline, just like NSLog() does. If you need to print a line of text with multiple statements, I suggest using `printf()` and `-[NSString UTF8String]`.
Quinn Taylor