]>
git.saurik.com Git - apple/libc.git/blob - include/asl.h
2 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
29 #include <sys/cdefs.h>
30 #include <Availability.h>
32 typedef struct __aslclient
*aslclient
;
33 typedef struct __aslmsg
*aslmsg
;
34 typedef struct __aslresponse
*aslresponse
;
37 * These routines provide an interface to the Apple System Log facility.
38 * The API allows client applications to create flexible, structured messages
39 * and send them to the syslogd server. Messages received by the server are
40 * saved in a data store, subject to input filtering constraints.
41 * This API also permits clients to create queries and search the message
42 * data store for matching messages.
48 * These are added to allow headerdoc2html to process
49 * the prototypes of asl_log and asl_vlog correctly.
50 * The "-p" option to headerdoc2html is required.
52 #ifndef __DARWIN_LDBL_COMPAT2
54 #define __DARWIN_LDBL_COMPAT2(a)
58 #define __printflike(a,b)
61 /*! @defineblock Log Message Priority Levels
62 * Log levels of the message.
64 #define ASL_LEVEL_EMERG 0
65 #define ASL_LEVEL_ALERT 1
66 #define ASL_LEVEL_CRIT 2
67 #define ASL_LEVEL_ERR 3
68 #define ASL_LEVEL_WARNING 4
69 #define ASL_LEVEL_NOTICE 5
70 #define ASL_LEVEL_INFO 6
71 #define ASL_LEVEL_DEBUG 7
74 /*! @defineblock Log Message Priority Level Strings
75 * Strings corresponding to log levels.
77 #define ASL_STRING_EMERG "Emergency"
78 #define ASL_STRING_ALERT "Alert"
79 #define ASL_STRING_CRIT "Critical"
80 #define ASL_STRING_ERR "Error"
81 #define ASL_STRING_WARNING "Warning"
82 #define ASL_STRING_NOTICE "Notice"
83 #define ASL_STRING_INFO "Info"
84 #define ASL_STRING_DEBUG "Debug"
87 /*! @defineblock Attribute Matching
88 * Attribute value comparison operations.
90 #define ASL_QUERY_OP_CASEFOLD 0x0010
91 #define ASL_QUERY_OP_PREFIX 0x0020
92 #define ASL_QUERY_OP_SUFFIX 0x0040
93 #define ASL_QUERY_OP_SUBSTRING 0x0060
94 #define ASL_QUERY_OP_NUMERIC 0x0080
95 #define ASL_QUERY_OP_REGEX 0x0100
97 #define ASL_QUERY_OP_EQUAL 0x0001
98 #define ASL_QUERY_OP_GREATER 0x0002
99 #define ASL_QUERY_OP_GREATER_EQUAL 0x0003
100 #define ASL_QUERY_OP_LESS 0x0004
101 #define ASL_QUERY_OP_LESS_EQUAL 0x0005
102 #define ASL_QUERY_OP_NOT_EQUAL 0x0006
103 #define ASL_QUERY_OP_TRUE 0x0007
106 /*! @defineblock Message Attributes
108 * These attributes are known by ASL, and are generally
109 * associated with all log messages.
110 * Additional attributes may be added as desired.
112 #define ASL_KEY_TIME "Time" /* Timestamp. Set automatically */
113 #define ASL_KEY_TIME_NSEC "TimeNanoSec" /* Nanosecond time. */
114 #define ASL_KEY_HOST "Host" /* Sender's address (set by the server). */
115 #define ASL_KEY_SENDER "Sender" /* Sender's identification string. Default is process name. */
116 #define ASL_KEY_FACILITY "Facility" /* Sender's facility. Default is "user". */
117 #define ASL_KEY_PID "PID" /* Sending process ID encoded as a string. Set automatically. */
118 #define ASL_KEY_UID "UID" /* UID that sent the log message (set by the server). */
119 #define ASL_KEY_GID "GID" /* GID that sent the log message (set by the server). */
120 #define ASL_KEY_LEVEL "Level" /* Log level number encoded as a string. See levels above. */
121 #define ASL_KEY_MSG "Message" /* Message text. */
122 #define ASL_KEY_READ_UID "ReadUID" /* User read access (-1 is any user). */
123 #define ASL_KEY_READ_GID "ReadGID" /* Group read access (-1 is any group). */
124 #define ASL_KEY_EXPIRE_TIME "ASLExpireTime" /* Expiration time for messages with long TTL. */
125 #define ASL_KEY_MSG_ID "ASLMessageID" /* 64-bit message ID number (set by the server). */
126 #define ASL_KEY_SESSION "Session" /* Session (set by the launchd). */
127 #define ASL_KEY_REF_PID "RefPID" /* Reference PID for messages proxied by launchd */
128 #define ASL_KEY_REF_PROC "RefProc" /* Reference process for messages proxied by launchd */
129 #define ASL_KEY_AUX_TITLE "ASLAuxTitle" /* Auxiliary title string */
130 #define ASL_KEY_AUX_UTI "ASLAuxUTI" /* Auxiliary Uniform Type ID */
131 #define ASL_KEY_AUX_URL "ASLAuxURL" /* Auxiliary Uniform Resource Locator */
132 #define ASL_KEY_AUX_DATA "ASLAuxData" /* Auxiliary in-line data */
133 #define ASL_KEY_OPTION "ASLOption" /* Internal */
134 #define ASL_KEY_SENDER_INSTANCE "SenderInstance" /* Sender instance UUID. */
137 /*! @defineblock aslmsg Types
138 * Message type argument passed to asl_new().
140 #define ASL_TYPE_MSG 0
141 #define ASL_TYPE_QUERY 1
144 /*! @defineblock Filter Masks
145 * Used in client-side filtering, which determines which
146 * messages are sent by the client to the syslogd server.
148 #define ASL_FILTER_MASK_EMERG 0x01
149 #define ASL_FILTER_MASK_ALERT 0x02
150 #define ASL_FILTER_MASK_CRIT 0x04
151 #define ASL_FILTER_MASK_ERR 0x08
152 #define ASL_FILTER_MASK_WARNING 0x10
153 #define ASL_FILTER_MASK_NOTICE 0x20
154 #define ASL_FILTER_MASK_INFO 0x40
155 #define ASL_FILTER_MASK_DEBUG 0x80
158 /*! @defineblock Filter Mask Macros
159 * Macros to create bitmasks for filter settings - see asl_set_filter().
161 #define ASL_FILTER_MASK(level) (1 << (level))
162 #define ASL_FILTER_MASK_UPTO(level) ((1 << ((level) + 1)) - 1)
165 /*! @defineblock Client Creation Options
166 * Options for asl_open().
168 #define ASL_OPT_STDERR 0x00000001
169 #define ASL_OPT_NO_DELAY 0x00000002
170 #define ASL_OPT_NO_REMOTE 0x00000004
176 * Initialize a connection to the ASL server.
178 * This call is optional in most cases. The library will perform any
179 * necessary initializations on the fly. A call to asl_open() is required
180 * if optional settings must be made before messages are sent to the server.
181 * These include setting the client filter and managing additional output
182 * file descriptors. Note that the default setting of the client filter is
183 * ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE), so ASL_LEVEL_DEBUG and ASL_LEVEL_INFO
184 * messages are not sent to the server by default.
186 * Options (defined above) may be set using the opts parameter. They are:
188 * ASL_OPT_STDERR - adds stderr as an output file descriptor
190 * ASL_OPT_NO_DELAY - connects to the server immediately
192 * ASL_OPT_NO_REMOTE - disables the remote-control mechanism for adjusting
193 * filter levers for processes using e.g. syslog -c ...
196 * (input) Sender name
198 * (input) Facility name
200 * (input) Options (see asl_open Options)
201 * @result Returns an ASL client handle
203 aslclient
asl_open(const char *ident
, const char *facility
, uint32_t opts
);
206 * Shuts down a connection to the server.
209 * (input) An ASL client handle
211 void asl_close(aslclient asl
);
214 * Write log messages to the given file descriptor.
216 * Log messages will be written to this file as well as to the server.
219 * (input) An ASL client handle
221 * (input) A file descriptor
222 * @result Returns 0 on success, non-zero on failure
224 int asl_add_log_file(aslclient asl
, int fd
);
227 * Stop writing log messages to the given file descriptor.
228 * The file descripter is not closed by this routine.
231 * (input) An ASL client handle
233 * (input) A file descriptor
234 * @result Returns 0 on success, non-zero on failure
236 int asl_remove_log_file(aslclient asl
, int fd
);
239 * Set a filter for messages being sent to the server.
240 * The filter is a bitmask representing priorities. The ASL_FILTER_MASK
241 * macro may be used to convert a priority level into a bitmask for that
242 * level. The ASL_FILTER_MASK_UPTO macro creates a bitmask for all
243 * priorities up to and including a given priority.
244 * Messages with priority levels that do not have a corresponding bit
245 * set in the filter are not sent to the server, although they will be
246 * sent to any file descripters added with asl_add_log_file().
247 * The default setting is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
248 * Returns the previous filter value.
251 * (input) An ASL client handle
253 * (input) A filter value
254 * @result Returns the previous filter value
256 int asl_set_filter(aslclient asl
, int f
);
259 * Examine attribute keys.
262 * (input) An ASL message
264 * (input) An index value
265 * @result Returns the key of the nth attribute in a message (beginning at zero),
266 * or NULL if n is greater than the largest message index.
268 const char *asl_key(aslmsg msg
, uint32_t n
);
271 * Create a new log message or query message.
274 * (input) Message type (see aslmsg Types)
275 * @result Returns a newly allocated asmsg of the specified type
277 aslmsg
asl_new(uint32_t type
);
280 * Set or re-set a message attribute.
285 * (input) Attribute key
287 * (input) Attribute value
288 * @result returns 0 for success, non-zero for failure
290 int asl_set(aslmsg msg
, const char *key
, const char *value
);
293 * Remove a message attribute.
298 * (input) Attribute key
299 * returns 0 for success, non-zero for failure
301 int asl_unset(aslmsg msg
, const char *key
);
304 * Get the value of a message attribute.
309 * (input) Attribute key
310 * @result Returns the attribute value, or NULL if the message does not contain the key
312 const char *asl_get(aslmsg msg
, const char *key
);
315 * Log a message with a particular log level.
318 * (input) An ASL client handle
320 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
322 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
324 * (input) A printf() - style format string followed by a list of arguments
325 * @result Returns 0 for success, non-zero for failure
327 #ifdef __DARWIN_LDBL_COMPAT2
328 int asl_log(aslclient asl
, aslmsg msg
, int level
, const char *format
, ...) __DARWIN_LDBL_COMPAT2(asl_log
) __printflike(4, 5);
330 int asl_log(aslclient asl
, aslmsg msg
, int level
, const char *format
, ...) __printflike(4, 5);
334 * Log a message with a particular log level.
335 * Similar to asl_log, but takes a va_list argument.
338 * (input) An ASL client handle
340 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
342 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
344 * (input) A printf() - style format string followed by a list of arguments
346 * (input) A va_list containing the values for the format string
347 * @result Returns 0 for success, non-zero for failure
349 #ifdef __DARWIN_LDBL_COMPAT2
350 int asl_vlog(aslclient asl
, aslmsg msg
, int level
, const char *format
, va_list ap
) __DARWIN_LDBL_COMPAT2(asl_vlog
) __printflike(4, 0);
352 int asl_vlog(aslclient asl
, aslmsg msg
, int level
, const char *format
, va_list ap
) __printflike(4, 0);
358 * This routine may be used instead of asl_log() or asl_vlog() if asl_set()
359 * has been used to set all of a message's attributes.
362 * (input) An ASL client handle
365 * @result Returns 0 for success, non-zero for failure
367 int asl_send(aslclient asl
, aslmsg msg
);
370 * Free a message. Frees all the attribute keys and values.
373 * (input) An aslmsg to free
375 void asl_free(aslmsg msg
);
378 * Set arbitrary parameters of a query.
379 * This is similar to asl_set, but allows richer query operations.
380 * See ASL_QUERY_OP_* above.
385 * (input) Attribute key
387 * (input) Attribute value
389 * (input) An operation (ASL_QUERY_OP_*)
390 * @result Returns 0 for success, non-zero for failure
392 int asl_set_query(aslmsg msg
, const char *key
, const char *value
, uint32_t op
);
395 * Search for messages matching the criteria described by the aslmsg.
396 * The caller should set the attributes to match using asl_set_query() or asl_set().
397 * The operatoin ASL_QUERY_OP_EQUAL is used for attributes set with asl_set().
400 * (input) An aslmsg to match
401 * @result Returns a set of messages accessable using aslresponse_next(),
403 aslresponse
asl_search(aslclient asl
, aslmsg msg
);
406 * Iterate over responses returned from asl_search().
409 * (input) An aslresponse returned by asl_search()
410 * @result Returns the next message (an aslmsg) in the response, or NULL when there are no more messages
412 aslmsg
aslresponse_next(aslresponse r
);
415 * Free a response returned from asl_search().
417 * (input) An aslresponse returned by asl_search()
419 void aslresponse_free(aslresponse r
);
422 * Creates an auxiliary file that may be used to save arbitrary data. The ASL message msg
423 * will be saved at the time that the auxiliary file is closed with asl_close_auxiliary_file().
424 * The log entry will include any keys and values found in msg, and it will include the title
425 * and Uniform Type Identifier specified. If NULL is supplied as a value for the uti parameter,
426 * the type "public.data" is used. Console.app will display a hyperlink to the file.
427 * Output parameter out_fd will contain a readable and writable file descriptor for the new
430 * By default, the file will be world-readable. If the message contains a ReadUID and/or a
431 * ReadGID key, then the values for those keys will determine read access to the file.
433 * The file will be deleted at the same time that the message expires from the ASL data store.
434 * The aslmanager utility manages message expiry. If msg contains a value for ASLExpireTime,
435 * then the message and the file will not be deleted before that time. The value may be in
436 * seconds after the Epoch, or it may be ctime() format, e.g "Thu Jun 24 18:22:48 2010".
441 * (input) A title string for the file
443 * (input) Uniform Type Identifier for the file
445 * (output) A writable file descriptor
446 * @result Returns 0 for success, non-zero for failure
448 int asl_create_auxiliary_file(aslmsg msg
, const char *title
, const char *uti
, int *out_fd
)
449 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
);
452 * Close an auxiliary file opened by asl_create_auxiliary_file() when writing is complete.
453 * syslogd will log the message provided to asl_create_auxiliary_file() when this routine
457 * (input) The file descriptor
458 * @result Returns 0 for success, non-zero for failure
460 int asl_close_auxiliary_file(int fd
)
461 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
);
464 * Sends an ASL message to syslogd along with a title string, Uniform Resource Locator,
465 * and Uniform Type Identifier specified. Console.app will hyperlink the title string to
466 * the specified URL. If NULL is supplied as a value for the uti parameter, the default
467 * type "public.data" is used.
472 * (input) A title string for the file
474 * (input) Uniform Type Identifier for the file
476 * (input) Uniform Type Locator
477 * @result Returns 0 for success, non-zero for failure
479 int asl_log_auxiliary_location(aslmsg msg
, const char *title
, const char *uti
, const char *url
)
480 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
);
483 * Creates an aslclient for logging to a file descriptor. The file must be opened for read and
484 * write access. This routine may be used in conjunction with asl_create_auxiliary_file() to
485 * save ASL format log messages to an auxiliary file.
487 * The file will be truncated if it is not empty. When logging to the auxiliary file is complete,
488 * aslclient should be closed using asl_close(). The file should be closed using
489 * asl_close_auxiliary_file() if it was returned by asl_create_auxiliary_file(), or close()
492 * The returned aslclient is thread-safe.
494 * Note that per-message read access controls (ReadUID and ReadGID) and message expire
495 * times (ASLExpireTime) keys have no effect for messages written to this file.
498 * (input) A file descriptor
500 * (input) Sender name
502 * (input) Facility name
503 * @result An aslclient
505 aslclient
asl_open_from_file(int fd
, const char *ident
, const char *facility
)
506 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
);
510 #endif /* __ASL_H__ */