ASL provides two ways to write to the log.
The first, and most recognizable, way is the asl_log pair of functions:
int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...); int asl_vlog(aslclient asl, aslmsg msg, int level, const char *format, va_list ap);
The last two arguments (
ap) are the same as the arguments to
vsyslog. They’re also similar to
NSLogv, except that ASL does not support
%@ in its format strings.
The first two arguments to
asl_log are optional. If you pass
NULL as the client, it uses a default connection with default settings. If you pass
NULL for the message, all of the message’s properties other than the level and content will have their default values. Normally, you will pass
NULL for the first two arguments.
The argument in the middle,
level, specifies the importance of the message. It works exactly the same way as syslog(3)’s
priority argument; in fact, they use the same values, and those values have the same names.
If you don’t feel like changing over all your
NSLog calls to use
asl_log because you’ll have to change your format strings to use
-UTF8String instead of
%@, don’t worry. Here’s a one-line macro you can stash in a header file to give you an
#define asl_NSLog(client, msg, level, format, ...) asl_log(client, msg, level, "%s", [[NSString stringWithFormat:format, ##__VA_ARGS__] UTF8String])
This macro gives you all the flexibility of
asl_log and all the format specifiers of
The other way to write to the log is to construct a message object yourself and send the whole thing.
ASL defines a type called aslmsg, which is little more than a key-value mapping. All keys and values are UTF-8 C strings. ASL provides and uses several stock keys for the normal properties of a message:
- The priority level of the message, expressed as the decimal representation of an integer (
ASL_LEVEL_*). When setting this property, you can use the level-name strings (
ASL_STRING_*) here; they are names, not numbers, but they will be translated to the correct values for you.