Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the prefix in NSLog mean?

When I use NSLog, I get output similar to the following:

2012-01-24 17:05:32:860 App[21856:71939] {logging goes here}

I recognize that '2012-01-24 17:05:32:860' is the date, 'App' is the app name, but I have no clue what '[21856:71939]' means. Can someone fill me in on what that is and where it's generated at?

All I'm trying to do is get logging that lines up nicely so it's easy to read, but the '[21856:71939]' varies in digits enough to mess up any alignment attempts. If I knew how the numbers in '[21856:71939]' were generated, I could add spaces as needed to make it line up correctly, but that's my only idea at this point.

Any help would be much appreciated :)

like image 487
Sean Freitag Avatar asked Jan 24 '12 23:01

Sean Freitag


2 Answers

21856 is the process id. 71939 is the thread id.

You can generate this portion of the log on your own using:

[NSString stringWithFormat:@"[%ld,%lx]",
    (long) getpid(),
    (long) pthread_mach_thread_np(pthread_self())];

Edit 2014-09-23:

At least on the simulator in iOS 8, the second number is now the pthread_threadid_np of the thread.

    __uint64_t threadId;
    if (pthread_threadid_np(0, &threadId)) {
        threadId = pthread_mach_thread_np(pthread_self());
    }

    [NSString stringWithFormat:@"[%ld,%llu]", (long) getpid(), threadId]
like image 110
iccir Avatar answered Oct 26 '22 23:10

iccir


IIRC, the 21856 is the process PID, and the 71939 is some sort of thread identifier.

like image 43
Lily Ballard Avatar answered Oct 27 '22 00:10

Lily Ballard