]> git.saurik.com Git - apple/libc.git/blob - include/asl.h
Libc-825.24.tar.gz
[apple/libc.git] / include / asl.h
1 /*
2 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef __ASL_H__
25 #define __ASL_H__
26
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <sys/cdefs.h>
30 #include <Availability.h>
31
32 typedef struct __aslclient *aslclient;
33 typedef struct __aslmsg *aslmsg;
34 typedef struct __aslresponse *aslresponse;
35
36 /*! @header
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.
43 */
44
45 /*
46 * NOTE FOR HeaderDoc
47 *
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.
51 */
52 #ifndef __printflike
53 /*! @parseOnly */
54 #define __printflike(a,b)
55 #endif
56
57 /*! @defineblock Log Message Priority Levels
58 * Log levels of the message.
59 */
60 #define ASL_LEVEL_EMERG 0
61 #define ASL_LEVEL_ALERT 1
62 #define ASL_LEVEL_CRIT 2
63 #define ASL_LEVEL_ERR 3
64 #define ASL_LEVEL_WARNING 4
65 #define ASL_LEVEL_NOTICE 5
66 #define ASL_LEVEL_INFO 6
67 #define ASL_LEVEL_DEBUG 7
68 /*! @/defineblock */
69
70 /*! @defineblock Log Message Priority Level Strings
71 * Strings corresponding to log levels.
72 */
73 #define ASL_STRING_EMERG "Emergency"
74 #define ASL_STRING_ALERT "Alert"
75 #define ASL_STRING_CRIT "Critical"
76 #define ASL_STRING_ERR "Error"
77 #define ASL_STRING_WARNING "Warning"
78 #define ASL_STRING_NOTICE "Notice"
79 #define ASL_STRING_INFO "Info"
80 #define ASL_STRING_DEBUG "Debug"
81 /*! @/defineblock */
82
83 /*! @defineblock Attribute Matching
84 * Attribute value comparison operations.
85 */
86 #define ASL_QUERY_OP_CASEFOLD 0x0010
87 #define ASL_QUERY_OP_PREFIX 0x0020
88 #define ASL_QUERY_OP_SUFFIX 0x0040
89 #define ASL_QUERY_OP_SUBSTRING 0x0060
90 #define ASL_QUERY_OP_NUMERIC 0x0080
91 #define ASL_QUERY_OP_REGEX 0x0100
92
93 #define ASL_QUERY_OP_EQUAL 0x0001
94 #define ASL_QUERY_OP_GREATER 0x0002
95 #define ASL_QUERY_OP_GREATER_EQUAL 0x0003
96 #define ASL_QUERY_OP_LESS 0x0004
97 #define ASL_QUERY_OP_LESS_EQUAL 0x0005
98 #define ASL_QUERY_OP_NOT_EQUAL 0x0006
99 #define ASL_QUERY_OP_TRUE 0x0007
100 /*! @/defineblock */
101
102 /*! @defineblock Message Attributes
103 *
104 * These attributes are known by ASL, and are generally
105 * associated with all log messages.
106 * Additional attributes may be added as desired.
107 */
108 #define ASL_KEY_TIME "Time" /* Timestamp. Set automatically */
109 #define ASL_KEY_TIME_NSEC "TimeNanoSec" /* Nanosecond time. */
110 #define ASL_KEY_HOST "Host" /* Sender's address (set by the server). */
111 #define ASL_KEY_SENDER "Sender" /* Sender's identification string. Default is process name. */
112 #define ASL_KEY_FACILITY "Facility" /* Sender's facility. Default is "user". */
113 #define ASL_KEY_PID "PID" /* Sending process ID encoded as a string. Set automatically. */
114 #define ASL_KEY_UID "UID" /* UID that sent the log message (set by the server). */
115 #define ASL_KEY_GID "GID" /* GID that sent the log message (set by the server). */
116 #define ASL_KEY_LEVEL "Level" /* Log level number encoded as a string. See levels above. */
117 #define ASL_KEY_MSG "Message" /* Message text. */
118 #define ASL_KEY_READ_UID "ReadUID" /* User read access (-1 is any user). */
119 #define ASL_KEY_READ_GID "ReadGID" /* Group read access (-1 is any group). */
120 #define ASL_KEY_EXPIRE_TIME "ASLExpireTime" /* Expiration time for messages with long TTL. */
121 #define ASL_KEY_MSG_ID "ASLMessageID" /* 64-bit message ID number (set by the server). */
122 #define ASL_KEY_SESSION "Session" /* Session (set by the launchd). */
123 #define ASL_KEY_REF_PID "RefPID" /* Reference PID for messages proxied by launchd */
124 #define ASL_KEY_REF_PROC "RefProc" /* Reference process for messages proxied by launchd */
125 #define ASL_KEY_AUX_TITLE "ASLAuxTitle" /* Auxiliary title string */
126 #define ASL_KEY_AUX_UTI "ASLAuxUTI" /* Auxiliary Uniform Type ID */
127 #define ASL_KEY_AUX_URL "ASLAuxURL" /* Auxiliary Uniform Resource Locator */
128 #define ASL_KEY_AUX_DATA "ASLAuxData" /* Auxiliary in-line data */
129 #define ASL_KEY_OPTION "ASLOption" /* Internal */
130 #define ASL_KEY_SENDER_INSTANCE "SenderInstance" /* Sender instance UUID. */
131 /*! @/defineblock */
132
133 /*! @defineblock aslmsg Types
134 * Message type argument passed to asl_new().
135 */
136 #define ASL_TYPE_MSG 0
137 #define ASL_TYPE_QUERY 1
138 /*! @/defineblock */
139
140 /*! @defineblock Filter Masks
141 * Used in client-side filtering, which determines which
142 * messages are sent by the client to the syslogd server.
143 */
144 #define ASL_FILTER_MASK_EMERG 0x01
145 #define ASL_FILTER_MASK_ALERT 0x02
146 #define ASL_FILTER_MASK_CRIT 0x04
147 #define ASL_FILTER_MASK_ERR 0x08
148 #define ASL_FILTER_MASK_WARNING 0x10
149 #define ASL_FILTER_MASK_NOTICE 0x20
150 #define ASL_FILTER_MASK_INFO 0x40
151 #define ASL_FILTER_MASK_DEBUG 0x80
152 /*! @/defineblock */
153
154 /*! @defineblock Filter Mask Macros
155 * Macros to create bitmasks for filter settings - see asl_set_filter().
156 */
157 #define ASL_FILTER_MASK(level) (1 << (level))
158 #define ASL_FILTER_MASK_UPTO(level) ((1 << ((level) + 1)) - 1)
159 /*! @/defineblock */
160
161 /*! @defineblock Client Creation Options
162 * Options for asl_open().
163 */
164 #define ASL_OPT_STDERR 0x00000001
165 #define ASL_OPT_NO_DELAY 0x00000002
166 #define ASL_OPT_NO_REMOTE 0x00000004
167 /*! @/defineblock */
168
169 /*! @defineblock File Descriptor Types
170 * Instructions on how to treat the file descriptor in asl_log_descriptor().
171 */
172 #define ASL_LOG_DESCRIPTOR_READ 1
173 #define ASL_LOG_DESCRIPTOR_WRITE 2
174
175 /*!
176 * ASL_PREFILTER_LOG is a macro similar to asl_log(), but it first checks
177 * if the message will simply be ignored due to local filter settings.
178 * This prevents the variable argument list from being evaluated.
179 * Note that the message may still be processed if it will be written
180 * to a file or stderr.
181 *
182 * @param asl
183 * (input) An ASL client handle
184 * @param msg
185 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
186 * @param level
187 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
188 * @param format
189 * (input) A printf() - style format string followed by a list of arguments
190 */
191 #define ASL_PREFILTER_LOG(asl, msg, level, format, ...) \
192 do { \
193 aslclient _asl = (asl); \
194 aslmsg _msg = (msg); \
195 uint32_t _asl_eval = _asl_evaluate_send(_asl, _msg, (level)); \
196 if (_asl_eval != 0) _asl_lib_log(_asl, _asl_eval, _msg, (format), ## __VA_ARGS__); \
197 } while (0)
198
199 __BEGIN_DECLS
200
201 /* ASL Library SPI - do not call directly */
202 int _asl_lib_log(aslclient asl, uint32_t eval, aslmsg msg, const char *format, ...) __printflike(4, 5);
203
204 uint32_t _asl_evaluate_send(aslclient asl, aslmsg msg, int level);
205
206 /*!
207 * Initialize a connection to the ASL server.
208 *
209 * This call is optional in most cases. The library will perform any
210 * necessary initializations on the fly. A call to asl_open() is required
211 * if optional settings must be made before messages are sent to the server.
212 * These include setting the client filter and managing additional output
213 * file descriptors. Note that the default setting of the client filter is
214 * ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE), so ASL_LEVEL_DEBUG and ASL_LEVEL_INFO
215 * messages are not sent to the server by default.
216 *
217 * Options (defined above) may be set using the opts parameter. They are:
218 *
219 * ASL_OPT_STDERR - adds stderr as an output file descriptor
220 *
221 * ASL_OPT_NO_DELAY - connects to the server immediately
222 *
223 * ASL_OPT_NO_REMOTE - disables the remote-control mechanism for adjusting
224 * filter levers for processes using e.g. syslog -c ...
225 *
226 * @param ident
227 * (input) Sender name
228 * @param facility
229 * (input) Facility name
230 * @param opts
231 * (input) Options (see asl_open Options)
232 * @result Returns an ASL client handle
233 */
234 aslclient asl_open(const char *ident, const char *facility, uint32_t opts);
235
236 /*!
237 * Shuts down a connection to the server.
238 *
239 * @param asl
240 * (input) An ASL client handle
241 */
242 void asl_close(aslclient asl);
243
244 /*!
245 * Write log messages to the given file descriptor.
246 *
247 * Log messages will be written to this file as well as to the server.
248 *
249 * @param asl
250 * (input) An ASL client handle
251 * @param descriptor
252 * (input) A file descriptor
253 * @result Returns 0 on success, non-zero on failure
254 */
255 int asl_add_log_file(aslclient asl, int descriptor);
256
257 /*!
258 * Stop writing log messages to the given file descriptor.
259 * The file descripter is not closed by this routine.
260 *
261 * @param asl
262 * (input) An ASL client handle
263 * @param descriptor
264 * (input) A file descriptor
265 * @result Returns 0 on success, non-zero on failure
266 */
267 int asl_remove_log_file(aslclient asl, int descriptor);
268
269 /*!
270 * Set a filter for messages being sent to the server.
271 * The filter is a bitmask representing priorities. The ASL_FILTER_MASK
272 * macro may be used to convert a priority level into a bitmask for that
273 * level. The ASL_FILTER_MASK_UPTO macro creates a bitmask for all
274 * priorities up to and including a given priority.
275 * Messages with priority levels that do not have a corresponding bit
276 * set in the filter are not sent to the server, although they will be
277 * sent to any file descripters added with asl_add_log_file().
278 * The default setting is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
279 * Returns the previous filter value.
280 *
281 * @param asl
282 * (input) An ASL client handle
283 * @param f
284 * (input) A filter value
285 * @result Returns the previous filter value
286 */
287 int asl_set_filter(aslclient asl, int f);
288
289 /*
290 * Examine attribute keys.
291 *
292 * @param msg
293 * (input) An ASL message
294 * @param n
295 * (input) An index value
296 * @result Returns the key of the nth attribute in a message (beginning at zero),
297 * or NULL if n is greater than the largest message index.
298 */
299 const char *asl_key(aslmsg msg, uint32_t n);
300
301 /*!
302 * Create a new log message or query message.
303 *
304 * @param type
305 * (input) Message type (see aslmsg Types)
306 * @result Returns a newly allocated asmsg of the specified type
307 */
308 aslmsg asl_new(uint32_t type);
309
310 /*!
311 * Set or re-set a message attribute.
312 *
313 * @param msg
314 * (input) An aslmsg
315 * @param key
316 * (input) Attribute key
317 * @param value
318 * (input) Attribute value
319 * @result returns 0 for success, non-zero for failure
320 */
321 int asl_set(aslmsg msg, const char *key, const char *value);
322
323 /*!
324 * Remove a message attribute.
325 *
326 * @param msg
327 * (input) An aslmsg
328 * @param key
329 * (input) Attribute key
330 * returns 0 for success, non-zero for failure
331 */
332 int asl_unset(aslmsg msg, const char *key);
333
334 /*!
335 * Get the value of a message attribute.
336 *
337 * @param msg
338 * (input) An aslmsg
339 * @param key
340 * (input) Attribute key
341 * @result Returns the attribute value, or NULL if the message does not contain the key
342 */
343 const char *asl_get(aslmsg msg, const char *key);
344
345 /*!
346 * Log a message with a particular log level.
347 *
348 * @param asl
349 * (input) An ASL client handle
350 * @param msg
351 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
352 * @param level
353 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
354 * @param format
355 * (input) A printf() - style format string followed by a list of arguments
356 * @result Returns 0 for success, non-zero for failure
357 */
358 int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __printflike(4, 5);
359
360 /*!
361 * Log a message with a particular log level.
362 * Similar to asl_log, but takes a va_list argument.
363 *
364 * @param asl
365 * (input) An ASL client handle
366 * @param msg
367 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
368 * @param level
369 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
370 * @param format
371 * (input) A printf() - style format string followed by a list of arguments
372 * @param ap
373 * (input) A va_list containing the values for the format string
374 * @result Returns 0 for success, non-zero for failure
375 */
376 int asl_vlog(aslclient asl, aslmsg msg, int level, const char *format, va_list ap) __printflike(4, 0);
377
378 /*!
379 * Log a message.
380 *
381 * This routine may be used instead of asl_log() or asl_vlog() if asl_set()
382 * has been used to set all of a message's attributes.
383 *
384 * @param asl
385 * (input) An ASL client handle
386 * @param msg
387 * (input) An aslmsg
388 * @result Returns 0 for success, non-zero for failure
389 */
390 int asl_send(aslclient asl, aslmsg msg);
391
392 /*!
393 * Free a message. Frees all the attribute keys and values.
394 *
395 * @param msg
396 * (input) An aslmsg to free
397 */
398 void asl_free(aslmsg msg);
399
400 /*!
401 * Set arbitrary parameters of a query.
402 * This is similar to asl_set, but allows richer query operations.
403 * See ASL_QUERY_OP_* above.
404 *
405 * @param msg
406 * (input) An aslmsg
407 * @param key
408 * (input) Attribute key
409 * @param value
410 * (input) Attribute value
411 * @param op
412 * (input) An operation (ASL_QUERY_OP_*)
413 * @result Returns 0 for success, non-zero for failure
414 */
415 int asl_set_query(aslmsg msg, const char *key, const char *value, uint32_t op);
416
417 /*!
418 * Search for messages matching the criteria described by the aslmsg.
419 * The caller should set the attributes to match using asl_set_query() or asl_set().
420 * The operatoin ASL_QUERY_OP_EQUAL is used for attributes set with asl_set().
421 *
422 * @param msg
423 * (input) An aslmsg to match
424 * @result Returns a set of messages accessable using aslresponse_next(),
425 */
426 aslresponse asl_search(aslclient asl, aslmsg msg);
427
428 /*!
429 * Iterate over responses returned from asl_search().
430 *
431 * @param r
432 * (input) An aslresponse returned by asl_search()
433 * @result Returns the next message (an aslmsg) in the response, or NULL when there are no more messages
434 */
435 aslmsg aslresponse_next(aslresponse r);
436
437 /*!
438 * Free a response returned from asl_search().
439 * @param r
440 * (input) An aslresponse returned by asl_search()
441 */
442 void aslresponse_free(aslresponse r);
443
444 /*!
445 * Creates an auxiliary file that may be used to save arbitrary data. The ASL message msg
446 * will be saved at the time that the auxiliary file is closed with asl_close_auxiliary_file().
447 * The log entry will include any keys and values found in msg, and it will include the title
448 * and Uniform Type Identifier specified. If NULL is supplied as a value for the uti parameter,
449 * the type "public.data" is used. Console.app will display a hyperlink to the file.
450 * Output parameter out_descriptor will contain a readable and writable file descriptor for the new
451 * auxiliary file.
452 *
453 * By default, the file will be world-readable. If the message contains a ReadUID and/or a
454 * ReadGID key, then the values for those keys will determine read access to the file.
455 *
456 * The file will be deleted at the same time that the message expires from the ASL data store.
457 * The aslmanager utility manages message expiry. If msg contains a value for ASLExpireTime,
458 * then the message and the file will not be deleted before that time. The value may be in
459 * seconds after the Epoch, or it may be ctime() format, e.g "Thu Jun 24 18:22:48 2010".
460 *
461 * @param msg
462 * (input) An aslmsg
463 * @param tite
464 * (input) A title string for the file
465 * @param uti
466 * (input) Uniform Type Identifier for the file
467 * @param out_descriptor
468 * (output) A writable file descriptor
469 * @result Returns 0 for success, non-zero for failure
470 */
471 int asl_create_auxiliary_file(aslmsg msg, const char *title, const char *uti, int *out_descriptor)
472 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0);
473
474 /*!
475 * Close an auxiliary file opened by asl_create_auxiliary_file() when writing is complete.
476 * syslogd will log the message provided to asl_create_auxiliary_file() when this routine
477 * is called.
478 *
479 * @param descriptor
480 * (input) The file descriptor
481 * @result Returns 0 for success, non-zero for failure
482 */
483 int asl_close_auxiliary_file(int descriptor)
484 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0);
485
486 /*!
487 * Sends an ASL message to syslogd along with a title string, Uniform Resource Locator,
488 * and Uniform Type Identifier specified. Console.app will hyperlink the title string to
489 * the specified URL. If NULL is supplied as a value for the uti parameter, the default
490 * type "public.data" is used.
491 *
492 * @param msg
493 * (input) An aslmsg
494 * @param title
495 * (input) A title string for the file
496 * @param uti
497 * (input) Uniform Type Identifier for the file
498 * @param url
499 * (input) Uniform Type Locator
500 * @result Returns 0 for success, non-zero for failure
501 */
502 int asl_log_auxiliary_location(aslmsg msg, const char *title, const char *uti, const char *url)
503 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0);
504
505 /*!
506 * Creates an aslclient for logging to a file descriptor. The file must be opened for read and
507 * write access. This routine may be used in conjunction with asl_create_auxiliary_file() to
508 * save ASL format log messages to an auxiliary file.
509 *
510 * The file will be truncated if it is not empty. When logging to the auxiliary file is complete,
511 * aslclient should be closed using asl_close(). The file should be closed using
512 * asl_close_auxiliary_file() if it was returned by asl_create_auxiliary_file(), or close()
513 * otherwise.
514 *
515 * The returned aslclient is thread-safe.
516 *
517 * Note that per-message read access controls (ReadUID and ReadGID) and message expire
518 * times (ASLExpireTime) keys have no effect for messages written to this file.
519 *
520 * @param descriptor
521 * (input) A file descriptor
522 * @param ident
523 * (input) Sender name
524 * @param facility
525 * (input) Facility name
526 * @result An aslclient
527 */
528 aslclient asl_open_from_file(int descriptor, const char *ident, const char *facility)
529 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0);
530
531 /*!
532 * This API provides functionality to use file descriptors to send logging
533 * data to ASL.
534 *
535 * asl is retained by ASL and must still be closed by the caller by calling
536 * asl_close() if the caller loses reference to it. msg is copied by ASL and
537 * similarly must still be freed by the caller by calling asl_free() if the
538 * caller loses reference to it. Any changes made to it after calling
539 * asl_log_descriptor() are not applicable to the message used. descriptor
540 * is treated differentlty based on the value of fd_type.
541 *
542 * If fd_type is ASL_LOG_DESCRIPTOR_READ, the descriptor must be open for read
543 * access. ASL uses GCD to read from the descriptor as data becomes available.
544 * These data are line buffered and passed to asl_log. When EOF is read, the
545 * descriptor is closed.
546 *
547 * Example:
548 * asl_log_descriptor(c, m, ASL_LEVEL_NOTICE, STDIN_FILENO, ASL_LOG_DESCRIPTOR_READ);
549 *
550 * If fd_type is ASL_LOG_DESCRIPTOR_WRITE, the descriptor is closed and a new
551 * writable descriptor is created with the same fileno. Any data written to
552 * this new descriptor are line buffered and passed to asl_log. When EOF is
553 * sent, no further data are read. The caller is responsible for closing the
554 * new descriptor. One common use for this API is to redirect writes to stdout
555 * or stderr to ASL by passing STDOUT_FILENO or STDERR_FILENO as descriptor.
556 *
557 * Example:
558 * asl_log_descriptor(c, m, ASL_LEVEL_NOTICE, STDOUT_FILENO, ASL_LOG_DESCRIPTOR_WRITE);
559 * asl_log_descriptor(c, m, ASL_LEVEL_ERR, STDERR_FILENO, ASL_LOG_DESCRIPTOR_WRITE);
560 *
561 * @param asl
562 * (input) An ASL client handle
563 * @param msg
564 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
565 * @param level
566 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
567 * @param descriptor
568 * (input) An open file descriptor to read from
569 * @param fd_type
570 * (input) Either ASL_LOG_DESCRIPTOR_READ or ASL_LOG_DESCRIPTOR_WRITE
571 * @result Returns 0 for success, non-zero for failure
572 */
573 int asl_log_descriptor(aslclient asl, aslmsg msg, int level, int descriptor, uint32_t fd_type)
574 __OSX_AVAILABLE_STARTING(__MAC_10_8,__IPHONE_5_1);
575
576 __END_DECLS
577
578 #endif /* __ASL_H__ */