基本上这种事情一定可以在Apple文档中找到,看 NSLog 的文档,第一句话就说: Logs an error message to the Apple System Log facility. ,所以首先, NSLog 就不是设计作为普通的debug log的,而是error log;其次, NSLog 也并非是 printf 的简单封装,而是 Apple System Log (后面简称ASL)的封装。
ASL
ASL是个啥?从 官方手册 上,或者从终端执行 man 3 asl 都可以看到说明:
These routines provide an interface to the Apple System Log facility. They are intended to be a replacement for the syslog(3) API, which will continue to be supported for backwards compatibility.
You can use two interfaces in OS X to log messages: ASL and Syslog. You can also use a number of higher-level approaches such as NSLog. However, because most daemons are not linked against Foundation or the Application Kit, the low-level APIs are often more appropriate
It writes log messages to the Apple System Logging (asl) facility. This allows log messages to show up in Console.app.
It also checks to see if the application's stderr stream is going to a terminal (such as when the application is being run via Xcode). If so it writes the log message to stderr (so that it shows up in the Xcode console).
To send a log message to the ASL facility, you basically open a client connection to the ASL daemon and send the message. BUT - each thread must use a separate client connection. So, to be thread safe, every time NSLog is called it opens a new asl client connection, sends the message, and then closes the connection.