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