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 OS_ALWAYS_INLINE
static inline void _os_log_verify_format_str(__unused
const char *msg
, ...) __attribute__((format(os_trace
, 1, 2)));
52 OS_ALWAYS_INLINE
static inline void _os_log_verify_format_str(__unused
const char *msg
, ...) { /* placeholder */ }
54 #if OS_OBJECT_USE_OBJC
55 OS_OBJECT_DECL(os_log
);
57 typedef struct os_log_s
*os_log_t
;
58 #endif /* OS_OBJECT_USE_OBJC */
61 * @const OS_LOG_DISABLED
64 * Use this to disable a specific log message.
66 #define OS_LOG_DISABLED NULL
69 * @const OS_LOG_DEFAULT
72 * Use this to log a message in accordance with current system settings.
74 #define OS_LOG_DEFAULT OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default)
75 __OSX_AVAILABLE_STARTING(__MAC_10_12
,__IPHONE_10_0
)
77 struct os_log_s _os_log_default
;
83 * Supported log message types.
85 * @constant OS_LOG_TYPE_DEFAULT
86 * Equivalent type for "os_log()" messages, i.e., default messages that are always
87 * captured to memory or disk.
89 * @constant OS_LOG_TYPE_INFO
90 * Equivalent type for "os_log_info()" messages, i.e., Additional informational messages.
92 * @constant OS_LOG_TYPE_DEBUG
93 * Equivalent type for "os_log_debug()" messages, i.e., Debug messages.
95 * @constant OS_LOG_TYPE_ERROR
96 * Equivalent type for "os_log_error()" messages, i.e., local process error messages.
98 * @constant OS_LOG_TYPE_FAULT
99 * Equivalent type for "os_log_fault()" messages, i.e., a system error that involves
100 * potentially more than one process, usually used by daemons and services.
102 OS_ENUM(os_log_type
, uint8_t,
103 OS_LOG_TYPE_DEFAULT
= 0x00,
104 OS_LOG_TYPE_INFO
= 0x01,
105 OS_LOG_TYPE_DEBUG
= 0x02,
106 OS_LOG_TYPE_ERROR
= 0x10,
107 OS_LOG_TYPE_FAULT
= 0x11);
110 * @function os_log_create
113 * Creates a log object to be used with other log related functions.
116 * Creates a log object to be used with other log related functions. The
117 * log object serves two purposes: (1) tag related messages by subsystem
118 * and category name for easy filtering, and (2) control logging system
119 * behavior for messages.
121 * A log object may customize logging system behavior for its messages by
122 * adding a configuration file in /Library/LogPreferences. Most options
123 * accept 3 values: "Default", "Yes" or "No" as strings, where "Default"
124 * signifies follow system behavior for the level of messages.
128 * os_log_create("com.company.mysubsystem", "connections");
130 * System-provided preferences are located in /System/Library/LogPreferences/<subsystem>.plist
134 * <!-- Default options applied to message types in each category, which can be overriden. -->
135 * <key>DEFAULT-OPTIONS</key>
137 * <key>Enabled</key> <!-- Enabled state follows system defaults -->
138 * <string>Default</string>
139 * <key>Persist</key> <!-- Do not persist to disk, use memory-only buffer if enabled -->
140 * <string>No</string>
141 * <key>TTL</key> <!-- Follow system default behavior if persistence is enabled -->
142 * <string>Default</string> <!-- Can specify in days with "d" or hours "h" (e.g., "4h" = 4 hours) -->
145 * <!-- category named “connections” -->
146 * <key>connections</key>
149 * <!-- Options that control "os_log()" behavior. The "Enabled" option is ignored. -->
152 * <key>Persist</key> <!-- Always persist to disk -->
153 * <string>Yes</string>
154 * <key>TTL</key> <!-- Store default messages for maximum 4 days -->
155 * <integer>4d</integer>
158 * <!-- Subdictionary of options that control "os_log_info()" behavior -->
161 * <key>Persist</key> <!-- If enabled persist to disk -->
162 * <string>Yes</string>
163 * <key>TTL</key> <!-- Store Info messages for 2 days -->
164 * <string>2d</string>
167 * <!-- Subdictionary of options that control "os_log_debug()" behavior -->
170 * <key>Enabled</key> <!-- Not enabled, must be enabled specifically -->
171 * <string>No</string>
176 * All other preferences and system-overrides are stored in /Library/LogPreferences/.
179 * The identifier of the given subsystem should be in reverse DNS form
180 * (i.e., com.company.mysubsystem). This string must be a constant string,
181 * not dynamically generated.
184 * The category within the given subsystem that specifies the settings for
185 * the log object. This string must be a constant string, not dynamically
189 * Returns an os_log_t value to be passed to other os_log API calls. This
190 * should be called once at log initialization and rely on system to detect
191 * changes to settings. This object should be released when no longer used
192 * via os_release or -[release] method.
194 * A value will always be returned to allow for dynamic enablement.
196 __OSX_AVAILABLE_STARTING(__MAC_10_12
,__IPHONE_10_0
)
197 OS_EXPORT OS_NOTHROW OS_WARN_RESULT OS_OBJECT_RETURNS_RETAINED
199 os_log_create(const char *subsystem
, const char *category
);
202 * @function os_log_info_enabled
205 * Returns if development log messages are enabled for a particular log object.
208 * Returns if development log messages are enabled for a particular log object.
211 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
214 * Returns ‘true’ if debug log messages are enabled.
216 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
217 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
219 os_log_info_enabled(os_log_t log
);
222 * @function os_log_debug_enabled
225 * Returns if debug log messages are enabled for a particular log object.
228 * Returns if debug log messages are enabled for a particular log object.
231 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
234 * Returns ‘true’ if debug log messages are enabled.
236 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
237 OS_EXPORT OS_NOTHROW OS_WARN_RESULT
239 os_log_debug_enabled(os_log_t log
);
245 * Insert a log message into the Unified Logging and Tracing system.
248 * Insert a log message into the Unified Logging and Tracing system in
249 * accordance with the preferences specified by the provided log object.
250 * These messages cannot be disabled and therefore always captured either
253 * When an os_activity_id_t is present, the log message will also be scoped by
254 * that identifier. Activities provide granular filtering of log messages
255 * across threads and processes.
257 * There is a physical cap of 256 bytes per entry for dynamic content,
258 * i.e., %s and %@, that can be written to the persistence store. As such,
259 * all content exceeding the limit will be truncated before written to disk.
260 * Live streams will continue to show the full content.
263 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
266 * A format string to generate a human-readable log message when the log
267 * line is decoded. This string must be a constant string, not dynamically
268 * generated. Supports all standard printf types and %@ (objects).
270 #define os_log(log, format, ...) __extension__({ \
271 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
272 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
273 _os_log_verify_format_str(format, ##__VA_ARGS__); \
274 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEFAULT, _os_log_fmt, ##__VA_ARGS__); \
275 __asm__(""); /* avoid tailcall */ \
279 * @function os_log_info
282 * Insert a development log message into the Unified Logging and Tracing system.
285 * Insert a log message into the Unified Logging and Tracing system in
286 * accordance with the preferences specified by the provided log object.
288 * When an os_activity_id_t is present, the log message will also be scoped by
289 * that identifier. Activities provide granular filtering of log messages
290 * across threads and processes.
292 * There is a physical cap of 256 bytes per entry for dynamic content,
293 * i.e., %s and %@, that can be written to the persistence store. As such,
294 * all content exceeding the limit will be truncated before written to disk.
295 * Live streams will continue to show the full content.
298 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
301 * A format string to generate a human-readable log message when the log
302 * line is decoded. This string must be a constant string, not dynamically
303 * generated. Supports all standard printf types and %@ (objects).
305 #define os_log_info(log, format, ...) __extension__({ \
306 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
307 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
308 _os_log_verify_format_str(format, ##__VA_ARGS__); \
309 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_INFO, _os_log_fmt, ##__VA_ARGS__); \
310 __asm__(""); /* avoid tailcall */ \
314 * @function os_log_debug
317 * Insert a debug log message into the Unified Logging and Tracing system.
320 * Insert a debug log message into the Unified Logging and Tracing system in
321 * accordance with the preferences specified by the provided log object.
323 * When an os_activity_id_t is present, the log message will also be scoped by
324 * that identifier. Activities provide granular filtering of log messages
325 * across threads and processes.
327 * There is a physical cap of 256 bytes per entry for dynamic content,
328 * i.e., %s and %@, that can be written to the persistence store. As such,
329 * all content exceeding the limit will be truncated before written to disk.
330 * Live streams will continue to show the full content.
333 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
336 * A format string to generate a human-readable log message when the log
337 * line is decoded. This string must be a constant string, not dynamically
338 * generated. Supports all standard printf types and %@ (objects).
340 #define os_log_debug(log, format, ...) __extension__({ \
341 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
342 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
343 _os_log_verify_format_str(format, ##__VA_ARGS__); \
344 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
345 __asm__(""); /* avoid tailcall */ \
349 * @function os_log_error
352 * Insert an error log message into the Unified Logging and Tracing system.
355 * Insert an error log message into the Unified Logging and Tracing system.
357 * When an os_activity_id_t is present, the log message will also be scoped by
358 * that identifier. Activities provide granular filtering of log messages
359 * across threads and processes.
361 * There is a physical cap of 256 bytes per entry for dynamic content,
362 * i.e., %s and %@, that can be written to the persistence store. As such,
363 * all content exceeding the limit will be truncated before written to disk.
364 * Live streams will continue to show the full content.
367 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
370 * A format string to generate a human-readable log message when the log
371 * line is decoded. This string must be a constant string, not dynamically
372 * generated. Supports all standard printf types and %@ (objects).
374 #define os_log_error(log, format, ...) __extension__({ \
375 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
376 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
377 _os_log_verify_format_str(format, ##__VA_ARGS__); \
378 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_ERROR, _os_log_fmt, ##__VA_ARGS__); \
379 __asm__(""); /* avoid tailcall */ \
383 * @function os_log_fault
386 * Insert a fault log message into the Unified Logging and Tracing system.
389 * Log a fault message issue into the Unified Logging and Tracing system
390 * signifying a multi-process (i.e., system error) related issue, either
391 * due to interaction via IPC or some other. Faults will gather information
392 * from the entire process chain and record it for later inspection.
394 * When an os_activity_id_t is present, the log message will also be scoped by
395 * that identifier. Activities provide granular filtering of log messages
396 * across threads and processes.
398 * There is a physical cap of 256 bytes per entry for dynamic content,
399 * i.e., %s and %@, that can be written to the persistence store. As such,
400 * all content exceeding the limit will be truncated before written to disk.
401 * Live streams will continue to show the full content.
404 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
407 * A format string to generate a human-readable log message when the log
408 * line is decoded. This string must be a constant string, not dynamically
409 * generated. Supports all standard printf types and %@ (objects).
411 #define os_log_fault(log, format, ...) __extension__({ \
412 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
413 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
414 _os_log_verify_format_str(format, ##__VA_ARGS__); \
415 _os_log_internal(&__dso_handle, log, OS_LOG_TYPE_FAULT, _os_log_fmt, ##__VA_ARGS__); \
416 __asm__(""); /* avoid tailcall */ \
420 * @function os_log_with_type
423 * Log a message using a specific type.
426 * Will log a message with the provided os_log_type_t.
429 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
432 * Pass a valid type from os_log_type_t.
435 * A format string to generate a human-readable log message when the log
436 * line is decoded. This string must be a constant string, not dynamically
437 * generated. Supports all standard printf types and %@ (objects).
439 #define os_log_with_type(log, type, format, ...) __extension__({ \
440 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
441 __attribute__((section("__TEXT,__os_log"))) static const char _os_log_fmt[] = format; \
442 _os_log_verify_format_str(format, ##__VA_ARGS__); \
443 _os_log_internal(&__dso_handle, log, type, _os_log_fmt, ##__VA_ARGS__); \
444 __asm__(""); /* avoid tailcall */ \
448 * @function os_log_sensitive_debug
451 * Insert a debug log message containing sensitive content (i.e., personal
452 * identifying information).
455 * Insert a debug log message containing sensitive content (i.e., personal
456 * identifying information) in accordance with the preferences specified by
457 * the provided log object.
459 * All strings are considered potentially sensitive, though this call
460 * specifically signifies the message as containing sensitive content.
461 * The message will be stored separately from other messages.
463 * When an os_activity_id_t is present, the log message will also be scoped by
464 * that identifier. Activities provide granular filtering of log messages
465 * across threads and processes.
467 * There is a physical cap of 256 bytes per entry for dynamic content,
468 * i.e., %s and %@, that can be written to the persistence store. As such,
469 * all content exceeding the limit will be truncated before written to disk.
470 * Live streams will continue to show the full content.
473 * Pass OS_LOG_DEFAULT or a log object previously created with os_log_create.
476 * A format string to generate a human-readable log message when the log
477 * line is decoded. This string must be a constant string, not dynamically
478 * generated. Supports all standard printf types and %@ (objects).
480 #define os_log_sensitive_debug(log, format, ...) __extension__({ \
481 _Static_assert(__builtin_constant_p(format), "format string must be constant"); \
482 __attribute__((section("__TEXT,__os_log_sens"))) static const char _os_log_fmt[] = format; \
483 _os_log_verify_format_str(format, ##__VA_ARGS__); \
484 _os_log_sensitive(&__dso_handle, log, OS_LOG_TYPE_DEBUG, _os_log_fmt, ##__VA_ARGS__); \
485 __asm__(""); /* avoid tailcall */ \
489 * @function _os_log_internal
492 * Internal function used by macros.
494 __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0)
497 _os_log_internal(void *dso
, os_log_t log
, os_log_type_t type
, const char *message
, ...);
501 #endif /* __os_log_h */