Skip to content

Commit 6aacf5e

Browse files
authored
Re-land: Use os_log instead of syslog on Apple platforms (flutter#16549)
Migrates to using os_log for app logging on iOS 13 and above, macOS 10.11 and above. On older platform, fall back to syslog(), which is what we used previously. This re-lands commit 78a8909 with a fix. That commit was reverted in a61dbf2.
1 parent 5041ff1 commit 6aacf5e

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

β€Žfml/logging.ccβ€Ž

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
#if defined(OS_ANDROID)
1313
#include <android/log.h>
14-
#elif defined(OS_IOS)
14+
#elif defined(OS_MACOSX)
15+
#include <os/log.h>
1516
#include <syslog.h>
1617
#endif
1718

@@ -81,8 +82,37 @@ LogMessage::~LogMessage() {
8182
break;
8283
}
8384
__android_log_write(priority, "flutter", stream_.str().c_str());
84-
#elif defined(OS_IOS)
85-
syslog(LOG_ALERT, "%s", stream_.str().c_str());
85+
#elif defined(OS_MACOSX)
86+
const char* chars = stream_.str().c_str();
87+
// Unified logging (os_log) became available in iOS 9.0 and syslog stopped
88+
// working in iOS 13.0. idevicesyslog made device syslog available on the
89+
// connected host, but there is no known API to view device unified logging on
90+
// the host. Flutter tool will continue to observe syslog on devices older
91+
// than iOS 13.0 since it provides more logging context, particularly for
92+
// application crashes.
93+
if (__builtin_available(iOS 13.0, macOS 10.11, *)) {
94+
os_log_t engine_log = os_log_create("io.flutter", "engine");
95+
switch (severity_) {
96+
// TODO(flutter/flutter#45931): LogSeverity LOG_INFO and LOG_WARNING
97+
// collide with syslog log level macros.
98+
case 0 /* LOG_INFO */:
99+
os_log_debug(engine_log, "%s", chars);
100+
break;
101+
case 1 /* LOG_WARNING */:
102+
case LOG_ERROR:
103+
os_log_error(engine_log, "%s", chars);
104+
break;
105+
case LOG_FATAL:
106+
os_log_fault(engine_log, "%s", chars);
107+
break;
108+
default:
109+
os_log(engine_log, "%s", chars);
110+
break;
111+
}
112+
} else {
113+
syslog(LOG_ALERT, "%s", chars);
114+
}
115+
86116
#else
87117
std::cerr << stream_.str();
88118
std::cerr.flush();

β€Žlib/ui/dart_runtime_hooks.ccβ€Ž

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
#if defined(OS_ANDROID)
3232
#include <android/log.h>
33-
#elif defined(OS_IOS)
33+
#elif defined(OS_MACOSX)
34+
#include <os/log.h>
3435
extern "C" {
3536
// Cannot import the syslog.h header directly because of macro collision.
3637
extern void syslog(int, const char*, ...);
@@ -199,19 +200,29 @@ void Logger_PrintString(Dart_NativeArguments args) {
199200
// Write to the logcat on Android.
200201
__android_log_print(ANDROID_LOG_INFO, logger_prefix.c_str(), "%.*s",
201202
(int)length, chars);
202-
#elif defined(OS_IOS)
203-
// Write to syslog on iOS.
204-
//
203+
#elif defined(OS_MACOSX)
205204
// TODO(cbracken): replace with dedicated communication channel and bypass
206205
// iOS logging APIs altogether.
207-
syslog(1 /* LOG_ALERT */, "%.*s", (int)length, chars);
206+
//
207+
// Unified logging (os_log) became available in iOS 9.0 and syslog stopped
208+
// working in iOS 13.0. idevicesyslog made device syslog available on the
209+
// connected host, but there is no known API to view device unified logging
210+
// on the host. Flutter tool will continue to observe syslog on devices
211+
// older than iOS 13.0 since it provides more logging context, particularly
212+
// for application crashes.
213+
if (__builtin_available(iOS 13.0, macOS 10.11, *)) {
214+
os_log_t dart_log = os_log_create("io.flutter", "dart");
215+
os_log(dart_log, "%.*s", static_cast<int>(length), chars);
216+
} else {
217+
syslog(1 /* LOG_ALERT */, "%.*s", static_cast<int>(length), chars);
218+
}
208219
#else
209220
std::cout << log_string << std::endl;
210221
#endif
211222
}
212223

213224
if (dart::bin::ShouldCaptureStdout()) {
214-
// For now we report print output on the Stdout stream.
225+
// Report print output on the Stdout stream.
215226
uint8_t newline[] = {'\n'};
216227
Dart_ServiceSendDataEvent("Stdout", "WriteEvent",
217228
reinterpret_cast<const uint8_t*>(chars), length);

0 commit comments

Comments
 (0)