ASL: Logging to a file
As I mentioned in the previous post, every ASL client connection (including the default connection) has a set of file descriptors to which ASL will mirror any message you send through it. The set is initially empty, unless you use ASL_OPT_STDERR when creating the connection, in which case ASL will add STDERR_FILENO to it for you.
You can add files to the set with asl_add_log_file, and you can remove them from it with asl_remove_log_file:
int asl_add_log_file(aslclient asl, int fd); int asl_remove_log_file(aslclient asl, int fd);
As usual, pass NULL to these functions to work with the default connection.
The format of the output changed in Leopard. In Tiger, it's the “raw” format:
220 [Time 1197742037] [Host] [Sender ReadFromASLOnDemand] [PID 848] [UID 501] [GID 501] [Level 4] [Message Content object's class (AppDelegate) isn't the class that the object controller is expecting (NSMutableDictionary)]
That number at the start, which is space-padded to ten characters, is the length of the message, including both the space after the length and the newline at the end.
In Leopard, it's the “std” format:
Fri Dec 7 20:31:55 Foldable-Mac.local ReadFromASLOnDemand[4188] <Warning>: Content object's class (AppDelegate) isn't the class that the object controller is expecting (NSMutableDictionary)
As you can see, the “std” format is simply the NSLog format with the addition of the name of the log level.
Speaking of log levels, every file receives all messages sent to the connection, regardless of the connection's filter mask.
In either format, ASL encodes your message using strvis. strvis is a function for escaping various characters; ASL uses it to escape newlines, tabs, and any non-ASCII characters. That's right: Any non-ASCII characters that you send to ASL will show up in stderr as escape sequences (but they will be logged correctly). You can see why this escaping is annoying; worse, there's no way to turn it off. I've filed this in RadarWeb, to request a way to turn this “feature” off.
I recommend you always add stderr to the default connection, preferably as early as possible (in main). This way, every time you do something like this, it will show up in the Xcode run log:
asl_log(/*client*/ NULL, ASL_LEVEL_DEBUG, "Number of active frobnitzers: %u\n", [activeFrobnitzers count]);
Another way would be to open your own client connection with the ASL_OPT_STDERR option (as described above), and use that for all your logging.
Finally, in case you're wondering, removing a log file from a connection does not close the file. The documentation and header make a rather big point of this.
Next in the ASL series: Searching the log database
