4444
4545#if defined(__APPLE__ )
4646# include <AvailabilityMacros.h>
47+ # include <TargetConditionals.h>
4748# include <mach-o/loader.h>
48- # include <os/log.h>
49+ // The os_log unified logging APIs were introduced in macOS 10.12, iOS 10.0,
50+ // tvOS 10.0, and watchOS 3.0;
51+ # if defined(TARGET_OS_IPHONE ) && TARGET_OS_IPHONE
52+ # define HAS_APPLE_SYSTEM_LOG 1
53+ # elif defined(TARGET_OS_OSX ) && TARGET_OS_OSX
54+ # if defined(MAC_OS_X_VERSION_10_12 ) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
55+ # define HAS_APPLE_SYSTEM_LOG 1
56+ # else
57+ # define HAS_APPLE_SYSTEM_LOG 0
58+ # endif
59+ # else
60+ # define HAS_APPLE_SYSTEM_LOG 0
61+ # endif
62+
63+ # if HAS_APPLE_SYSTEM_LOG
64+ # include <os/log.h>
65+ # endif
4966#endif
5067
5168#ifdef HAVE_SIGNAL_H
@@ -75,7 +92,7 @@ static PyStatus init_sys_streams(PyThreadState *tstate);
7592#ifdef __ANDROID__
7693static PyStatus init_android_streams (PyThreadState * tstate );
7794#endif
78- #if defined(__APPLE__ )
95+ #if defined(__APPLE__ ) && HAS_APPLE_SYSTEM_LOG
7996static PyStatus init_apple_streams (PyThreadState * tstate );
8097#endif
8198static void wait_for_thread_shutdown (PyThreadState * tstate );
@@ -1258,7 +1275,7 @@ init_interp_main(PyThreadState *tstate)
12581275 return status ;
12591276 }
12601277#endif
1261- #if defined(__APPLE__ )
1278+ #if defined(__APPLE__ ) && HAS_APPLE_SYSTEM_LOG
12621279 if (config -> use_system_logger ) {
12631280 status = init_apple_streams (tstate );
12641281 if (_PyStatus_EXCEPTION (status )) {
@@ -2933,7 +2950,7 @@ init_android_streams(PyThreadState *tstate)
29332950
29342951#endif // __ANDROID__
29352952
2936- #if defined(__APPLE__ )
2953+ #if defined(__APPLE__ ) && HAS_APPLE_SYSTEM_LOG
29372954
29382955static PyObject *
29392956apple_log_write_impl (PyObject * self , PyObject * args )
@@ -2944,14 +2961,9 @@ apple_log_write_impl(PyObject *self, PyObject *args)
29442961 return NULL ;
29452962 }
29462963
2947- // Call the underlying Apple logging API. The os_log unified logging APIs
2948- // were introduced in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0;
2949- // this call is a no-op on older versions.
2950- #if TARGET_OS_IPHONE || (TARGET_OS_OSX && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 )
29512964 // Pass the user-provided text through explicit %s formatting
29522965 // to avoid % literals being interpreted as a formatting directive.
29532966 os_log_with_type (OS_LOG_DEFAULT , logtype , "%s" , text );
2954- #endif
29552967 Py_RETURN_NONE ;
29562968}
29572969
@@ -2986,7 +2998,6 @@ init_apple_streams(PyThreadState *tstate)
29862998 if (result == NULL ) {
29872999 goto error ;
29883000 }
2989-
29903001 goto done ;
29913002
29923003error :
@@ -3000,7 +3011,7 @@ init_apple_streams(PyThreadState *tstate)
30003011 return status ;
30013012}
30023013
3003- #endif // __APPLE__
3014+ #endif // __APPLE__ && HAS_APPLE_SYSTEM_LOG
30043015
30053016
30063017static void
0 commit comments