]> git.saurik.com Git - apple/libc.git/blob - include/asl.h
Libc-763.11.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 __DARWIN_LDBL_COMPAT2
53 /*! @parseOnly */
54 #define __DARWIN_LDBL_COMPAT2(a)
55 #endif
56 #ifndef __printflike
57 /*! @parseOnly */
58 #define __printflike(a,b)
59 #endif
60
61 /*! @defineblock Log Message Priority Levels
62 * Log levels of the message.
63 */
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
72 /*! @/defineblock */
73
74 /*! @defineblock Log Message Priority Level Strings
75 * Strings corresponding to log levels.
76 */
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"
85 /*! @/defineblock */
86
87 /*! @defineblock Attribute Matching
88 * Attribute value comparison operations.
89 */
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
96
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
104 /*! @/defineblock */
105
106 /*! @defineblock Message Attributes
107 *
108 * These attributes are known by ASL, and are generally
109 * associated with all log messages.
110 * Additional attributes may be added as desired.
111 */
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. */
135 /*! @/defineblock */
136
137 /*! @defineblock aslmsg Types
138 * Message type argument passed to asl_new().
139 */
140 #define ASL_TYPE_MSG 0
141 #define ASL_TYPE_QUERY 1
142 /*! @/defineblock */
143
144 /*! @defineblock Filter Masks
145 * Used in client-side filtering, which determines which
146 * messages are sent by the client to the syslogd server.
147 */
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
156 /*! @/defineblock */
157
158 /*! @defineblock Filter Mask Macros
159 * Macros to create bitmasks for filter settings - see asl_set_filter().
160 */
161 #define ASL_FILTER_MASK(level) (1 << (level))
162 #define ASL_FILTER_MASK_UPTO(level) ((1 << ((level) + 1)) - 1)
163 /*! @/defineblock */
164
165 /*! @defineblock Client Creation Options
166 * Options for asl_open().
167 */
168 #define ASL_OPT_STDERR 0x00000001
169 #define ASL_OPT_NO_DELAY 0x00000002
170 #define ASL_OPT_NO_REMOTE 0x00000004
171 /*! @/defineblock */
172
173 __BEGIN_DECLS
174
175 /*!
176 * Initialize a connection to the ASL server.
177 *
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.
185 *
186 * Options (defined above) may be set using the opts parameter. They are:
187 *
188 * ASL_OPT_STDERR - adds stderr as an output file descriptor
189 *
190 * ASL_OPT_NO_DELAY - connects to the server immediately
191 *
192 * ASL_OPT_NO_REMOTE - disables the remote-control mechanism for adjusting
193 * filter levers for processes using e.g. syslog -c ...
194 *
195 * @param ident
196 * (input) Sender name
197 * @param facility
198 * (input) Facility name
199 * @param opts
200 * (input) Options (see asl_open Options)
201 * @result Returns an ASL client handle
202 */
203 aslclient asl_open(const char *ident, const char *facility, uint32_t opts);
204
205 /*!
206 * Shuts down a connection to the server.
207 *
208 * @param asl
209 * (input) An ASL client handle
210 */
211 void asl_close(aslclient asl);
212
213 /*!
214 * Write log messages to the given file descriptor.
215 *
216 * Log messages will be written to this file as well as to the server.
217 *
218 * @param asl
219 * (input) An ASL client handle
220 * @param fd
221 * (input) A file descriptor
222 * @result Returns 0 on success, non-zero on failure
223 */
224 int asl_add_log_file(aslclient asl, int fd);
225
226 /*!
227 * Stop writing log messages to the given file descriptor.
228 * The file descripter is not closed by this routine.
229 *
230 * @param asl
231 * (input) An ASL client handle
232 * @param fd
233 * (input) A file descriptor
234 * @result Returns 0 on success, non-zero on failure
235 */
236 int asl_remove_log_file(aslclient asl, int fd);
237
238 /*!
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.
249 *
250 * @param asl
251 * (input) An ASL client handle
252 * @param f
253 * (input) A filter value
254 * @result Returns the previous filter value
255 */
256 int asl_set_filter(aslclient asl, int f);
257
258 /*
259 * Examine attribute keys.
260 *
261 * @param msg
262 * (input) An ASL message
263 * @param n
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.
267 */
268 const char *asl_key(aslmsg msg, uint32_t n);
269
270 /*!
271 * Create a new log message or query message.
272 *
273 * @param type
274 * (input) Message type (see aslmsg Types)
275 * @result Returns a newly allocated asmsg of the specified type
276 */
277 aslmsg asl_new(uint32_t type);
278
279 /*!
280 * Set or re-set a message attribute.
281 *
282 * @param msg
283 * (input) An aslmsg
284 * @param key
285 * (input) Attribute key
286 * @param value
287 * (input) Attribute value
288 * @result returns 0 for success, non-zero for failure
289 */
290 int asl_set(aslmsg msg, const char *key, const char *value);
291
292 /*!
293 * Remove a message attribute.
294 *
295 * @param msg
296 * (input) An aslmsg
297 * @param key
298 * (input) Attribute key
299 * returns 0 for success, non-zero for failure
300 */
301 int asl_unset(aslmsg msg, const char *key);
302
303 /*!
304 * Get the value of a message attribute.
305 *
306 * @param msg
307 * (input) An aslmsg
308 * @param key
309 * (input) Attribute key
310 * @result Returns the attribute value, or NULL if the message does not contain the key
311 */
312 const char *asl_get(aslmsg msg, const char *key);
313
314 /*!
315 * Log a message with a particular log level.
316 *
317 * @param asl
318 * (input) An ASL client handle
319 * @param msg
320 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
321 * @param level
322 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
323 * @param format
324 * (input) A printf() - style format string followed by a list of arguments
325 * @result Returns 0 for success, non-zero for failure
326 */
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);
329 #else
330 int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __printflike(4, 5);
331 #endif
332
333 /*!
334 * Log a message with a particular log level.
335 * Similar to asl_log, but takes a va_list argument.
336 *
337 * @param asl
338 * (input) An ASL client handle
339 * @param msg
340 * (input) An aslmsg (default attributes will be supplied if msg is NULL)
341 * @param level
342 * (input) Log level (ASL_LEVEL_DEBUG to ASL_LEVEL_EMERG)
343 * @param format
344 * (input) A printf() - style format string followed by a list of arguments
345 * @param ap
346 * (input) A va_list containing the values for the format string
347 * @result Returns 0 for success, non-zero for failure
348 */
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);
351 #else
352 int asl_vlog(aslclient asl, aslmsg msg, int level, const char *format, va_list ap) __printflike(4, 0);
353 #endif
354
355 /*!
356 * Log a message.
357 *
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.
360 *
361 * @param asl
362 * (input) An ASL client handle
363 * @param msg
364 * (input) An aslmsg
365 * @result Returns 0 for success, non-zero for failure
366 */
367 int asl_send(aslclient asl, aslmsg msg);
368
369 /*!
370 * Free a message. Frees all the attribute keys and values.
371 *
372 * @param msg
373 * (input) An aslmsg to free
374 */
375 void asl_free(aslmsg msg);
376
377 /*!
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.
381 *
382 * @param msg
383 * (input) An aslmsg
384 * @param key
385 * (input) Attribute key
386 * @param value
387 * (input) Attribute value
388 * @param op
389 * (input) An operation (ASL_QUERY_OP_*)
390 * @result Returns 0 for success, non-zero for failure
391 */
392 int asl_set_query(aslmsg msg, const char *key, const char *value, uint32_t op);
393
394 /*!
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().
398 *
399 * @param msg
400 * (input) An aslmsg to match
401 * @result Returns a set of messages accessable using aslresponse_next(),
402 */
403 aslresponse asl_search(aslclient asl, aslmsg msg);
404
405 /*!
406 * Iterate over responses returned from asl_search().
407 *
408 * @param r
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
411 */
412 aslmsg aslresponse_next(aslresponse r);
413
414 /*!
415 * Free a response returned from asl_search().
416 * @param r
417 * (input) An aslresponse returned by asl_search()
418 */
419 void aslresponse_free(aslresponse r);
420
421 /*!
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
428 * auxiliary file.
429 *
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.
432 *
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".
437 *
438 * @param msg
439 * (input) An aslmsg
440 * @param tite
441 * (input) A title string for the file
442 * @param uti
443 * (input) Uniform Type Identifier for the file
444 * @param out_fd
445 * (output) A writable file descriptor
446 * @result Returns 0 for success, non-zero for failure
447 */
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);
450
451 /*!
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
454 * is called.
455 *
456 * @param fd
457 * (input) The file descriptor
458 * @result Returns 0 for success, non-zero for failure
459 */
460 int asl_close_auxiliary_file(int fd)
461 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0);
462
463 /*!
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.
468 *
469 * @param msg
470 * (input) An aslmsg
471 * @param title
472 * (input) A title string for the file
473 * @param uti
474 * (input) Uniform Type Identifier for the file
475 * @param url
476 * (input) Uniform Type Locator
477 * @result Returns 0 for success, non-zero for failure
478 */
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);
481
482 /*!
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.
486 *
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()
490 * otherwise.
491 *
492 * The returned aslclient is thread-safe.
493 *
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.
496 *
497 * @param fd
498 * (input) A file descriptor
499 * @param ident
500 * (input) Sender name
501 * @param facility
502 * (input) Facility name
503 * @result An aslclient
504 */
505 aslclient asl_open_from_file(int fd, const char *ident, const char *facility)
506 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0);
507
508 __END_DECLS
509
510 #endif /* __ASL_H__ */