21
21
# include < fcntl.h>
22
22
#else
23
23
# include < stdio.h>
24
+ # include < unistd.h>
24
25
#endif
25
26
27
+ #include < stdarg.h>
28
+
29
+ #if defined(__APPLE__)
30
+ #include < asl.h>
31
+ #elif defined(__ANDROID__)
32
+ #include < android/log.h>
33
+ #endif
34
+
35
+ namespace {
36
+ void error (const char *fmt, ...) {
37
+ char buffer[1024 ];
38
+ va_list argp;
39
+
40
+ va_start (argp, fmt);
41
+ vsnprintf (buffer, sizeof (buffer), fmt, argp);
42
+ va_end (argp);
43
+
44
+ #if defined(__APPLE__)
45
+ asl_log (nullptr , nullptr , ASL_LEVEL_ERR, " %s" , buffer);
46
+ #elif defined(__ANDROID__)
47
+ __android_log_printf (ANDROID_LOG_FATAL, " SwiftRuntime" , " %s" , buffer);
48
+ #elif defined(_WIN32)
49
+ #define STDERR_FILENO 2
50
+ _write (STDERR_FILENO, buffer, strlen (buffer));
51
+ #else
52
+ write (STDERR_FILENO, buffer, strlen (buffer));
53
+ #endif
54
+ }
55
+ }
56
+
26
57
using namespace llvm ;
27
58
28
59
void __swift::__runtime::llvm::report_fatal_error (const char *Reason,
29
60
bool GenCrashDiag) {
30
- report_fatal_error (std::string (Reason), GenCrashDiag);
61
+ error (" LLVM ERROR: %s\n " , Reason);
62
+ abort ();
31
63
}
32
64
33
65
void __swift::__runtime::llvm::report_fatal_error (const std::string &Reason,
34
66
bool GenCrashDiag) {
35
- // Blast the result out to stderr. We don't try hard to make sure this
36
- // succeeds (e.g. handling EINTR) and we can't use errs() here because
37
- // raw ostreams can call report_fatal_error.
38
- fprintf (stderr, " LLVM ERROR: %s\n " , Reason.c_str ());
39
- abort ();
67
+ report_fatal_error (Reason.c_str (), GenCrashDiag);
40
68
}
41
69
42
70
void __swift::__runtime::llvm::report_fatal_error (StringRef Reason,
@@ -48,7 +76,7 @@ void __swift::__runtime::llvm::report_bad_alloc_error(const char *Reason,
48
76
bool GenCrashDiag) {
49
77
// Don't call the normal error handler. It may allocate memory. Directly write
50
78
// an OOM to stderr and abort.
51
- fprintf (stderr, " LLVM ERROR: out of memory\n " );
79
+ error ( " LLVM ERROR: out of memory\n " );
52
80
abort ();
53
81
}
54
82
@@ -58,11 +86,11 @@ void __swift::__runtime::llvm::llvm_unreachable_internal(
58
86
// llvm_unreachable is intended to be used to indicate "impossible"
59
87
// situations, and not legitimate runtime errors.
60
88
if (msg)
61
- fprintf (stderr, " %s\n " , msg);
62
- fprintf (stderr, " UNREACHABLE executed" );
89
+ error ( " %s\n " , msg);
90
+ error ( " UNREACHABLE executed" );
63
91
if (file)
64
- fprintf (stderr, " at %s:%u" , file, line);
65
- fprintf (stderr, " !\n " );
92
+ error ( " at %s:%u" , file, line);
93
+ error ( " !\n " );
66
94
abort ();
67
95
#ifdef LLVM_BUILTIN_UNREACHABLE
68
96
// Windows systems and possibly others don't declare abort() to be noreturn,
0 commit comments