2 * Copyright (c) 2015 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@
27 #include <os/object.h>
31 #ifndef __has_attribute
32 #define __has_attribute(x) 0
36 #define __has_builtin(x) 0
39 #if __has_attribute(not_tail_called)
40 #define OS_LOG_NOTAILCALL __attribute__((not_tail_called))
41 #define OS_LOG_NOTAILCALL_MARKER
43 #define OS_LOG_NOTAILCALL
44 #define OS_LOG_NOTAILCALL_MARKER __asm__("")
49 extern void *__dso_handle
;
51 #ifdef XNU_KERNEL_PRIVATE
52 extern bool startup_serial_logging_active
;
53 extern uint64_t startup_serial_num_procs
;
54 #endif /* XNU_KERNEL_PRIVATE */
56 OS_ALWAYS_INLINE
static inline void _os_log_verify_format_str(__unused
const char *msg
, ...) __attribute__((format(os_log
, 1, 2)));
57 OS_ALWAYS_INLINE
static inline void _os_log_verify_format_str(__unused
const char *msg
, ...) { /* placeholder */ }
59 #if OS_OBJECT_USE_OBJC
60 OS_OBJECT_DECL(os_log
);
62 typedef struct os_log_s
*os_log_t
;
63 #endif /* OS_OBJECT_USE_OBJC */
66 * @const OS_LOG_DISABLED
69 * Use this to disable a specific log message.
71 #define OS_LOG_DISABLED NULL
74 * @const OS_LOG_DEFAULT
77 * Use this to log a message in accordance with current system settings.
79 #define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
80 __OSX_AVAILABLE_STARTING(__MAC_10_12
,__IPHONE_10_0
)
82 struct os_log_s _os_log_default
;
88 * Supported log message types.
90 * @constant OS_LOG_TYPE_DEFAULT
91 * Equivalent type for "os_log()" messages, i.e., default messages that are always
92 * captured to memory or disk.
94 * @constant OS_LOG_TYPE_INFO
95 * Equivalent type for "os_log_info()" messages, i.e., Additional informational messages.
97 * @constant OS_LOG_TYPE_DEBUG
98 * Equivalent type for "os_log_debug()" messages, i.e., Debug messages.
100 * @constant OS_LOG_TYPE_ERROR
101 * Equivalent type for "os_log_error()" messages, i.e., local process error messages.
103 * @constant OS_LOG_TYPE_FAULT
104 * Equivalent type for "os_log_fault()" messages, i.e., a system error that involves
105 * potentially more than one process, usually used by daemons and services.
107 OS_ENUM(os_log_type
, uint8_t,
108 OS_LOG_TYPE_DEFAULT
= 0x00,
109 OS_LOG_TYPE_INFO
= 0x01,
110 OS_LOG_TYPE_DEBUG
= 0x02,
111 OS_LOG_TYPE_ERROR
= 0x10,
112 OS_LOG_TYPE_FAULT
= 0x11);
115 * @function os_log_create
118 * Creates a log object to be used with other log related functions.
121 * Creates a log object to be used with other log related functions. The
122 * log object serves two purposes: (1) tag related messages by subsystem
123 * and category name for easy filtering, and (2) control logging system
124 * behavior for messages.
126 * A log object may customize logging system behavior for its messages by
127 * adding a configuration file in /Library/LogPreferences. Most options
128 * accept 3 values: "Default", "Yes" or "No" as strings, where "Default"
129 * signifies follow system behavior for the level of messages.
133 * os_log_create("com.company.mysubsystem", "connections");
135 * System-provided preferences are located in /System/Library/LogPreferences/<subsystem>.plist
139 * <!-- Default options applied to message types in each category, which can be overriden. -->
140 * <key>DEFAULT-OPTIONS</key>
142 * <key>Enabled</key> <!-- Enabled state follows system defaults -->
143 * <string>Default</string>
144 * <key>Persist</key> <!-- Do not persist to disk, use memory-only buffer if enabled -->
145 * <string>No</string>
146 * <key>TTL</key> <!-- Follow system default behavior if persistence is enabled -->
147 * <string>Default</string> <!-- Can specify in days with "d" or hours "h" (e.g., "4h" = 4 hours) -->
150 * <!-- category named “connections” -->
151 * <key>connections</key>
154 * <!-- Options that control "os_log()" behavior. The "Enabled" option is ignored. -->
157 * <key>Persist</key> <!-- Always persist to disk -->
158 * <string>Yes</string>
159 * <key>TTL</key> <!-- Store default messages for maximum 4 days -->
160 * <integer>4d</integer>
163 * <!-- Subdictionary of options that control "os_log_info()" behavior -->
166 * <key>Persist</key> <!-- If enabled persist to disk -->
167 * <string>Yes</string>
168 * <key>TTL</key> <!-- Store Info messages for 2 days -->
169 * <string>2d</string>
172 * <!-- Subdictionary of options that control "os_log_debug()" behavior -->
175 * <key>Enabled</key> <!-- Not enabled, must be enabled specifically -->
176 * <string>No</string>
181 * All other preferences and system-overrides are stored in /Library/LogPreferences/.
184 * The identifier of the given subsystem should be in reverse DNS form
185 * (i.e., com.company.mysubsystem). This string must be a constant string,
186 * not dynamically generated.
189 * The category within the given subsystem that specifies the settings for
190 * the log object. This string must be a constant string, not dynamically
194 * Returns an os_log_t value to be passed to other os_log API calls. This
195 * should be called once at log initialization and rely on system to detect
196 * changes to settings. This object should be released when no longer used
197 * via os_release or -[release] method.
199 * A value will always be returned to allow for dynamic enablement.
201 __OSX_AVAILABLE_STARTING(__MAC_10_12
,__IPHONE_10_0
)
202 OS_EXPORT OS_NOTHROW OS_WARN_RESULT OS_OBJECT_RETURNS_RETAINED
204 os_log_create(const char *subsystem
, const char *category
);
207 * @function os_log_info_enabled
210 * Returns if development log messages are enabled for a particular log object.
213 * Returns if development log messages are enabled for a particular log object.
216 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
219 * Returns ‘true’ if debug log messages are enabled.
221 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
222 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
224 os_log_info_enabled(os_log_t log
);
227 * @function os_log_debug_enabled
230 * Returns if debug log messages are enabled for a particular log object.
233 * Returns if debug log messages are enabled for a particular log object.
236 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
239 * Returns ‘true’ if debug log messages are enabled.
241 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
242 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
244 os_log_debug_enabled(os_log_t log
);
250 * Insert a log message into the Unified Logging and Tracing system.
253 * Insert a log message into the Unified Logging and Tracing system in
254 * accordance with the preferences specified by the provided log object.
255 * These messages cannot be disabled and therefore always captured either
258 * When an os_activity_id_t is present, the log message will also be scoped by
259 * that identifier. Activities provide granular filtering of log messages
260 * across threads and processes.
262 * There is a physical cap of 256 bytes per entry for dynamic content,
263 * i.e., %s and %@, that can be written to the persistence store. As such,
264 * all content exceeding the limit will be truncated before written to disk.
265 * Live streams will continue to show the full content.
268 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
271 * A format string to generate a human-readable log message when the log
272 * line is decoded. This string must be a constant string, not dynamically
273 * generated. Supports all standard printf types and %@ (objects).
275 #define os_log(log, format, ...) __extension__({ \
276 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
277 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
278 _os_log_verify_format_str(format, ##__VA_ARGS__); \
279 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEFAULT, _os_log_fmt, ##__VA_ARGS__); \
280 __asm__(""); /* avoid tailcall */ \
284 * @function os_log_info
287 * Insert a development log message into the Unified Logging and Tracing system.
290 * Insert a log message into the Unified Logging and Tracing system in
291 * accordance with the preferences specified by the provided log object.
293 * When an os_activity_id_t is present, the log message will also be scoped by
294 * that identifier. Activities provide granular filtering of log messages
295 * across threads and processes.
297 * There is a physical cap of 256 bytes per entry for dynamic content,
298 * i.e., %s and %@, that can be written to the persistence store. As such,
299 * all content exceeding the limit will be truncated before written to disk.
300 * Live streams will continue to show the full content.
303 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
306 * A format string to generate a human-readable log message when the log
307 * line is decoded. This string must be a constant string, not dynamically
308 * generated. Supports all standard printf types and %@ (objects).
310 #define os_log_info(log, format, ...) __extension__({ \
311 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
312 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
313 _os_log_verify_format_str(format, ##__VA_ARGS__); \
314 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_INFO, _os_log_fmt, ##__VA_ARGS__); \
315 __asm__(""); /* avoid tailcall */ \
319 * @function os_log_debug
322 * Insert a debug log message into the Unified Logging and Tracing system.
325 * Insert a debug log message into the Unified Logging and Tracing system in
326 * accordance with the preferences specified by the provided log object.
328 * When an os_activity_id_t is present, the log message will also be scoped by
329 * that identifier. Activities provide granular filtering of log messages
330 * across threads and processes.
332 * There is a physical cap of 256 bytes per entry for dynamic content,
333 * i.e., %s and %@, that can be written to the persistence store. As such,
334 * all content exceeding the limit will be truncated before written to disk.
335 * Live streams will continue to show the full content.
338 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
341 * A format string to generate a human-readable log message when the log
342 * line is decoded. This string must be a constant string, not dynamically
343 * generated. Supports all standard printf types and %@ (objects).
345 #define os_log_debug(log, format, ...) __extension__({ \
346 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
347 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
348 _os_log_verify_format_str(format, ##__VA_ARGS__); \
349 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
350 __asm__(""); /* avoid tailcall */ \
354 * @function os_log_error
357 * Insert an error log message into the Unified Logging and Tracing system.
360 * Insert an error log message into the Unified Logging and Tracing system.
362 * When an os_activity_id_t is present, the log message will also be scoped by
363 * that identifier. Activities provide granular filtering of log messages
364 * across threads and processes.
366 * There is a physical cap of 256 bytes per entry for dynamic content,
367 * i.e., %s and %@, that can be written to the persistence store. As such,
368 * all content exceeding the limit will be truncated before written to disk.
369 * Live streams will continue to show the full content.
372 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
375 * A format string to generate a human-readable log message when the log
376 * line is decoded. This string must be a constant string, not dynamically
377 * generated. Supports all standard printf types and %@ (objects).
379 #define os_log_error(log, format, ...) __extension__({ \
380 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
381 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
382 _os_log_verify_format_str(format, ##__VA_ARGS__); \
383 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_ERROR, _os_log_fmt, ##__VA_ARGS__); \
384 __asm__(""); /* avoid tailcall */ \
388 * @function os_log_fault
391 * Insert a fault log message into the Unified Logging and Tracing system.
394 * Log a fault message issue into the Unified Logging and Tracing system
395 * signifying a multi-process (i.e., system error) related issue, either
396 * due to interaction via IPC or some other. Faults will gather information
397 * from the entire process chain and record it for later inspection.
399 * When an os_activity_id_t is present, the log message will also be scoped by
400 * that identifier. Activities provide granular filtering of log messages
401 * across threads and processes.
403 * There is a physical cap of 256 bytes per entry for dynamic content,
404 * i.e., %s and %@, that can be written to the persistence store. As such,
405 * all content exceeding the limit will be truncated before written to disk.
406 * Live streams will continue to show the full content.
409 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
412 * A format string to generate a human-readable log message when the log
413 * line is decoded. This string must be a constant string, not dynamically
414 * generated. Supports all standard printf types and %@ (objects).
416 #define os_log_fault(log, format, ...) __extension__({ \
417 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
418 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
419 _os_log_verify_format_str(format, ##__VA_ARGS__); \
420 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_FAULT, _os_log_fmt, ##__VA_ARGS__); \
421 __asm__(""); /* avoid tailcall */ \
425 * @function os_log_with_type
428 * Log a message using a specific type.
431 * Will log a message with the provided os_log_type_t.
434 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
437 * Pass a valid type from os_log_type_t.
440 * A format string to generate a human-readable log message when the log
441 * line is decoded. This string must be a constant string, not dynamically
442 * generated. Supports all standard printf types and %@ (objects).
444 #define os_log_with_type(log, type, format, ...) __extension__({ \
445 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
446 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
447 _os_log_verify_format_str(format, ##__VA_ARGS__); \
448 _os_log_internal(&__dso_handle, log, type, _os_log_fmt, ##__VA_ARGS__); \
449 __asm__(""); /* avoid tailcall */ \
453 * @function os_log_sensitive_debug
456 * Insert a debug log message containing sensitive content (i.e., personal
457 * identifying information).
460 * Insert a debug log message containing sensitive content (i.e., personal
461 * identifying information) in accordance with the preferences specified by
462 * the provided log object.
464 * All strings are considered potentially sensitive, though this call
465 * specifically signifies the message as containing sensitive content.
466 * The message will be stored separately from other messages.
468 * When an os_activity_id_t is present, the log message will also be scoped by
469 * that identifier. Activities provide granular filtering of log messages
470 * across threads and processes.
472 * There is a physical cap of 256 bytes per entry for dynamic content,
473 * i.e., %s and %@, that can be written to the persistence store. As such,
474 * all content exceeding the limit will be truncated before written to disk.
475 * Live streams will continue to show the full content.
478 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
481 * A format string to generate a human-readable log message when the log
482 * line is decoded. This string must be a constant string, not dynamically
483 * generated. Supports all standard printf types and %@ (objects).
485 #define os_log_sensitive_debug(log, format, ...) __extension__({ \
486 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
487 __attribute__((section("__TEXT,__os_log_sens"))) static const char _os_log_fmt[] = format; \
488 _os_log_verify_format_str(format, ##__VA_ARGS__); \
489 _os_log_sensitive(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
490 __asm__(""); /* avoid tailcall */ \
493 #ifdef XNU_KERNEL_PRIVATE
494 #define os_log_with_startup_serial(log, format, ...) __extension__({ \
495 if (startup_serial_logging_active) { printf(format, ##__VA_ARGS__); } \
496 else { os_log(log, format, ##__VA_ARGS__); } \
498 #endif /* XNU_KERNEL_PRIVATE */
501 * @function _os_log_internal
504 * Internal function used by macros.
506 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
509 _os_log_internal(void *dso
, os_log_t log
, os_log_type_t type
, const char *message
, ...);
513 #endif /* __os_log_h */