snaggs
snaggs

Reputation: 5713

NSlog does not print new lines \r\n

I try to add new lines to NSlog.

I run:

NSLog(@"\n%@\r\n\r\n", json1);
NSLog(@"\n%@\r\n\r\n", json2);

However The output is:

2017-03-29 16:09:50 InAppTestAfApp2[312:33527]  
{"attribution":"true"}
2017-03-29 16:09:50 InAppTestAfApp2[312:33527]  
{"attribution":"false"}

It ignores new line as \r\n (tried also \n only) but 1st \n before string works

I expect:

2017-03-29 16:09:50 InAppTestAfApp2[312:33527]  
{"attribution":"true"}


2017-03-29 16:09:50 InAppTestAfApp2[312:33527]  
{"attribution":"false"}


017-03-29 16:09:50 InAppTestAfApp2[312:33527] other logs ...

I use Xcode 8.3

[Edit 1]

I tried to run:

NSLog(@"%@ %@",type, @"xxxxx\n\n");

Still I see:

2017-03-29 16:09:50 InAppTestAfApp2[312:33527]  
xxxxx
017-03-29 16:09:50 InAppTestAfApp2[312:33527] other logs ...

[EDIT 2]

As work around I add garbage at the end but it looks messy:

NSLog(@"%@ %@\r\n\r\n%s",type, json, @"`");

Upvotes: 4

Views: 2185

Answers (1)

Caleb
Caleb

Reputation: 125007

Carriage return/linefeed pairs (CR+LF) have never been needed on the Mac. Classic MacOS used CR, and MacOS X uses LF (due to its Unix heritage). So it's not surprising that NSLog() strips the CR characters out of the text that you're logging.

It's also not surprising that NSLog() trims the white space from the end of your log statements. NSLog() is meant for logging messages -- it's not a general-purpose print statement. Keeping logs compact is useful, and trimming blank lines from the end of a message serves that purpose.

If you really want to include blank space in your messages, perhaps to make certain log messages stand out more, you can do it by including a printing character after the blank lines. For example:

    NSLog(@"\n\nMy Really Important Message\n\n.");

The result:

2017-03-29 09:28:12.898 MessageTest[13407:2524935] 

My Really Important Message

.

Upvotes: 6

Related Questions