1 .\" Copyright (c) 2005 Apple Computer
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of Apple Computer nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 .Nm asl_add_log_file ,
46 .Nm asl_remove_log_file ,
47 .Nm asl_set_cutoff_level ,
49 .Nm aslresponse_next ,
51 .Nd system log message sending and searching functions
55 .Fn asl_open "const char *ident, const char *facility, uint32_t opts"
57 .Fn asl_close "aslclient asl"
59 .Fn asl_new "uint32_t type"
61 .Fn asl_free "aslmsg msg"
63 .Fn asl_set "aslmsg msg, const char *key, const char *value"
65 .Fn asl_set_query "aslmsg msg, const char *key, const char *value, uint32_t op"
67 .Fn asl_key "aslmsg msg, uint32_t n"
69 .Fn asl_get "aslmsg msg, const char *key"
71 .Fn asl_unset "aslmsg msg, const char *key"
73 .Fn asl_log "aslclient asl, aslmsg msg, int level, const char *format, ..."
75 .Fn asl_vlog "aslclient asl, aslmsg msg, int level, const char *format, va_list ap"
77 .Fn asl_send "aslclient asl, aslmsg msg"
79 .Fn asl_add_log_file "aslclient asl, int fd"
81 .Fn asl_remove_log_file "aslclient asl, int fd"
83 .Fn asl_set_filter "aslclient asl, int f"
85 .Fn asl_search "aslclient asl, aslmsg msg"
87 .Fn aslresponse_next "aslresponse r"
89 .Fn aslresponse_free "aslresponse a"
91 These routines provide an interface to the Apple system log facility.
92 They are intended to be a replacement for the
94 API, which will continue to be supported for backwards compatibility.
95 The new API allows client applications to create flexible, structured messages and send them to the
97 server, where they may undergo additional processing.
98 Messages received by the server are saved in a data store (subject to input filtering constraints).
99 This API permits clients to create queries and search the message data store for matching messages.
101 At the core of this API is the aslmsg structure.
102 Although the structure is opaque and may not be directly manipulated, it contains a list of key/value pairs.
103 All keys and values are NULL-terminated C language character strings.
104 UTF-8 encoding may be used for non-ASCII characters.
106 Message structures are generally used to send log messages, and are created thusly:
108 aslmsg m = asl_new(ASL_TYPE_MSG);
110 Another message type, ASL_TYPE_QUERY, is used to create queries when searching the data store.
111 Query type messages and searching are described in detail in the
114 For the remainder of this section, the messages described will be of the ASL_TYPE_MSG variety.
116 Each aslmsg contains a default set of keys and values associated with them.
117 These keys are listed in the asl.h header file.
120 #define ASL_KEY_TIME "Time"
121 #define ASL_KEY_HOST "Host"
122 #define ASL_KEY_SENDER "Sender"
123 #define ASL_KEY_PID "PID"
124 #define ASL_KEY_UID "UID"
125 #define ASL_KEY_GID "GID"
126 #define ASL_KEY_LEVEL "Level"
127 #define ASL_KEY_MSG "Message"
129 Many of these correspond to equivalent parts of messages described in the
132 Values associated with these message keys are assigned appropriate defaults.
133 The value for ASL_KEY_HOST is the local host name,
134 the value associated with ASL_KEY_SENDER is the process name,
135 the ASL_KEY_PID is the client's process ID number, and so on.
137 Note the addition of the UID and GID keys.
138 The values for UID and GID are set in library code by the message sender.
139 The server will attempt to confirm the values, but no claim is made that these
140 values cannot be maliciously overridden in an attempt to deceive a log message
141 reader as to the identity of the sender of a message.
142 The contents of log messages must be regarded as insecure.
144 Also note the absence of a Facility key.
147 API does not require a process to choose a facility name.
150 server will use a default value of
152 if a facility is not set.
153 However, a client may set a facility name using:
155 asl_set(m, "Facility", "UsefulService");
157 An application may choose any facility name at will.
159 Default values are set in the message for each of the keys listed above except for
160 ASL_KEY_MSG, which may be explicitly set at any time using the
162 routine, or implicitly set at the time the message is sent using the
167 These two routines also have an integer level parameter for specifying the log priority.
168 The ASL_KEY_LEVEL value is set accordingly.
169 Finally, the value associated with ASL_KEY_TIME is set in the sending routine.
171 Although it may appear that there is significant overhead required to send a log message using this API,
172 the opposite is actually true.
175 program requires only:
179 asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");
185 will provide the appropriate default values when passed a NULL aslmsg argument.
188 In this example, the aslclient argument is NULL.
189 This is sufficient for a single-threaded application,
190 or for an application which only sends log messages from a single thread.
191 When logging from multiple threads, each thread must open a separate client handle using
193 The client handle may then be closed when it is no longer required using
196 When an application requires additional keys and values to be associated with each log message,
197 a single message structure may be allocated and set up as
201 aslmsg m = asl_new(ASL_TYPE_MSG);
202 asl_set(m, "Facility", "Spy vs. Spy");
203 asl_set(m, "Clearance", "Top Secret");
205 asl_log(NULL, m, ASL_LEVEL_NOTICE, "Message One");
207 asl_log(NULL, m, ASL_LEVEL_ERR, "Message Two");
209 The message structure will carry the values set for the
213 keys so that they are used in each call to
215 while the log level and the message text are taken from the calling parameters.
217 Key/value pairs may be removed from a message structure with
219 A message may be freed using
228 to transmit a message to the server.
229 This routine sets the value associated with ASL_KEY_TIME and send the message.
230 It may be called directly if all of a message's key/value pairs have been created using
233 When logging is done from a single thread,
234 a NULL value may be used in any of the routines that require an aslclient argument.
235 In this case the library will open an internal client handle on behalf of the application.
237 If multiple threads must do logging,
238 or if client options are desired,
239 then the application should call
241 to create a client handle for each thread.
245 routine may be given an ident argument,
246 which becomes the default value for the ASL_KEY_SENDER key,
247 and a facility argument,
248 which becomes the default facility name for the application.
250 Several options are available when creating a client handle.
253 .Bl -tag -width "ASL_OPT_NO_REMOTE" -compact
255 adds stderr as an output file descriptor
257 connects to the server immediately
258 .It ASL_OPT_NO_REMOTE
259 disables remote-control filter adjustment
262 See the FILTERING section below, and the
264 for additional details on filter controls.
266 A client handle is closed and it's resources released using
268 Note that if additional file descriptors were added to the handle either using the
269 ASL_OPT_STDERR option or afterwards with the
271 routine, those file descriptors are not closed by
273 .Ss LOGGING TO ADDITIONAL FILES
274 If a client handle is opened with the ASL_OPT_STDERR option to
276 a copy of each log message will be sent to stderr.
277 Additional output streams may be include using
278 .Nm asl_add_log_file .
279 File descriptors may be removed from the list of outputs associated with a client handle with
280 .Nm asl_remove_log_file .
281 This routine simply removes the file descriptor from the output list.
282 The file is not closed as a result.
284 The ASL_OPT_STDERR option may not be unset after a client handle has been opened.
286 In the present release of Mac OS X, a
288 format is used to format messages sent to file descriptors added to a client handle.
289 Each message is preceded by a 10-character field containing a message length.
290 The message length is padded with leading white space.
291 The length gives the string length of the remainder of the output string.
292 Following the length is a space character, and then the message.
293 The message is encoded as a set of key/value pairs enclosed in square brackets,
294 which are themselves separated by a space character.
295 The key is separated from the value by space character.
296 Embedded closing square brackets are escaped by a backslash.
297 Embedded space characters in keys are escaped by a backslash;
298 Embedded newlines are summarily turned into semicolons.
299 The output is terminated by a trailing newline and a NUL character.
303 server archives received messages in a data store that may be searched using the
305 .Nm aslresponse_next ,
309 A query message is created using:
311 aslmsg q = asl_new(ASL_TYPE_QUERY);
313 Search settings are made in the query using
315 A search is performed on the data store with
320 The caller may then call
322 to iterate through matching messages.
325 structure may be freed with
326 .Nm aslresponse_free .
328 Like other messages, ASL_TYPE_QUERY messages contain keys and values.
329 They also associate an operation with each key and value.
330 The operation is used to decide if a message matches the query.
331 The simplest operation is ASL_QUERY_OP_EQUAL, which tests for equality.
332 For example, the following code snippet searches for messages with a Sender value equal to
337 q = asl_new(ASL_TYPE_QUERY);
338 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
339 r = asl_search(NULL, q);
341 More complex searches may be performed using other query operations.
343 .Bl -tag -width "ASL_QUERY_OP_GREATER_EQUAL" -compact
344 .It ASL_QUERY_OP_EQUAL
346 .It ASL_QUERY_OP_GREATER
348 .It ASL_QUERY_OP_GREATER_EQUAL
349 value greater than or equal to
350 .It ASL_QUERY_OP_LESS
352 .It ASL_QUERY_OP_LESS_EQUAL
353 value less than or equal to
354 .It ASL_QUERY_OP_NOT_EQUAL
356 .It ASL_QUERY_OP_REGEX
357 regular expression search
358 .It ASL_QUERY_OP_TRUE
359 always true - use to test for the existence of a key
362 Regular expression search uses
365 Patterns are compiled using the REG_EXTENDED and REG_NOSUB options.
367 Modifiers that change the behavior of these operations may also be specified
368 by ORing the modifier value with the operation.
371 .Bl -tag -width "ASL_QUERY_OP_SUBSTRING" -compact
372 .It ASL_QUERY_OP_CASEFOLD
373 string comparisons are case-folded
374 .It ASL_QUERY_OP_PREFIX
375 match a leading substring
376 .It ASL_QUERY_OP_SUFFIX
377 match a trailing substring
378 .It ASL_QUERY_OP_SUBSTRING
380 .It ASL_QUERY_OP_NUMERIC
381 values are converted to integer using
385 The only modifier that is checked for ASL_QUERY_OP_REGEX search is ASL_QUERY_OP_CASEFOLD.
386 This causes the regular expression to be compiled with the REG_ICASE option.
388 If a query message contains more than one set of key/value/operation triples,
389 the result will be a logical AND. For example, to find messages from
391 with a priority level less than or equal to
396 q = asl_new(ASL_TYPE_QUERY);
397 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
398 asl_set_query(q, ASL_KEY_LEVEL, "3",
399 ASL_QUERY_OP_LESS_EQUAL | ASL_QUERY_OP_NUMERIC);
400 r = asl_search(NULL, q);
408 to iterate through all matching messages.
409 To iterate through the keys and values in a message, use
411 to iterate through the keys, then call
413 to get the value associated with each key.
417 const char *key, *val;
420 r = asl_search(NULL, q);
421 while (NULL != (m = aslresponse_next(r)))
423 for (i = 0; (NULL != (key = asl_key(m, i))); i++)
425 val = asl_get(m, key);
431 .Ss FILTERING AND REMOTE CONTROL
432 Clients may set a filter mask value with
434 The mask specifies which messages should be sent to the
436 daemon by specifying a yes/no setting for each priority level.
437 Clients typically set a filter mask to avoid sending relatively unimportant messages.
438 For example, Debug or Info priority level messages are generally only useful for debugging operations.
439 By setting a filter mask, a process can improve performance by avoiding
440 sending messages that are in most cases unnecessary.
442 As a convenience, the macros ASL_FILTER_MASK(level) and ASL_FILTER_MASK_UPTO(level)
443 may be used to construct a bit mask corresponding to a given priority level,
444 or corresponding to a bit mask for all priority levels from ASL_LEVEL_EMERG to a
447 The default filter mask is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
448 This means that by default, and in the absence of remote-control changes (described below),
449 ASL_LEVEL_DEBUG and ASL_LEVEL_INFO priority level messages are not sent to the
453 Three different filters exist for each application.
454 The first is the filter mask set using
457 The Apple System Log facility also manages a
460 The master filter mask usually has a value that indicates to the library that it is
462 and thus it has no effect.
463 However, the mask filter mask may be enabled by giving it a value using the
468 When the master filter mask has been set,
469 it takes precedence over the client's filter mask.
470 The client's mask is unmodified, and will become active again if remote-control filtering is disabled.
472 In addition to the master filter mask,
473 The Apple System Log facility also manages a per-client remote-control filter mask.
474 Like the master filter mask, the per-client mask is usually
476 having no effect on a client.
477 If a per-client filter mask is set using the
481 option, then it takes precedence over both the client's filter mask and the master filter mask.
482 As is the case with the master filter mask, a per-client mask ceases having any effect when if is disabled.
484 The ASL_OPT_NO_REMOTE option to
486 causes both the master and per-client remote-control masks to be ignored in the library.
487 In that case, only the client's own filter mask is used to determine which messages are
489 This may be useful for Applications that produce log messages that should never be filtered
490 due to security considerations.
491 Note that root (administrator) access is required to set or change the master filter mask,
492 and that only root may change a per-client remote-control filter mask for a root (UID 0) process.
494 These functions first appeared in