Stop Xcode from truncating NSLogs

Whenever I NSLog multiple times in a short time, Xcode randomly truncates some of them like:

2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-28 22:00:00 +0000, day: 29, week: 2015-53
2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-29 22:00:00 +0002015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-30 22:00:00 +0000, day: 31, week: 2015-53
2015-11-25 15:53:47.208 AppName[97163:38002062] date: 2015-12-31 22:00:00 +0000, day: 1, week: 2015-53

when it should look like this:

2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-28 22:00:00 +0000, day: 29, week: 2015-53
2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-29 22:00:00 +0000, day: 30, week: 2015-53
2015-11-25 15:53:47.207 AppName[97163:38002062] date: 2015-12-30 22:00:00 +0000, day: 31, week: 2015-53
2015-11-25 15:53:47.208 AppName[97163:38002062] date: 2015-12-31 22:00:00 +0000, day: 1, week: 2015-53

Some other times, the logs are coming in the wrong order too.

Why is this happening and how can I stop it?

Code snippet (requiring DateTools):

for (NSInteger year = 2015; year <= 2016; year++) {
    for (NSInteger month = (year == 2015 ? 11 : 1); month <= (year == 2015 ? 12 : 2); month++) {
        for (NSInteger day = 1; day <= (month == 11 ? 30 : 31); day++) {
            NSDate *date = [NSDate dateWithYear:year month:month day:day];

            NSLog(@"INFO: date: %@, day: %d, week: %d-%d", date, (int)day, (int)[date yearForWeekOfYearWithCalendar:calendar], (int)[date weekOfYearWithCalendar:calendar]);
        }
    }
}

Answers


The fact that they're in the wrong order sometimes is a clue--it looks like you're calling NSLog on a thread, and not only are the threads executing in an order that you don't expect, but clearly they can sometimes stomp on each other. You can wrap your calls to NSLog inside calls to dispatch_sync(dispatch_get_main_queue(), ^{ NSLog(...) }) to prevent them from stomping on each other. If you want them to execute in a specific order, though, then the callers have to be executed synchronously, not asynchronously.


Need Your Help

Create more than two StatusBar parts

c winapi

I am unable to create more than two StatusBar parts:

UnicodeDecodeError, ascii processing for Snowball stemming algorithm in python

python regex encoding byte-order-mark python-unicode

I am having some trouble getting general files to read into a program that I have made. The problem I am currently having is that pdfs are based in some kind of mutated utf-8 including a BOM that t...