]> git.saurik.com Git - apple/syslog.git/blame - libsystem_asl.tproj/man/asl.3
syslog-377.0.1.tar.gz
[apple/syslog.git] / libsystem_asl.tproj / man / asl.3
CommitLineData
f3df4c03 1.\" Copyright (c) 2005-2013 Apple Inc.
81582353
A
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
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.
15.\"
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
26.\" SUCH DAMAGE.
27.\"
28.\"
29.Dd October 1, 2011
30.Dt asl 3
31.Os "Mac OS X"
32.Sh NAME
33.Nm asl_add_log_file ,
86a8bcf5 34.Nm asl_add_output_file ,
f3df4c03 35.Nm asl_append ,
81582353
A
36.Nm asl_close ,
37.Nm asl_close_auxiliary_file ,
f3df4c03 38.Nm asl_count ,
81582353 39.Nm asl_create_auxiliary_file ,
f3df4c03
A
40.Nm asl_decode_buffer ,
41.Nm asl_encode_buffer ,
42.Nm asl_fetch_key_val_op ,
43.Nm asl_format ,
81582353
A
44.Nm asl_free ,
45.Nm asl_get ,
f3df4c03
A
46.Nm asl_get_index ,
47.Nm asl_get_type ,
81582353
A
48.Nm asl_key ,
49.Nm asl_log ,
50.Nm asl_log_auxiliary_location ,
51.Nm asl_log_descriptor ,
f3df4c03 52.Nm asl_match ,
81582353 53.Nm asl_new ,
f3df4c03 54.Nm asl_next ,
81582353
A
55.Nm asl_open ,
56.Nm asl_open_from_file ,
f3df4c03
A
57.Nm asl_open_path ,
58.Nm asl_prepend ,
59.Nm asl_prev ,
60.Nm asl_release ,
61.Nm asl_remove_index ,
81582353 62.Nm asl_remove_log_file ,
f3df4c03
A
63.Nm asl_reset_iteration ,
64.Nm asl_retain ,
81582353
A
65.Nm asl_search ,
66.Nm asl_send ,
67.Nm asl_set ,
68.Nm asl_set_filter ,
69.Nm asl_set_output_file_filter ,
70.Nm asl_set_query ,
71.Nm asl_unset ,
72.Nm asl_vlog ,
73.Nm aslresponse_free ,
74.Nm aslresponse_next
75.Nd system log message sending and searching functions
76.Sh SYNOPSIS
77.Fd #include <asl.h>
78.\"
79.Ft int
80.Fo asl_add_log_file
f3df4c03 81.Fa "asl_object_t client"
81582353
A
82.Fa "int descriptor"
83.Fc
84.Ft int
85.Fo asl_add_output_file
f3df4c03 86.Fa "asl_object_t client"
81582353
A
87.Fa "int descriptor"
88.Fa "const char *msg_fmt"
89.Fa "const char *time_fmt"
90.Fa "int filter"
91.Fa "int text_encoding"
92.Fc
93.Ft void
f3df4c03
A
94.Fo asl_append
95.Fa "asl_object_t obj"
96.Fa "asl_object_t msg"
81582353 97.Fc
f3df4c03 98.Pp
81582353 99.Ft void
f3df4c03
A
100.Fo asl_close
101.Fa "asl_object_t obj"
102.Fc
103.Ft int
81582353
A
104.Fo asl_close_auxiliary_file
105.Fa "int descriptor"
106.Fc
f3df4c03
A
107.Ft size_t
108.Fo asl_count
109.Fa "asl_object_t obj"
110.Fc
111.Ft int
81582353 112.Fo asl_create_auxiliary_file
f3df4c03 113.Fa "asl_object_t msg"
81582353
A
114.Fa "const char *title"
115.Fa "const char *uti"
116.Fa "int *out_descriptor"
117.Fc
f3df4c03
A
118.Ft int
119.Fo asl_decode_buffer
120.Fa "const char *in"
121.Fa "char **buf"
122.Fa "size_t *len"
123.Fc
124.Ft char *
125.Fo asl_encode_buffer
126.Fa "const char *in"
127.Fa "size_t len"
128.Fc
129.Ft int
130.Fo asl_fetch_key_val_op
131.Fa "asl_object_t msg"
132.Fa "uint32_t n"
133.Fa "const char **key"
134.Fa "const char **val"
135.Fa "uint32_t *op"
136.Fc
137.Ft char *
138.Fo asl_format
139.Fa "asl_object_t msg"
140.Fa "const char *msg_fmt"
141.Fa "const char *time_fmt"
142.Fa "uint32_t text_encoding"
143.Fc
144.Ft [DEPRECATED] void
81582353 145.Fo asl_free
f3df4c03 146.Fa "asl_object_t obj"
81582353
A
147.Fc
148.Ft const char *
149.Fo asl_get
f3df4c03 150.Fa "asl_object_t msg"
81582353
A
151.Fa "const char *key"
152.Fc
f3df4c03
A
153.Ft asl_object_t
154.Fo asl_get_index
155.Fa "asl_object_t list"
156.Fa "size_t index"
157.Fc
158.Ft uint32_t
159.Fo asl_get_type
160.Fa "asl_object_t obj"
161.Fc
81582353
A
162.Ft const char *
163.Fo asl_key
f3df4c03 164.Fa "asl_object_t msg"
81582353
A
165.Fa "uint32_t n"
166.Fc
167.Ft int
168.Fo asl_log
f3df4c03
A
169.Fa "asl_object_t obj"
170.Fa "asl_object_t msg"
81582353
A
171.Fa "int level"
172.Fa "const char *format"
173.Fa "..."
174.Fc
f3df4c03 175.Ft int
81582353 176.Fo asl_log_auxiliary_location
f3df4c03 177.Fa "asl_object_t msg"
81582353
A
178.Fa "const char *title"
179.Fa "const char *uti"
180.Fa "const char *url"
181.Fc
182.Ft int
183.Fo asl_log_descriptor
f3df4c03
A
184.Fa "asl_object_t client"
185.Fa "asl_object_t msg"
81582353
A
186.Fa "int level"
187.Fa "int descriptor"
188.Fa "uint32_t fd_type"
189.Fc
f3df4c03
A
190.Ft int
191.Fo asl_log_message
192.Fa "int level"
193.Fa "const char *format"
194.Fa "..."
195.Fc
196.Ft asl_object_t
197.Fo asl_match
198.Fa "asl_object_t obj"
199.Fa "asl_object_t querylist"
200.Fa "size_t *last"
201.Fa "size_t start"
202.Fa "size_t count"
203.Fa "uint32_t duration"
204.Fa "int32_t direction"
205.Fc
206.Ft asl_object_t
81582353
A
207.Fo asl_new
208.Fa "uint32_t type"
209.Fc
f3df4c03
A
210.Ft asl_object_t
211.Fo asl_next
212.Fa "asl_object_t obj"
213.Fc
214.Ft asl_object_t
81582353
A
215.Fo asl_open
216.Fa "const char *ident"
217.Fa "const char *facility"
218.Fa "uint32_t opts"
219.Fc
f3df4c03 220.Ft asl_object_t
81582353
A
221.Fo asl_open_from_file
222.Fa "int descriptor"
223.Fa "const char *ident"
224.Fa "const char *facility"
225.Fc
f3df4c03
A
226.Ft asl_object_t
227.Fo asl_open_path
228.Fa "const char *path"
229.Fa "uint32_t opts"
230.Fc
231.Ft void
232.Fo asl_prepend
233.Fa "asl_object_t obj"
234.Fa "asl_object_t msg"
235.Fc
236.Ft asl_object_t
237.Fo asl_prev
238.Fa "asl_object_t obj"
239.Fc
240.Ft void
241.Fo asl_release
242.Fa "asl_object_t obj"
243.Fc
244.Ft void
245.Fo asl_remove_index
246.Fa "asl_object_t list"
247.Fa "size_t index"
248.Fc
81582353
A
249.Ft int
250.Fo asl_remove_log_file
f3df4c03 251.Fa "asl_object_t asl"
81582353
A
252.Fa "int descriptor"
253.Fc
f3df4c03
A
254.Ft void
255.Fo asl_reset_iteration
256.Fa "asl_object_t obj"
257.Fa "size_t position"
258.Fc
259.Ft asl_object_t
260.Fo asl_retain
261.Fa "asl_object_t obj"
262.Fc
263.Ft asl_object_t
81582353 264.Fo asl_search
f3df4c03
A
265.Fa "asl_object_t obj"
266.Fa "asl_object_t query"
81582353
A
267.Fc
268.Ft int
269.Fo asl_send
f3df4c03
A
270.Fa "asl_object_t obj"
271.Fa "asl_object_t msg"
81582353
A
272.Fc
273.Ft int
274.Fo asl_set
f3df4c03 275.Fa "asl_object_t msg"
81582353
A
276.Fa "const char *key"
277.Fa "const char *value"
278.Fc
279.Ft int
280.Fo asl_set_filter
f3df4c03 281.Fa "asl_object_t asl"
81582353
A
282.Fa "int filter"
283.Fc
284.Ft int
285.Fo asl_set_output_file_filter
f3df4c03 286.Fa "asl_object_t asl"
81582353
A
287.Fa "int descriptor"
288.Fa "int filter"
289.Fc
290.Ft int
291.Fo asl_set_query
f3df4c03 292.Fa "asl_object_t msg"
81582353
A
293.Fa "const char *key"
294.Fa "const char *value"
295.Fa "uint32_t op"
296.Fc
297.Ft int
298.Fo asl_unset
f3df4c03 299.Fa "asl_object_t msg"
81582353
A
300.Fa "const char *key"
301.Fc
302.Ft int
303.Fo asl_vlog
f3df4c03
A
304.Fa "asl_object_t obj"
305.Fa "asl_object_t msg"
81582353
A
306.Fa "int level"
307.Fa "const char *format"
308.Fa "va_list ap"
309.Fc
f3df4c03 310.Ft [DEPRECATED] void
81582353 311.Fo aslresponse_free
f3df4c03 312.Fa "asl_object_t obj"
81582353 313.Fc
f3df4c03 314.Ft [DEPRECATED] asl_object_t
81582353 315.Fo aslresponse_next
f3df4c03 316.Fa "asl_object_t obj"
81582353
A
317.Fc
318.Sh DESCRIPTION
af7d442c
A
319.Bf -symbolic
320This interface is obsoleted by
321.Xr os_log 3 .
322.Ef
323.Pp
f3df4c03
A
324These routines provide an interface to the Apple System Log facility and to various
325data bearing memory objects, files, and storage directories.
326.Pp
327The API allows client applications to create and manipulate
328flexible, structured messages, send them to the
81582353
A
329.Nm syslogd
330server, where they may undergo additional processing.
331Messages received by the server are saved in a data store
332(subject to input filtering constraints).
f3df4c03
A
333.Pp
334Log messages may also be written directly to the filesystem from the ASL library.
335This output may go to plain text files, to ASL-format data files, or to ASL databases.
336.Pp
81582353 337This API permits clients to create queries
f3df4c03
A
338and search the system ASL database, ASL-format files, or other ASL databases for matching messages.
339.Pp
340Clients that simply need to send messages to the ASL server may do so using
341.Fn asl_log_message .
342Other routines allow for more complex logging tasks.
81582353
A
343.Pp
344An introduction to the concepts underlying this interface follows the interface summary below.
345.Ss INTERFACE SUMMARY
f3df4c03
A
346.Fo asl_log_message
347.Fa level
348.Fa format
349.Fa "..."
350.Fc
351sends a message to the ASL server
352.Nm syslogd .
353.Fa level
354is an integer between 7 (ASL_LEVEL_DEBUG) and 0 (ASL_LEVEL_EMERG),
355indicating the priority of the message.
356Note that message priority levels are used as the basis of filtering
357messages in several places in the ASL system.
358In general, messages with level ASL_LEVEL_DEBUG and ASL_LEVEL_INFO are often excluded from long-term storage,
359or have shorter time-to-live durations.
360.Pp
361.Fa format
362is a printf-like format string.
363In addition to the conversion specifications supported by
364.Nm printf ,
365.Fn asl_log_message
366supports the
367.Dq %m
368conversion, which is converted to the current error string returned by the
369.Nm strerror
370function for the current value of
371.Fa errno .
372.Pp
373.Fn asl_log_message
374is a simplified version of the
375.Fn asl_log
376interface.
377It uses the default (NULL) ASL client handle.
378This interface is thread-safe, although callers will contend for a mutex lock when using this routine.
379Applications that log from multiple threads or dispatch queues may experience undesired performance
380characteristics when using this routine.
381The use of
382.Fn asl_open
383and
384.Fn asl_log ,
385.Fn asl_vlog ,
386or
387.Fn asl_send
388is advised for applications that log from multiple threads.
389.Pp
390.Fo asl_log
391.Fa obj
392.Fa msg
393.Fa level
394.Fa format
395.Fa "..."
396.Fc
397prepares a message, normally to be sent to the ASL server
398.Nm syslogd .
399The first parameter,
400.Fa obj ,
401may be an asl_object_t of any type.
402It is typically of type ASL_TYPE_CLIENT.
403In this case the settings and options associated with the ASL client handle
404.Fa obj
405are used when preparing the message.
406The client may direct the ASL library to
407print copies of the message to various output files as well as sending it to the ASL server.
408Filter settings in the client may further direct the library in selecting where the message
409will be sent, and may in fact prevent the message from being sent to the ASL server at all.
410ASL client handles are created using
411.Fn asl_open
412and are described extensively below.
413.Pp
414ASL message are dictionaries.
415The
416.Fn asl_log
417routine combines information carried in the ASL client handle
418.Fa client
419and the ASL message dictionary
420.Fa msg ,
421together with the
422.Fa format
423string and the associated arguments to construct a final message to be sent
424to the ASL server and/or to be written to output files.
425In general, the ASL client handle will provide the values for the
426ASL_KEY_SENDER and ASL_KEY_FACILITY keys.
427If
428.Fa msg
429is non-NULL, it may override the values for ASL_KEY_SENDER and ASL_KEY_FACILITY,
430and it may supply additional key/value pairs.
431The
432.Fa format
86a8bcf5 433string and its associated arguments are used to construct a string value for the
f3df4c03
A
434ASL_KEY_MSG key in the final log message dictionary.
435.Pp
436If the
437.Fa obj
438parameter is of a type other than ASL_TYPE_CLIENT, then
439.Fn asl_log
440creates a message as if it were to be sent to
441.Nm syslogd ,
442but rather than sending the message, it stores the message in the
443.Fa obj
444provided.
445If
446.Fa obj
447is of type ASL_TYPE_FILE or ASL_TYPE_STORE that has been opened for writing,
448then the message is saved to the file or ASL data store.
449If
450.Fa obj
451is of type ASL_TYPE_LIST, then the message is appended to the list.
452If
453.Fa obj
454is of type ASL_TYPE_MSG, then the message key/value pairs constructed by
455.Fn asl_log
456are merged with
457.Fa obj .
458In a merge operation, existing keys and values in
459.Fa obj
460are preserved.
461New values in the
462.Fn asl_log
463message are attached.
464Although this routine works for type ASL_TYPE_QUERY,
465new key/value pairs are attached with an operation value of zero.
466.Pp
467The ASL_PREFILTER_LOG(obj, msg, level, format, ...) macro may be used in
468place of
469.Fn asl_log
470when
471.Fa obj
472is of type ASL_TYPE_CLIENT.
473The macro avoids processing the variable argument list in those cases where
474the message would be filtered out due to filter settings, would not be
475written to a log file associated with the asl_object_t, or would not be
476written to stderr.
477The macro may provide a performance benefit for some applications.
478Details on filter setting, additional log files, and asl_object_t options
479are described below in this manual.
480.Pp
481.Fo asl_vlog
482.Fa obj
483.Fa msg
484.Fa level
485.Fa format
486.Fa ap
487.Fc
488is similar to
489.Fn asl_log
490except that it takes a va_list argument.
491.Pp
492.Fo asl_send
493.Fa obj
494.Fa msg
495.Fc
496is similar to
497.Fn asl_log ,
5222c21d 498except the value for ASL_KEY_MSG is taken from
f3df4c03
A
499.Ar msg
500rather than being constructed using a
501.Fn printf
502style syntax.
503.Pp
81582353
A
504.Fo asl_open
505.Fa ident
506.Fa facility
507.Fa opts
508.Fc
509creates and returns a client handle, or NULL if an error occurs in the library.
510Messages sent using this handle will default to having the string
511.Ar ident
512as the value associated with the ASL_KEY_SENDER key, and the value
513.Ar facility
514associated with the ASL_KEY_FACILITY key.
f3df4c03
A
515If
516.Ar ident
517is NULL, the library uses the sending process name.
518If
519.Ar facility
520is NULL, the library will use the
521.Dq user
522facility for processes with non-zero UID, and
523.Dq daemon
524for processes with zero UID.
525.Pp
81582353
A
526Several options are available, as described in the
527.Sx CLIENT HANDLES
528section.
529.Pp
530Each client handle holds state information that is used when a message is logged using that handle.
531This information includes the
532.Ar ident
533and
534.Ar facility
535strings and the options from the
536.Ar opts
537parameter.
538Client handles also contain various filter, file descriptor, and control data.
539.Pp
f3df4c03
A
540The state information in a client handle is not protected by locking or thread synchronization mechanisms,
541except for one special case where NULL is used as a client handle.
542That special case is described below.
543.Pp
81582353 544It is not safe for two or more threads to use a single client handle simultaneously.
f3df4c03
A
545Multi-threaded applications should generally create one client handle for each thread
546or serial dispatch queue that logs messages.
81582353
A
547A client handle may only be safely shared amongst multiple threads if the application uses locks or some
548synchronization strategy to ensure single-threaded access.
549.Pp
550As a special case, the ASL library allows the use of NULL in place of a client handle.
551In this case, the library uses an internal structure which contains its own lock.
f3df4c03
A
552Multiple threads may safely use NULL in place of an ASL client handle,
553although there may be contention for the lock.
81582353
A
554.Pp
555Applications that use libdispatch may use NULL in place of a client handle,
556although this may cause undesirable synchronization behavior and degraded performance because of lock contention.
557A better design is often to use one or more serial dispatch queues specifically for logging.
558Each such serial queue should use a separate client handle.
559.Pp
f3df4c03
A
560.Fo asl_open_path
561.Fa path
562.Fa opts
563.Fc
564opens an ASL data store or ASL data file for read or write access.
565Returns an object of type ASL_TYPE_STORE or ASL_TYPE_FILE,
566depending on the input parameters.
567By default, the ASL store or file is opened for reading.
568The routine checks the filesystem type of
569.Fa path ,
570and returns an object of type ASL_TYPE_STORE for an ASL data store (a directory in the filesystem)
571or an object of type ASL_TYPE_FILE for an ASL data file.
572If
573.Fa path
574is NULL, the system's ASL database (/var/log/asl) is opened.
575.Pp
576If the ASL_OPT_OPEN_WRITE option is specified, an existing file or database is
577opened for writing.
578New messages may be added to the file or database using
579.Fn asl_log ,
580.Fn asl_vlog ,
581.Fn asl_send ,
582or
583.Fn asl_append .
584Existing messages in the store or file may not be deleted or modified.
585.Pp
586If the path does not exist in the filesystem,
587.Fn asl_open_path
588will create a new data store if ASL_OPT_CREATE_STORE is set in the options,
589The file will be created with the user's effective UID and GID as owner and group.
590The mode will be 0644.
591If a different mode, UID, or GID is desired, an empty file or directory may be
592pre-created with the desired settings.
593.Pp
81582353
A
594.Fo asl_close
595.Fa asl
596.Fc
597closes the client handle
598.Ar asl
599and releases its associated resources.
f3df4c03
A
600.Fn asl_release
601may also be used to close a client handle.
602.Pp
603.Fo asl_set_filter
604.Fa asl
605.Fa f
606.Fc
607sets a filter for messages being sent to the server.
608The filter is a bitmask representing priority levels.
609Only messages having a priority level with a corresponding bit set in the filter mask are sent to the
610.Nm syslogd
611server.
612The filter does not control writes to additional files associated with the client handle using
613.Fn asl_add_output_file .
614.Fn asl_set_filter
615returns the previous filter value.
81582353
A
616.Pp
617.Fo asl_add_output_file
618.Fa asl
619.Fa descriptor
620.Fa msg_fmt
621.Fa time_fmt
622.Fa filter
623.Fa text_encoding
624.Fc
625adds the file descriptor
626.Ar descriptor
627to the a set of file descriptors associated with the client handle
628.Ar asl .
629Each log message sent by that client handle is also written to these file descriptors
630(depending on the setting of the
631.Ar filter
632argument).
633The message format is specified by the
634.Ar msg_fmt
635argument.
636The format for timestamps is specified by the
637.Ar time_fmt
638argument, although custom format strings may specify more advanced formats for timestamps.
639Details on custom format strings are below.
640.Pp
641Each output file has an associated
642.Ar filter
643value.
644The filter determines which messages are formatted and written to the file based on the message priority level.
645.Pp
646Special handling for certain characters is specified by the
647.Ar text_encoding
648argument.
649The supported values and their effect are described below.
650.Pp
651The
652.Ar msg_format
653argument is a character string that tells the library how to format each message written to the output file.
654There are several pre-defined message formats, described below.
655Custom formats are also supported,
656giving complete control over which ASL message keys should be written
657and the overall format of each output line.
658The pre-defined formats are identified by constants in the asl.h header file.
659.Pp
660.Bl -tag -width "ASL_MSG_FMT_RAW" -compact
661.It ASL_MSG_FMT_RAW
662The contents of the ASL message dictionaries are formatted as a list,
663with each key-value pair formatted as
664.Dq [Key Value] .
665.Pp
666.It ASL_MSG_FMT_STD
667Messages are formatted using the standard ASL message format of the form
668.Pp
669.Dl Time Host Sender[PID] <Level>: Message
670.Pp
671Time formats are described below.
672.Pp
673.It ASL_MSG_FMT_BSD
674The legacy format used for plain-text log files.
675Similar to the ASL_MSG_FMT_STD format, but the message priority level is excluded.
676.Pp
677.It ASL_MSG_FMT_MSG
678The output line contains only the value of the Message key in each ASL message dictionary.
679.Pp
680.It ASL_MSG_FMT_XML
681Produces multiple lines of output for each ASL message.
682The message is formatted as an XML dictionary:
683.Pp
684.Dl <dict>
685.Dl \t<key>ASLMessageKey1</key>
686.Dl \t<string>Key 1 Value</string>
687.Dl \t<key>ASLMessageKey2</key>
688.Dl \t<string>Key 2 Value</string>
689.Dl \t\t...
690.Dl </dict>
691.Pp
692.El
693.Pp
694A NULL value for
695.Ar msg_fmt
f3df4c03 696causes the library to use the ASL_MSG_FMT_STD format.
81582353
A
697.Pp
698Custom format strings may contain a mix of characters that are directly copied to the output line
699and variables, which are a dollar sign
700.Sq $
701followed by specific ASL message dictionary keys, whose values will be interpolated into the output.
702For example, the format string:
703.Pp
704.Dl This message from $Sender PID=$PID at $Time *** $Message
705.Pp
706would result in lines in the output file like, e.g.:
707.Pp
708.Dl This message from login PID=982 at Jul 27 08:41:27 *** USER_PROCESS: 330 ttys000
709.Dl This message from Mail PID=987 at Jul 27 08:42:16 *** Using V2 Layout
710.Pp
711Normally, a space character terminates a variable name.
712However, the name may be wrapped in parentheses if a space character is not desired in the output.
713For example:
714.Pp
715.Dl $(Sender)[$(PID)]: $Message
716.Pp
717A third form for specifying variables may be used for the ASL
718.Dq Level
719and
720.Dq Time
721message keys.
722Note that a
723.Dq Time
724specification using one of the forms below will override the
725.Ar time_fmt
726argument to the function.
727.Pp
728The following forms are recognized:
729.Pp
730.Bl -tag -width "$((Time)([+|-]HH[:MM]))"
731.It $((Level)(str))
732Formats a Level value as a string, for example
733.Dq Error ,
734.Dq Alert ,
735.Dq Warning ,
736and so on.
737Note that $(Level) or $Level formats the value as an integer 0 through 7.
f3df4c03
A
738.It $((Level)(char))
739Formats a Level value as a single character from the set
740.Dq PACEWNID ,
741for levels 0 through 7.
742These are abbreviations for Panic, Alert, Critical, Error, Warning, Notice, Info, and Debug.
81582353
A
743.It $((Time)(sec))
744Formats a Time value as the number of seconds since the Epoch.
745.It $((Time)(raw))
746Alias for $((Time)(sec)).
747.It $((Time)(local))
748Formats a Time value as a string of the form
749.Dq "Mmm dd hh:mm:ss" ,
750where Mmm is the abbreviation for the month, dd is the date (1 - 31) and hh:mm:ss is the time.
751The local timezone is used.
752.It $((Time)(lcl))
753Alias for $((Time)(local)).
754.It $((Time)(utc))
755Formats a Time value as a string of the form
756.Dq "yyyy-mm-dd hh:mm:ssZ" ,
757using Coordinated Universal Time, or the
758.Dq Zulu
759time zone.
760.It $((Time)(zulu))
761Alias for $((Time)(utc)).
762.It $((Time)(X))
763Where X may be any letter in the range A - Z or a - z.
764Formats the Time using the format
765.Dq "yyyy-mm-dd hh:mm:ssX" ,
766using the specified nautical timezone.
767Z is the same as UTC/Zulu time. Timezones A - M (except J) decrease by one hour to the east of the
768Zulu time zone.
769Timezones N - Y increase by one hour to the west of Z.
770M and Y have the same clock time, but differ by one day.
771J is used to indicate the local timezone.
772When printing using $((Time)(J)), the output format is
773.Dq "yyyy-mm-dd hh:mm:ss" ,
774without a trailing timezone letter.
775.It $((Time)(JZ))
776Specifies the local timezone.
777The timezone offset from UTC follows the date and time.
778The time is formatted as
779.Dq "yyyy-mm-dd hh:mm:ss[+|-]HH[:MM]" .
780Minutes in the timezone offset are only printed if they are non-zero.
781.It $((Time)(ISO8601))
782Specifies the local timezone, formatted as specified by ISO 8601.
783The timezone offset from UTC follows the date and time.
784The time is formatted as
785.Dq "yyyy-mm-ddThh:mm:ss[+|-]HH[:MM]" .
786Minutes in the timezone offset are only printed if they are non-zero.
787Note that this differs from
788.Dq JZ
789format only in that a
790.Dq T
791character separates the date and time.
792.It $((Time)([+|-]HH[:MM]))
793Specifies an offset (+ or -) of the indicated number of hours (HH) and optionally minutes (MM) to UTC.
794The value is formatted as a string of the form
795.Dq "yyyy-mm-dd hh:mm:ss[+|-]HH[:MM]" .
796Minutes in the timezone offset are only printed if they are non-zero.
797.El
798.Pp
799Unless a custom message format uses one of the specialized forms for
800.Dq Time
801described above, then any timestamps in an output message will be formatted according the the
802.Ar time_fmt
803argument.
804The known formats are identified by constants in the asl.h header file.
805.Pp
806.Bl -tag -width "ASL_TIME_FMT_SEC"
807.It ASL_TIME_FMT_SEC
808Formats timestamps as the number of seconds since the Epoch.
809.Pp
810.It ASL_TIME_FMT_UTC
811Formats a Time value as a string of the form
812.Dq "yyyy-mm-dd hh:mm:ssZ" ,
813using Coordinated Universal Time, or the
814.Dq Zulu
815time zone.
816.It ASL_TIME_FMT_LCL
817Formats a Time value as a string of the form
818.Dq "Mmm dd hh:mm:ss" ,
819where Mmm is the abbreviation for the month, dd is the date (1 - 31) and hh:mm:ss is the time.
820The local timezone is used.
821.El
822.Pp
823A value of NULL for the
824.Ar time_fmt
825argument will cause the default format ASL_TIME_FMT_LCL to be used.
826.Pp
827The
828.Ar encoding
829parameter specifies how certain characters are to be treated when preparing a message for output.
830The known encodings are:
831.Bl -tag -width "ASL_ENCODE_NONE"
832.It ASL_ENCODE_NONE
833No special character encode is done.
834.Pp
835.It ASL_ENCODE_ASL
836Newlines and tabs are also encoded as "\\n" and "\\t" respectively.
837In
838.Dq ASL_MSG_FMT_RAW
839format, space characters embedded in log message keys are encoded as "\\s"
840and embedded brackets are escaped to print as "\\[" and "\\]".
841.Pp
842.It ASL_ENCODE_SAFE
843Encodes backspace characters as ^H.
844Carriage returns are mapped to newlines.
845A tab character is appended after newlines so that message text is indented.
846.Pp
847.It ASL_ENCODE_XML
848This encoding should be used when formatting messages using ASL_MSG_FMT_XML.
849XML format output requires that keys are valid UTF8 strings.
850Keys which are not valid UTF8 are ignored, and the associated value is not printed.
851.Pp
852Values that contain legal UTF8 are printed as strings.
853Ampersand, less than, greater than, quotation mark, and apostrophe characters are encoded according to XML conventions.
854Embedded control characters are encoded as
855.Dq &#xNN;
856where NN is the character's hexadecimal value.
857.Pp
858Values that do not contain legal UTF8 are encoded in base-64 and printed as data objects.
859.El
860.Pp
f3df4c03 861.Fn asl_add_output_file
81582353
A
862Returns 0 on success, non-zero on failure.
863.Pp
864.Pp
865.Fo asl_add_log_file
866.Fa asl
867.Fa descriptor
868.Fc
869Is equivalent to
870.Pp
871.Dl asl_add_output_file(asl, descriptor, ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG), ASL_ENCODE_SAFE);
872.Pp
873Returns 0 on success, non-zero on failure.
874.Pp
875.Fo asl_set_output_file_filter
876.Fa asl
877.Fa descriptor
878.Fa filter
879.Fc
880replaces the current filter value associated with a file descriptor that has been added to a client handle.
881Returns the previous filter value.
882.Pp
883.Fo asl_remove_log_file
884.Fa asl
885.Fa descriptor
886.Fc
887removes a file descriptor from the set of file descriptors associated with a client handle.
888Returns 0 on success, non-zero on failure.
889.Pp
f3df4c03
A
890.Fo asl_format
891.Fa msg
892.Fa msg_fmt
893.Fa time_fmt
894.Fa text_encoding
895.Fc
896formats the
897.Fa msg
898object using the message format string, time format string, and text encoding specified.
899Message formatting is described above for the
900.Fn asl_add_output_file
901routine.
902The caller must free the returned character string.
903.Pp
81582353
A
904.Fo asl_new
905.Fa type
906.Fc
f3df4c03 907allocates and returns an asl_object_t structure, or NULL in the case of a failure in the library.
81582353
A
908The
909.Ar type
f3df4c03
A
910argument should be ASL_TYPE_MSG, ASL_TYPE_QUERY, or ASL_TYPE_LIST.
911.Pp
912.Fo asl_get_type
913.Fa obj
914.Fc
915Returns the type of the object
916.Fa obj ,
917or ASL_TYPE_UNDEF if the object is not a recognized type.
918.Pp
919.Fo asl_retain
920.Fa obj
921.Fc
922Increments an internal reference count for
923.Fa obj .
924ASL objects are created with a reference count of 1.
925Objects returned by ASL routines should be retained if they are used outside
926of the immediate scope of the call that returned them.
927.Pp
928.Fo asl_release
929.Fa obj
930.Fc
931Decrements the internal reference count for
932.Fa obj .
933It frees the object and its associated resources when the reference count becomes zero.
81582353 934.Pp
f3df4c03 935.Em DEPRECATED
81582353 936.Fo asl_free
f3df4c03 937.Fa obj
81582353 938.Fc
f3df4c03
A
939This interface is deprecated in favor of
940.Fn asl_release .
941It is implemented as a call to
942.Fn asl_release .
81582353
A
943.Pp
944.Fo asl_set
945.Fa msg
946.Fa key
947.Fa value
948.Fc
f3df4c03 949creates a new key and value in an asl_object_t structure, or replaces the value of an existing key.
81582353
A
950Returns 0 on success, non-zero on failure.
951.Pp
952.Fo asl_set_query
953.Fa msg
954.Fa key
955.Fa op
956.Fa value
957.Fc
958is used to construct searches.
959It is similar to
960.Fn asl_set ,
961except that it takes an additional
962.Ar op
963(operation) argument.
f3df4c03 964Creates a new (key, op, value) triple in an asl_object_t structure,
81582353
A
965or replaces the value and operation for an existing key.
966See the
967.Sx SEARCHING
968section for more information.
969Returns 0 on success, non-zero on failure.
970.Pp
971.Fo asl_unset
972.Fa msg
973.Fa key
974.Fc
f3df4c03 975removes a key and its associated value from an asl_object_t structure.
81582353
A
976Returns 0 on success, non-zero on failure.
977.Pp
978.Fo asl_key
979.Fa msg
980.Fa n
981.Fc
f3df4c03 982returns the nth key in an asl_object_t (beginning at zero),
81582353
A
983allowing an application to iterate through the keys.
984Returns NULL if
985.Ar n
986indexes beyond the number of keys in
987.Ar msg .
988.Pp
989.Fo asl_get
990.Fa msg
991.Fa key
992.Fc
993returns the value associated with
994.Ar key
f3df4c03 995in the asl_object_t
81582353
A
996.Ar msg .
997Returns NULL if
998.Ar msg
999does not contain
1000. Ar key .
1001.Pp
f3df4c03
A
1002.Fo asl_fetch_key_val_op
1003.Fa msg
1004.Fa n
1005.Fa key
1006.Fa val
1007.Fa op
81582353 1008.Fc
f3df4c03
A
1009Returns, in the
1010.Fa key ,
1011.Fa val ,
1012and
1013.Fa op
1014output parameters, the key, value, and operation (for ASL_TYPE_QUERY) at index
1015.Fa n
1016in the given object
1017.Fa msg .
1018The input
1019.Fa msg
1020should be of type ASL_TYPE_MSG or ASL_TYPE_QUERY.
1021Returns 0 on success, or non-zero otherwise.
1022Any of the output parameters may be NULL, in which case that parameter value will not
1023be returned.
81582353 1024.Pp
f3df4c03
A
1025.Fo asl_count
1026.Fa obj
1027.Fc
1028returns a count of the number of elements contained in
1029.Fa obj .
1030For objects of type ASL_TYPE_MSG or ASL_TYPE_QUERY,
1031this is the number of dictionary keys.
1032For ASL_TYPE_LIST, it is the number of items in the list.
1033For ASL_TYPE_FILE, returns the number of messages contained in the file.
1034Returns zero for ASL_TYPE_STORE and ASL_TYPE_CLIENT.
1035.Pp
1036.Fo asl_append
1037.Fa obj
81582353 1038.Fa msg
81582353 1039.Fc
f3df4c03
A
1040appends the
1041.Fa msg
1042object, which is typically of type ASL_TYPE_MSG or ASL_TYPE_QUERY, to the target
1043.Fa obj .
1044The target
1045.Fa obj
1046is typically a type that contains a collection of messages,
1047i.e. ASL_TYPE_LIST, ASL_TYPE_FILE, ASL_TYPE_STORE, or ASL_TYPE_CLIENT
1048(where the collection is the system ASL database).
1049.Fn asl_append
1050appends the
1051.Fa msg
1052object to the end of the target
1053.Fa obj .
81582353 1054.Pp
f3df4c03
A
1055If
1056.Fa msg
1057is of type ASL_TYPE_LIST and
1058.Fa obj
1059is of type ASL_TYPE_LIST, ASL_TYPE_FILE, ASL_TYPE_STORE, or ASL_TYPE_CLIENT,
1060the each message in the
1061.Fa msg
1062list is appended in sequence to the the target
1063.Fa obj .
81582353 1064.Pp
f3df4c03
A
1065If both
1066.Fa msg
1067and
1068.Fa obj
1069are of type ASL_TYPE_MSG or ASL_TYPE_QUERY, then the message dictionary from
1070.Fa msg
1071is merged with
1072.Fa obj .
1073Existing keys in
1074.Fa obj
1075are preserved.
1076For keys that are in
1077.Fa msg
1078that are not in
1079.Fa obj ,
1080the key and its value and operation are added to
1081.Fa obj .
1082.Pp
1083.Fo asl_prepend
1084.Fa obj
81582353 1085.Fa msg
81582353
A
1086.Fc
1087is similar to
f3df4c03
A
1088.Fn asl_append ,
1089except that the
1090.Fa msg
1091object is prepended to the target
1092.Fa obj.
1093In the case where both parameters are of type ASL_TYPE_MSG or ASL_TYPE_QUERY,
1094all keys from
81582353 1095.Fa msg
f3df4c03
A
1096are copied to
1097.Fa obj .
1098Existing keys are not preserved.
1099.Pp
1100.Fo asl_next
1101.Fa obj
1102.Fc
1103returns the next item in the target
1104.Fa obj ,
1105which may be of type ASL_TYPE_LIST, ASL_TYPE_FILE, ASL_TYPE_STORE, or of type ASL_TYPE_CLIENT
1106in which case the routine fetches messages consecutively from the system ASL database.
1107Returned objects are of type ASL_TYPE_MSG, or of type ASL_TYPE_QUERY if the target object is a
1108list containing query objects.
1109Returns NULL when there are no more objects to return from the target.
1110.Pp
1111.Fo asl_prev
1112.Fa obj
81582353
A
1113.Fc
1114is similar to
f3df4c03
A
1115.Fn asl_next ,
1116except that it returns objects in reverse order.
1117Objects that contain messages have an internal index for the
1118.Dq current
1119item.
1120.Fn asl_next
1121and
1122.Fn asl_prev
1123simply return the current item and move the index forward or backward.
1124The index position can be set using
1125.Fn asl_reset_iteration .
1126.Pp
1127.Fo asl_reset_iteration
1128.Fa obj
1129.Fa position
1130.Fc
1131sets the current position index used be
1132.Fn asl_next
1133and
1134.Fn asl_prev .
1135The value of
1136.Fa position
1137may be zero to set the position index for
1138.Fa obj
1139at the beginning of its contents,
1140or it may be SIZE_MAX to set the position index for
1141.Fa obj
1142at the end of its contents.
1143For objects of type ASL_TYPE_LIST, the position index is an actual index into the list.
1144For other message containing objects, the index is an ID number which may not be sequential.
1145.Pp
1146.Fo asl_get_index
1147.Fa list
1148.Fa index
1149.Fc
1150returns the object at position
1151.Fa index
1152in the target
1153.Fa list
1154object, which must be of type ASL_TYPE_LIST.
1155Returns NULL if the index is out of range or if
1156.Fa list
1157is not a list type.
1158.Pp
1159.Fo asl_remove_index
1160.Fa list
1161.Fa index
1162.Fc
1163removes the object at position
1164.Fa index
1165from the target
1166.Fa list
1167object, which must be of type ASL_TYPE_LIST.
81582353
A
1168.Pp
1169.Fo asl_log_descriptor
1170.Fa asl
1171.Fa msg
1172.Fa level
1173.Fa descriptor
1174.Fa fd_type
1175.Fc
1176provides functionality to use file descriptors to send logging data to ASL.
1177.Ar asl
1178is retained by ASL and must still be closed by the caller by calling
1179.Fn asl_close
1180if the caller loses reference to it.
1181.Ar msg
f3df4c03
A
1182is copied by ASL and similarly must still be releaser by the caller by calling
1183.Fn asl_release
81582353
A
1184if the caller loses reference to it. Any changes made to it after calling
1185.Fn asl_log_descriptor()
1186are not applicable to the message used.
1187.Ar descriptor is treated differently based on the value of
1188.Ar fd_type .
1189.Pp
1190If
1191.Ar fd_type
1192is ASL_LOG_DESCRIPTOR_READ, the descriptor must be open for read access. ASL
1193uses
1194.Xr dispatch 2
1195to read from the descriptor as data becomes available. These data are line
1196buffered and passed to
1197.Fn asl_log .
1198When EOF is read, ASL will
1199.Xr close 2
1200.Ar descriptor ..
1201.Pp
1202If
1203.Ar fd_type
1204is ASL_LOG_DESCRIPTOR_WRITE, the descriptor is closed and a new writable
1205descriptor is created with the same fileno. Any data written to this new
1206descriptor are line buffered and passed to
1207.Fn asl_log .
1208When EOF is sent, no further data are read. The caller is responsible for
1209closing the new descriptor. One common use for this API is to redirect writes
1210to stdout or stderr to ASL by passing STDOUT_FILENO or STDERR_FILENO as
1211.Ar descriptor .
1212.Pp
1213.Fo asl_search
f3df4c03
A
1214.Fa obj
1215.Fa query
81582353 1216.Fc
f3df4c03
A
1217searches messages in the
1218.Fa obj
1219object for messages that match the keys and values in
1220.Fa query ,
81582353 1221subject to matching operations associated with those keys and values.
f3df4c03
A
1222The return returns an object of type ASL_TYPE_LIST containing matching messages,
1223or NULL if no matches are found.
81582353 1224The
f3df4c03 1225.Ar query
81582353
A
1226argument should be constructed using
1227.Fn asl_set_query .
1228See the
1229.Sx SEARCHING
1230section for details on constructing queries.
81582353 1231.Pp
f3df4c03
A
1232The
1233.Fa obj
1234parameter may be any ASL object.
1235For type ASL_TYPE_CLIENT, the main ASL system database is searched.
1236If the object type is ASL_TYPE_STORE or ASL_TYPE_FILE,
1237then the corresponding data store or data file is searched.
1238For ASL_TYPE_LIST, matches are found in a message list.
1239If
1240.Fa obj
1241is of type ASL_TYPE_MSG and query is of type ASL_TYPE_QUERY,
1242.Fa obj
1243is matched against the query,
1244and a list containing
1245.Fa obj
1246is returned if the match succeeds.
1247If both
1248.Fa obj
1249and
1250.Fa query
1251are objects of type ASL_TYPE_MSG or both are of type ASL_TYPE_QUERY,
1252they are tested for exact match.
1253A list containing
1254.Fa obj
1255is returned if the match is exact.
1256If
1257.Fa obj
1258is of type ASL_TYPE_QUERY and
1259.Fa query
1260is of type ASL_TYPE_MSG, the routine returns NULL.
1261.Pp
1262.Fo asl_match
1263.Fa obj
1264.Fa querylist
1265.Fa last
1266.Fa start
1267.Fa count
1268.Fa duration
1269.Fa direction
1270.Fc
1271is similar to
1272.Fn asl_search ,
1273but allows more advanced searching of ASL objects.
1274The
1275.Fa obj
1276parameter may be of any type, as with
1277.Fn asl_search .
1278The
1279.Fa querylist
1280parameter must be an object of type ASL_TYPE_LIST,
1281containing zero or more objects of type ASL_TYPE_QUERY.
1282A NULL
1283.Fa querylist
1284or a list containing zero objects matches all messages in the target
1285.Fa obj.
1286.Pp
1287The caller may provide a starting ASL message ID, a direction, and a count.
1288A
1289.Fa start
1290ID value of 0 means that matching should commence at the beginning of the target
1291.Fa obj .
1292A value of SIZE_MAX indicates that matching should commence at the end (most recent message)
1293in the target.
1294If a non-zero
1295.Fa count
1296value is supplied, the routine will return when it has found that many messages,
1297or it has checked all messages.
1298If a non-zero
1299.Fa duration
1300is supplied, the routine will return after the specified time (in microseconds).
1301If both
1302.Fa count
1303and
1304.Fa duration
1305are non-zero, the routine will return when the desired number of items has been matched
1306or when the specified duration has been exceeded, whichever occurs first.
1307The search direction may be ASL_MATCH_DIRECTION_FORWARD or ASL_MATCH_DIRECTION_REVERSE.
1308The routine sets the value of the out parameter
1309.Fa last
1310to be an index of the last message checked while matching.
1311To fetch matching messages in batches (using a small count or duration value), the
1312.Fa start
1313value for each iteration should be set to
1314.Fa last
1315+ 1 if searching forward, or
1316.Fa last
1317- 1 for reverse search.
1318.Pp
1319.Em DEPRECATED
81582353
A
1320.Fo aslresponse_next
1321.Fa r
1322.Fc
f3df4c03
A
1323This interface is deprecated in favor of
1324.Fn asl_next .
1325It is implemented as a call to
1326.Fn asl_next .
81582353 1327.Pp
f3df4c03 1328.Em DEPRECATED
81582353
A
1329.Fo aslresponse_free
1330.Fa r
1331.Fc
f3df4c03
A
1332This interface is deprecated in favor of
1333.Fn asl_release .
1334It is implemented as a call to
1335.Fn asl_release .
81582353
A
1336.Pp
1337.Fo asl_create_auxiliary_file
1338.Fa msg
1339.Fa title
1340.Fa uti
1341.Fa out_descriptor
1342.Fc
1343Creates an auxiliary file that may be used by the client to save arbitrary data.
1344When the file is closed using
1345.Fo asl_close_auxiliary_file
1346.Fc ,
1347.Nm syslogd
1348will log the specified
1349.Fa msg
1350along with the
1351.Fa title
1352and the Uniform Type Identifier provided by
1353.Fa uti .
1354If a NULL value is supplied for
1355.Fa uti
1356the type
1357.Dq public.data
1358will be used.
1359The
1360.Nm Console
1361application will display the message with a link to the file.
1362.Pp
1363Auxiliary files are saved in the ASL data store.
1364They are automatically deleted at the same time that the log message expires.
1365Messages expire in 7 days by default.
1366A value set for the ASLExpireTime key will override the default.
1367Read access for the auxiliary file will be the same as read access for
1368.Fa msg .
1369By default, messages (and auxiliary files) are world-readable.
1370Access may be limited by setting values for the ReadUID and ReadGID keys.
1371.Pp
1372.Fo asl_close_auxiliary_file
1373.Fa descriptor
1374.Fc
1375closes the file descriptor
1376.Ar descriptor
1377previously returned by a call to
1378.Fn asl_create_auxiliary_file .
1379.Pp
1380.Fo asl_log_auxiliary_location
1381.Fa msg
1382.Fa title
1383.Fa uti
1384.Fa url
1385.Fc
1386will log the specified
1387.Fa msg
1388along with the
1389.Fa title ,
1390the Uniform Type Identifier provided by
1391.Fa uti ,
1392and the Uniform Resource Locator provided by
1393.Fa url .
1394The
1395.Nm Console
1396application will display the message with a link to the file.
1397This allows a client to save data in an auxiliary file, but unlike
f3df4c03 1398.Fn asl_create_auxiliary_file ,
81582353
A
1399the life-cycle of this file must be managed by some external system.
1400The file will not be removed when the corresponding log message expired from the ASL data store.
1401.Pp
1402.Fo asl_open_from_file
1403.Fa descriptor
1404.Fa facility
1405.Fa opts
1406.Fc
1407creates a client handle for an open file descriptor
1408.Fa descriptor .
1409This routine may be used in conjunction with
f3df4c03 1410.Fn asl_create_auxiliary_file
81582353 1411or
f3df4c03 1412.Fn asl_log_auxiliary_location
81582353
A
1413to save ASL format log messages in an auxiliary file.
1414The UTI type
1415.Dq com.apple.asl-file
1416should be used for ASL format auxiliary files.
1417.Pp
1418Files with this format may be read from the command line using
1419.Nm syslog Fl f Ar file ,
1420or from the
1421.Nm Console
1422utility.
1423.Pp
1424The file must be open for read and write access.
1425The file will be truncated and its existing contents will be lost.
1426.Fo asl_close
1427.Fc
1428must be called to close the client handle when logging to this file is complete.
1429The file should be closed using
1430.Fo asl_close_auxiliary_file
1431.Fc
1432if it was returned by
1433.Fo asl_create_auxiliary_file
1434.Fc ,
1435or
1436.Fo close
1437.Fc
1438otherwise.
1439.Pp
1440The client handle returned by
1441.Fn asl_open_from_file
1442contains an internal lock, and may be used safely by multiple threads or from independent dispatch queues.
1443Note that callers will contend for the internal lock when saving log messages to a file.
1444.Pp
1445Note that messages with ReadUID or ReadGID values will simply be saved to the file,
1446and will not effect read access to either the message or the file itself.
1447Similarly, messages with ASLExpireTime values will be saved, but will not effect the
1448life-cycle of either the individual messages or the file.
f3df4c03
A
1449.Pp
1450.Fo asl_encode_buffer
1451.Fa in
1452.Fa len
1453.Fc
1454is a utility routine for encoding arbitrary data buffers.
1455ASL message dictionary keys and values are nul-terminated C strings.
1456If an application wishes to include arbitrary data which may contain zero bytes,
1457the data buffer must first be encoded in a manner that eliminates any embedded zeros.
1458The
1459.Fn asl_encode_buffer
1460routine will encode an arbitrary data buffer at the address
1461.Fa in
1462containing
1463.Fa len
1464bytes (octets) of data.
1465The output of the routine is a nul-terminated C string.
1466The encoded string may be decoded using the companion
1467.Fn asl_decode_buffer
1468routine.
1469.Pp
1470This utility is used by the ASL server
1471.Nm syslogd
1472to encode the value associated with ASL_KEY_AUX_DATA in an ASL_TYPE_MSG object.
1473An ASL_KEY_AUX_DATA key/value pair is used to hold the data written to a file descriptor
1474created by
1475.Fn asl_create_auxiliary_file
1476on iOS systems, where the ASL database is stored in memory.
1477.Pp
1478.Fo asl_decode_buffer
1479.Fa in
1480.Fa buf
1481.Fa len
1482.Fc
1483decodes a C string previously created by
1484.Fn asl_encode_buffer
1485back into a buffer, possibly containing embedded zero bytes (octets).
1486The routine allocates memory for the buffer and returns a pointer in an output
1487.Fa buf
1488parameter.
1489The caller is responsible for freeing the buffer.
1490.Pp
1491This routine should be used to decode the value associated with an
1492ASL_KEY_AUX_DATA key in an ASL_TYPE_MSG object.
1493.Pp
81582353 1494.Ss MESSAGES
f3df4c03 1495At the core of this API is the asl_object_t structure.
81582353
A
1496Although the structure is opaque and may not be directly manipulated,
1497it contains a list of key/value pairs.
1498All keys and values are NUL-character terminated C language strings.
1499UTF-8 encoding may be used for non-ASCII characters.
1500.Pp
1501Message structures are generally used to send log messages,
1502and are created thusly:
1503.Pp
f3df4c03 1504 asl_object_t m = asl_new(ASL_TYPE_MSG);
81582353
A
1505.Pp
1506Another message type, ASL_TYPE_QUERY,
1507is used to create queries when searching the data store.
1508Query type messages and searching are described in detail in the
1509.Sx SEARCHING
1510section.
1511For the remainder of this section,
1512the messages described will be of the ASL_TYPE_MSG variety.
1513.Pp
f3df4c03 1514Each asl_object_t contains a default set of keys
81582353
A
1515and values that are associated with them.
1516These keys are listed in the asl.h header file.
1517They are:
1518.Pp
1519 #define ASL_KEY_TIME "Time"
1520 #define ASL_KEY_HOST "Host"
1521 #define ASL_KEY_SENDER "Sender"
1522 #define ASL_KEY_FACILITY "Facility"
1523 #define ASL_KEY_PID "PID"
1524 #define ASL_KEY_UID "UID"
1525 #define ASL_KEY_GID "GID"
1526 #define ASL_KEY_LEVEL "Level"
1527 #define ASL_KEY_MSG "Message"
1528.Pp
1529Many of these correspond to equivalent parts of messages described in the
1530.Xr syslog 3
1531API.
1532Values associated with these message keys are assigned appropriate defaults.
1533The value for ASL_KEY_HOST is the local host name,
1534the value associated with ASL_KEY_SENDER is the process name,
1535the ASL_KEY_PID is the client's process ID number, and so on.
1536.Pp
1537Note the addition of the UID and GID keys.
1538The values for UID and GID are set in library code by the message sender.
1539The server will attempt to confirm the values,
1540but no claim is made that these values cannot be maliciously overridden
1541in an attempt to deceive a log message reader
1542as to the identity of the sender of a message.
1543The contents of log messages must be regarded as insecure.
1544.Pp
1545The
1546.Xr asl 3
1547API does not require a process to choose a facility name.
1548The
1549.Nm syslogd
1550server will use a default value of
1551.Dq user
1552if a facility is not set.
1553However, a client may set a facility name as an argument in the
f3df4c03 1554.Fn asl_open
81582353
A
1555call, or by setting a specific value for the ASL_KEY_FACILITY in a message:
1556.Pp
1557 asl_set(m, ASL_KEY_FACILITY, "com.somename.greatservice");
1558.Pp
1559An application may choose any facility name at will.
1560Different facility names may be attached to different messages, perhaps to distinguish different subsystems in log messages.
1561Developers are encouraged to adopt a
1562.Dq Reverse ICANN
1563naming convention to avoid conflicting facility names.
1564.Pp
1565Default values are set in the message for each of the keys listed above,
1566except for ASL_KEY_MSG,
1567which may be explicitly set at any time using the
f3df4c03 1568.Fn asl_set
81582353 1569routine, or implicitly set at the time the message is sent using the
f3df4c03
A
1570.Fn asl_log_message ,
1571.Fn asl_log ,
81582353 1572or
f3df4c03 1573.Fn asl_vlog
81582353 1574routines.
f3df4c03 1575These three routines also have an integer-level parameter
81582353
A
1576for specifying the log priority.
1577The ASL_KEY_LEVEL value is set accordingly.
1578Finally, the value associated with ASL_KEY_TIME
1579is set in the sending routine.
1580.Pp
81582353
A
1581When logging from multiple threads,
1582each thread
1583.Em should
1584open a separate client handle using
f3df4c03 1585.Fn asl_open .
81582353 1586The client handle may then be closed when it is no longer required using
f3df4c03
A
1587.Fn asl_release .
1588Multiple threads may log messages safely using a NULL asl_object_t argument,
81582353
A
1589but the library will use an internal lock, so that in fact only one thread
1590will log at a time.
1591.Pp
1592When an application requires additional keys and values
1593to be associated with each log message,
1594a single message structure may be allocated and set up as
1595.Dq template
1596message of sorts:
1597.Pp
f3df4c03 1598 asl_object_t m = asl_new(ASL_TYPE_MSG);
81582353
A
1599 asl_set(m, ASL_KEY_FACILITY, "com.secrets.r.us");
1600 asl_set(m, "Clearance", "Top Secret");
1601 ...
1602 asl_log(NULL, m, ASL_LEVEL_NOTICE, "Message One");
1603 ...
1604 asl_log(NULL, m, ASL_LEVEL_ERR, "Message Two");
1605.Pp
1606The message structure will carry the values set for the
1607.Dq Facility
1608and
1609.Dq Clearance
1610keys so that they are used in each call to
f3df4c03 1611.Fn asl_log ,
81582353
A
1612while the log level and the message text
1613are taken from the calling parameters.
1614.Pp
1615The
1616.Ar format
1617argument to
f3df4c03 1618.Fn asl_log
81582353 1619and
f3df4c03 1620.Fn asl_vlog
81582353
A
1621is identical to
1622.Xr printf 3 ,
1623and may include
1624.Ql %m ,
1625which is replaced by the current error message
1626(as denoted by the global variable
1627.Va errno ;
1628see
1629.Xr strerror 3 . )
1630.Pp
1631Key/value pairs may be removed from a message structure with
f3df4c03 1632.Fn asl_unset .
81582353 1633A message may be freed using
f3df4c03 1634.Fn asl_release .
81582353
A
1635.Pp
1636The
f3df4c03 1637.Fn asl_send
81582353 1638routine is used by
f3df4c03 1639.Fn asl_log
81582353 1640and
f3df4c03 1641.Fn asl_vlog
81582353
A
1642to transmit a message to the server.
1643This routine sets the value associated with ASL_KEY_TIME
1644and sends the message.
1645It may be called directly if all of a message's key/value pairs
1646have been created using
f3df4c03 1647.Fn asl_set .
81582353
A
1648.Ss SECURITY
1649Messages that are sent to the
1650.Nm syslogd
1651server may be saved in a message store.
1652The store may be searched using
f3df4c03 1653.Fn asl_search ,
81582353
A
1654as described below.
1655By default, all messages are readable by any user.
1656However, some applications may wish to restrict read access
1657for some messages.
1658To accommodate this,
1659a client may set a value for the "ReadUID" and "ReadGID" keys.
1660These keys may be associated with a value
1661containing an ASCII representation of a numeric UID or GID.
1662Only the root user (UID 0),
1663the user with the given UID,
1664or a member of the group with the given GID
1665may fetch access-controlled messages from the database.
1666.Pp
1667Although the ASL system does not require a "Facility" key in a message,
1668many processes specify a "Facility" value similar
1669to the common usage of the BSD
1670.Nm syslog
1671API, although developers are encouraged to adopt facility names that make sense for their application.
1672A
1673.Dq Reverse ICANN
1674naming convention (e.g. "com.apple.system.syslog") should be adopted to avoid conflicting names.
1675The ASL system generally allows any string to be used as a facility value,
1676with one exception.
1677The value "com.apple.system",
1678or any string that has "com.apple.system" as a prefix,
1679may only be used by processes running with the UID 0.
1680This allows system processes to log messages that can not be "spoofed" by user processes.
1681Non-UID 0 client processes that specify "com.apple.system" as a facility, will be assigned the value "user"
1682by the
1683.Nm syslogd
1684server.
1685.Ss CLIENT HANDLES
1686A client handle contains various parameters and control settings that are used when a message is logged.
1687This includes an identification string, a facility name, filtering controls, additional file descriptors, and other data.
1688Client handles are not thread-safe.
1689Applications that log from multiple threads should create a client handle for each thread.
1690.Pp
1691Applications that use libdispatch must also avoid using a single client handle from multiple dispatch queues if those queues may run concurrently.
1692A good approach is to create one or more serial dispatch queues specifically for logging.
1693Each such queue should use its own ASL client handle.
1694.Pp
1695If a single handle must be accessed by multiple dispatch queues,
1696then the application must use locks, semaphores, or some other mechanism to prevent concurrent access to a client handle.
1697.Pp
1698A NULL value may be used in any of the routines
f3df4c03 1699that require an asl_object_t argument.
81582353
A
1700In this case, the library will use an internal client handle.
1701This internal handle contains its own lock, allowing multiple threads to safely use the NULL client handle.
1702Note, however, that contention for the lock may cause undesirable synchronization behavior or reduced performance.
1703.Pp
1704The
f3df4c03 1705.Fn asl_open
81582353
A
1706routine may be given an ident argument,
1707which becomes the default value for the ASL_KEY_SENDER key,
1708and a facility argument,
1709which becomes the value associated with the ASL_KEY_FACILITY key.
1710If NULL is passed as the value for
1711.Ar ident ,
1712the name of the currently running program will be used.
1713If NULL is passed as the value for
1714.Ar facility ,
1715the value
1716.Dq user
1717will be used for non UID 0 processes, and
1718.Dq
1719daemon
1720will be used for UID 0 processes.
1721.Pp
1722Several options are available when creating a client handle.
1723They are:
1724.Pp
1725.Bl -tag -width "ASL_OPT_NO_REMOTE" -compact
1726.It ASL_OPT_STDERR
1727adds stderr as an output file descriptor
1728.It ASL_OPT_NO_DELAY
1729connects to the server immediately
1730.It ASL_OPT_NO_REMOTE
1731disables remote-control filter adjustment
1732.El
1733.Pp
81582353
A
1734See the FILTERING section below, and the
1735.Xr syslog 1
1736for additional details on filter controls.
1737.Pp
1738A client handle is closed and its resources released using
f3df4c03 1739.Fn asl_close .
81582353
A
1740Note that if additional file descriptors were added to the handle,
1741either using the ASL_OPT_STDERR option
1742or afterwards with the
f3df4c03 1743.Fn asl_add_log_file
81582353 1744routine, those file descriptors are not closed by
f3df4c03 1745.Fn asl_close .
81582353
A
1746.Ss LOGGING TO ADDITIONAL FILES
1747If a client handle is opened with the ASL_OPT_STDERR option to
f3df4c03 1748.Fn asl_open ,
81582353
A
1749a copy of each log message will be sent to stderr.
1750Additional output streams may be include using
f3df4c03 1751.Fn asl_add_log_file .
81582353
A
1752.Pp
1753Messages sent to stderr or other files are printed in the "standard" message format
1754also used as a default format by the
1755.Xr syslog 1
1756command line utility.
1757Non-ASCII characters in a message are encoded using the
1758.Dq safe
1759encoding style used by
1760.Xr syslog 1
1761with the
1762.Fl E Ar safe
1763option.
1764Backspace characters are printed as ^H.
1765Carriage returns are mapped to newlines.
1766A tab character is appended after newlines so that message text is indented.
1767.Pp
1768File descriptors may be removed from the list of outputs associated
1769with a client handle with
f3df4c03 1770.Fn asl_remove_log_file .
81582353
A
1771This routine simply removes the file descriptor from the output list.
1772The file is not closed as a result.
1773.Pp
1774The ASL_OPT_STDERR option may not be unset
1775after a client handle has been opened.
1776.Ss SEARCHING
1777The
1778.Nm syslogd
1779server archives received messages in a data store
1780that may be searched using the
f3df4c03
A
1781.Fn asl_search ,
1782.Fn asl_next ,
81582353 1783and
f3df4c03 1784.Fn asl_release
81582353
A
1785routines.
1786A query message is created using:
1787.Pp
f3df4c03 1788 asl_object_t q = asl_new(ASL_TYPE_QUERY);
81582353
A
1789.Pp
1790Search settings are made in the query using
f3df4c03 1791.Fn asl_set_query .
81582353 1792A search is performed on the data store with
f3df4c03
A
1793.Fn asl_search .
1794It returns an object of type ASL_TYPE_LIST.
1795The caller may use routines that operate on lists, such as
1796.Fn asl_next ,
1797.Fn asl_prev ,
1798and
1799.Fn asl_get_index
1800to access the matching messages.
81582353
A
1801.Pp
1802Like other messages, ASL_TYPE_QUERY messages contain keys and values.
1803They also associate an operation with each key and value.
1804The operation is used to decide if a message matches the query.
1805The simplest operation is ASL_QUERY_OP_EQUAL, which tests for equality.
1806For example, the following code snippet searches for messages
1807with a Sender value equal to
1808.Dq MyApp .
1809.Pp
f3df4c03 1810 asl_object_t q, r;
81582353
A
1811 q = asl_new(ASL_TYPE_QUERY);
1812 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
1813 r = asl_search(NULL, q);
1814.Pp
1815More complex searches may be performed using other query operations.
1816.Pp
1817.Bl -tag -width "ASL_QUERY_OP_GREATER_EQUAL" -compact
1818.It ASL_QUERY_OP_EQUAL
1819value equality
1820.It ASL_QUERY_OP_GREATER
1821value greater than
1822.It ASL_QUERY_OP_GREATER_EQUAL
1823value greater than or equal to
1824.It ASL_QUERY_OP_LESS
1825value less than
1826.It ASL_QUERY_OP_LESS_EQUAL
1827value less than or equal to
1828.It ASL_QUERY_OP_NOT_EQUAL
1829value not equal
1830.It ASL_QUERY_OP_REGEX
1831regular expression search
1832.It ASL_QUERY_OP_TRUE
1833always true - use to test for the existence of a key
1834.El
1835.Pp
1836Regular expression search uses
1837.Xr regex 3
1838library.
1839Patterns are compiled using the REG_EXTENDED and REG_NOSUB options.
1840.Pp
1841Modifiers that change the behavior of these operations
1842may also be specified by ORing the modifier value with the operation.
1843The modifiers are:
1844.Pp
1845.Bl -tag -width "ASL_QUERY_OP_SUBSTRING" -compact
1846.It ASL_QUERY_OP_CASEFOLD
1847string comparisons are case-folded
1848.It ASL_QUERY_OP_PREFIX
1849match a leading substring
1850.It ASL_QUERY_OP_SUFFIX
1851match a trailing substring
1852.It ASL_QUERY_OP_SUBSTRING
1853match any substring
1854.It ASL_QUERY_OP_NUMERIC
1855values are converted to integer using
1856.Nm atoi
1857.El
1858.Pp
1859The only modifier that is checked
1860for ASL_QUERY_OP_REGEX search is ASL_QUERY_OP_CASEFOLD.
1861This causes the regular expression to be compiled
1862with the REG_ICASE option.
1863.Pp
1864If a query message contains more than one set of key/value/operation triples,
1865the result will be a logical AND. For example, to find messages from
1866.Dq MyApp
1867with a priority level less than or equal to
1868.Dq 3 :
1869.Pp
f3df4c03 1870 asl_object_t q, r;
81582353
A
1871 q = asl_new(ASL_TYPE_QUERY);
1872 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
1873 asl_set_query(q, ASL_KEY_LEVEL, "3",
1874 ASL_QUERY_OP_LESS_EQUAL | ASL_QUERY_OP_NUMERIC);
1875 r = asl_search(NULL, q);
1876.Pp
1877After calling
f3df4c03
A
1878.Fn asl_search
1879to get a list of matching messages, one can use
1880.Fn asl_next
1881to iterate through the list, and
1882.Fn asl_fetch_key_val_op
1883To iterate through the message dictionary.
81582353 1884.Pp
f3df4c03 1885 asl_object_t q, r;
81582353
A
1886.Pp
1887 ...
1888 r = asl_search(NULL, q);
f3df4c03 1889 while (NULL != (m = asl_next(r)))
81582353 1890 {
f3df4c03
A
1891 int i, n;
1892 n = asl_count(m);
1893 for (i = 0; i < n; i++)
81582353 1894 {
f3df4c03
A
1895 const char *key, *val;
1896 asl_fetch_key_val_op(m, i, key, val, NULL);
81582353
A
1897 ...
1898 }
1899 }
f3df4c03 1900 asl_release(r);
81582353
A
1901.Pp
1902.Ss FILTERING AND REMOTE CONTROL
1903Clients may set a filter mask value with
f3df4c03 1904.Fn asl_set_filter .
81582353
A
1905The mask specifies which messages should be sent to the
1906.Nm syslogd
1907daemon by specifying a yes/no setting for each priority level.
1908Clients typically set a filter mask
1909to avoid sending relatively unimportant messages.
1910For example, Debug or Info priority level messages
1911are generally only useful for debugging operations.
1912By setting a filter mask, a process can improve performance
1913by avoiding sending messages that are in most cases unnecessary.
1914.Pp
f3df4c03 1915.Fn asl_set_filter returns the previous value of the filter, i.e. the value of the filter before the routine was called.
81582353
A
1916.Pp
1917As a convenience, the macros ASL_FILTER_MASK(level) and ASL_FILTER_MASK_UPTO(level)
1918may be used to construct a bit mask corresponding to a given priority level,
1919or corresponding to a bit mask for all priority levels
1920from ASL_LEVEL_EMERG to a given input level.
1921.Pp
1922The default filter mask is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
1923This means that by default,
1924and in the absence of remote-control changes (described below),
1925ASL_LEVEL_DEBUG and ASL_LEVEL_INFO priority level messages
1926are not sent to the
f3df4c03 1927.Nm syslogd
81582353
A
1928server.
1929.Pp
1930Three different filters exist for each application.
1931The first is the filter mask set using
f3df4c03 1932.Fn asl_set_filter
81582353
A
1933as described above.
1934The Apple System Log facility also manages a
1935.Dq master
1936filter mask.
1937The master filter mask usually has a value
1938that indicates to the library that it is
1939.Dq off ,
1940and thus it has no effect.
1941However, the mask filter mask may be enabled
1942by giving it a value using the
1943.Nm syslog
1944command, using the
1945.Fl c
19460 option.
1947When the master filter mask has been set,
1948it takes precedence over the client's filter mask.
1949The client's mask is unmodified,
1950and will become active again if remote-control filtering is disabled.
1951.Pp
1952In addition to the master filter mask,
1953The Apple System Log facility
1954also manages a per-client remote-control filter mask.
1955Like the master filter mask, the per-client mask is usually
1956.Dq off ,
1957having no effect on a client.
1958If a per-client filter mask is set using the
1959.Nm syslog
1960command, using the
1961.Fl c Ar process
1962option, then it takes precedence
1963over both the client's filter mask and the master filter mask.
1964As is the case with the master filter mask,
1965a per-client mask ceases having any effect when if is disabled.
1966.Pp
1967The ASL_OPT_NO_REMOTE option to
f3df4c03 1968.Fn asl_open
81582353
A
1969causes both the master and per-client remote-control masks
1970to be ignored in the library.
1971In that case, only the client's own filter mask
1972is used to determine which messages are sent to the server.
1973This may be useful for Applications that produce log messages
1974that should never be filtered, due to security considerations.
1975Note that root (administrator) access is required
1976to set or change the master filter mask,
1977and that only root may change a per-client remote-control filter mask
1978for a root (UID 0) process.
1979.Pp
1980The per-process remote control filter value is kept as a state value
1981associated with a key managed by
1982.Nm notifyd .
1983The key is protected by an access control mechanism that only permits the
1984filter value to be accessed and modified by the same effective UID as the
1985ASL client at the time that the first ASL connection was created.
1986Remote filter control using
1987.Nm syslog Fl c
1988will fail for processes that change effective UID after starting an ASL connection.
1989Those processes should close all ASL client handles and then re-open ASL connections
1990if remote filter control support is desired.
1991.Sh HISTORY
1992These functions first appeared in
1993Mac OS X 10.4.
1994.Sh SEE ALSO
1995.Xr syslog 1 ,
1996.Xr strvis 3 ,
1997.Xr syslogd 8