19

After upgrading to Xcode 8 GM today i noticed that NSLog isn't printing the whole log-message to the console. This is especially noticeable when working against an API that downloads a lot of information, like a REST API download all the products from a database, it only shows the first 30 keys on the first product, the rest of the information is clipped...

I'm printing arrays and dictionaries, if that makes any difference.

NSDictionary *allProducts = responseFromAPI;
NSLog(@"All products:%@", allProducts);

Have anyone else noticed this? And does anybody know how to fix this?

Pointblaster
  • 504
  • 3
  • 18
  • I am having the same issue and I can't figure it out! I'm not printing dicts, this is mine - used to print entire string, now only prints out 1028 chars every time: NSLog(@"Data:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); – lomokat Sep 09 '16 at 19:32
  • Did this also start after upgrade to xCode 8? – Pointblaster Sep 12 '16 at 09:43
  • yup only started after xcode 8 GM. I didn't notice it in the last beta. – lomokat Sep 12 '16 at 20:38
  • @lomokat & Pointblaster It may help you [http://stackoverflow.com/questions/39538320/ios10-nslog-is-limited-to-1024-chars-strings/39538500#39538500](http://stackoverflow.com/questions/39538320/ios10-nslog-is-limited-to-1024-chars-strings/39538500#39538500) – Ketan Parmar Sep 16 '16 at 19:21
  • As the other question ^ offered, you can fake NSLog out with printf but that doesn’t help much in Swift (okay, one could write a bunch of code to bridge between swift and obj-c… but I’d rather a different solution) – AutomatonTec Sep 22 '16 at 19:25
  • Me too having the same issue, But I don't have a clue about how to solve this issue. I'm researching and will let u guys know on any updates. –  Sep 23 '16 at 12:30
  • Same here after upgrading Xcode. – Faisal Tanveer Sep 30 '16 at 06:43

2 Answers2

13

As @Lion described in his comment the easiest possible way is to use printf instead. It does not work exactly like NSLog but it shows what you want.

NSDictionary *allProducts = responseFromAPI;
NSString * string = [NSString stringWithFormat: @"%@", allProducts];
printf("%s", [string UTF8String]);

or shorter:

NSDictionary *allProducts = responseFromAPI;
printf("%s", [NSString stringWithFormat: @"%@", allProducts].UTF8String);

A tip is to place a "\n" at the beginning or end of the printf format so it will separate the outputs and not put all in a single line. Something like this:

printf("%s\n", string.UTF8String);

If you don't like writing printf instead every time you can use a #define to redirect the code to an printf like this (code from @xfdai):

#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

Hopefully this is just a bug from Apple that will get fixed soon, until then we can use this.

Pointblaster
  • 504
  • 3
  • 18
0

You can use this method. Split every 800 chars. Or can be set. NSLOG I think truncate every 1000 chars. If string is less than 800 will use a simple NSLog. This is useful for Json long strings and uses the console. printf uses Xcode debug window not the console.

-(void) JSLog:(NSString*)logString{

        int stepLog = 800;
        NSInteger strLen = [@([logString length]) integerValue];
        NSInteger countInt = strLen / stepLog;

        if (strLen > stepLog) {
        for (int i=1; i <= countInt; i++) {
            NSString *character = [logString substringWithRange:NSMakeRange((i*stepLog)-stepLog, stepLog)];
            NSLog(@"%@", character);

        }
        NSString *character = [logString substringWithRange:NSMakeRange((countInt*stepLog), strLen-(countInt*stepLog))];
        NSLog(@"%@", character);
        } else {

        NSLog(@"%@", logString);
        }

}
user2807083
  • 2,962
  • 4
  • 29
  • 37
Andy Vene
  • 41
  • 2