1 | //===-- DNBError.cpp --------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Created by Greg Clayton on 6/26/07. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "DNBError.h" |
14 | #include "CFString.h" |
15 | #include "DNBLog.h" |
16 | |
17 | #ifdef WITH_SPRINGBOARD |
18 | #include <SpringBoardServices/SpringBoardServer.h> |
19 | #endif |
20 | |
21 | const char *DNBError::AsString() const { |
22 | if (Success()) |
23 | return NULL; |
24 | |
25 | if (m_str.empty()) { |
26 | const char *s = NULL; |
27 | switch (m_flavor) { |
28 | case MachKernel: |
29 | s = ::mach_error_string(m_err); |
30 | break; |
31 | |
32 | case POSIX: |
33 | s = ::strerror(m_err); |
34 | break; |
35 | |
36 | #ifdef WITH_SPRINGBOARD |
37 | case SpringBoard: { |
38 | CFStringRef statusStr = SBSApplicationLaunchingErrorString(m_err); |
39 | if (CFString::UTF8(statusStr, m_str) == NULL) |
40 | m_str.clear(); |
41 | } break; |
42 | #endif |
43 | #ifdef WITH_BKS |
44 | case BackBoard: { |
45 | // You have to call ObjC routines to get the error string from |
46 | // BackBoardServices. |
47 | // Not sure I want to make DNBError.cpp an .mm file. For now just make |
48 | // sure you |
49 | // pre-populate the error string when you make the DNBError of type |
50 | // BackBoard. |
51 | m_str.assign( |
52 | "Should have set BackBoard error when making the error string." ); |
53 | } break; |
54 | #endif |
55 | #ifdef WITH_FBS |
56 | case FrontBoard: { |
57 | // You have to call ObjC routines to get the error string from |
58 | // FrontBoardServices. |
59 | // Not sure I want to make DNBError.cpp an .mm file. For now just make |
60 | // sure you |
61 | // pre-populate the error string when you make the DNBError of type |
62 | // FrontBoard. |
63 | m_str.assign( |
64 | "Should have set FrontBoard error when making the error string." ); |
65 | } break; |
66 | #endif |
67 | default: |
68 | break; |
69 | } |
70 | if (s) |
71 | m_str.assign(s: s); |
72 | } |
73 | if (m_str.empty()) |
74 | return NULL; |
75 | return m_str.c_str(); |
76 | } |
77 | |
78 | void DNBError::LogThreadedIfError(const char *format, ...) const { |
79 | if (Fail()) { |
80 | char *arg_msg = NULL; |
81 | va_list args; |
82 | va_start(args, format); |
83 | ::vasprintf(ptr: &arg_msg, f: format, arg: args); |
84 | va_end(args); |
85 | |
86 | if (arg_msg != NULL) { |
87 | const char *err_str = AsString(); |
88 | if (err_str == NULL) |
89 | err_str = "???" ; |
90 | DNBLogThreaded("error: %s err = %s (0x%8.8x)" , arg_msg, err_str, m_err); |
91 | free(ptr: arg_msg); |
92 | } |
93 | } |
94 | } |
95 | |
96 | void DNBError::LogThreaded(const char *format, ...) const { |
97 | char *arg_msg = NULL; |
98 | va_list args; |
99 | va_start(args, format); |
100 | ::vasprintf(ptr: &arg_msg, f: format, arg: args); |
101 | va_end(args); |
102 | |
103 | if (arg_msg != NULL) { |
104 | if (Fail()) { |
105 | const char *err_str = AsString(); |
106 | if (err_str == NULL) |
107 | err_str = "???" ; |
108 | DNBLogThreaded("error: %s err = %s (0x%8.8x)" , arg_msg, err_str, m_err); |
109 | } else { |
110 | DNBLogThreaded("%s err = 0x%8.8x" , arg_msg, m_err); |
111 | } |
112 | free(ptr: arg_msg); |
113 | } |
114 | } |
115 | |