2

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...

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
  • 1
    possible duplicate of [In XCode, is there a way to disable the timestamps that appear in the debugger console?](http://stackoverflow.com/questions/1354728/in-xcode-is-there-a-way-to-disable-the-timestamps-that-appear-in-the-debugger-co) – bbum Aug 15 '10 at 15:07

3 Answers3

9

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
  • 731
  • 4
  • 5
  • 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"??? –  Sep 03 '09 at 18:10
  • You can. It's valid C. Go ahead and try it. – jrbj Sep 03 '09 at 18:12
  • 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 Sep 03 '09 at 18:26
  • 2
    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 Sep 03 '09 at 19:52
  • 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 Sep 03 '09 at 21:03
  • 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 Sep 03 '09 at 21:43
2

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
  • 242,470
  • 58
  • 448
  • 498
  • 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 Sep 03 '09 at 18:41
-2

NSLog just prints to strerr. Use fprintf instead.

fprintf(stderr, "foobar");
Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
  • -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 Sep 03 '09 at 18:38
  • 5
    `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 Sep 03 '09 at 19:45
  • Bogus answer; fprintf can't deal w/objects, as previously stated. – bbum Sep 03 '09 at 19:49