]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/debugserver/source/DNBError.cpp
Vendor import of lldb trunk r256945:
[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 *
24 DNBError::AsString() const
25 {
26     if (Success())
27         return NULL;
28
29     if (m_str.empty())
30     {
31         const char *s = NULL;
32         switch (m_flavor)
33         {
34         case MachKernel:
35             s = ::mach_error_string (m_err);
36             break;
37
38         case POSIX:
39             s = ::strerror (m_err);
40             break;
41
42 #ifdef WITH_SPRINGBOARD
43         case SpringBoard:
44             {
45                 CFStringRef statusStr = SBSApplicationLaunchingErrorString (m_err);
46                 if (CFString::UTF8 (statusStr, m_str) == NULL)
47                     m_str.clear();
48             }
49             break;
50 #endif
51 #ifdef WITH_BKS
52         case BackBoard:
53             {
54                 // You have to call ObjC routines to get the error string from BackBoardServices.
55                 // Not sure I want to make DNBError.cpp an .mm file.  For now just make sure you
56                 // pre-populate the error string when you make the DNBError of type BackBoard.
57                 m_str.assign("Should have set BackBoard error when making the error string.");
58             }
59             break;
60 #endif
61 #ifdef WITH_FBS
62         case FrontBoard:
63             {
64                 // You have to call ObjC routines to get the error string from FrontBoardServices.
65                 // Not sure I want to make DNBError.cpp an .mm file.  For now just make sure you
66                 // pre-populate the error string when you make the DNBError of type FrontBoard.
67                 m_str.assign("Should have set FrontBoard error when making the error string.");
68             }
69             break;
70 #endif
71         default:
72             break;
73         }
74         if (s)
75             m_str.assign(s);
76     }
77     if (m_str.empty())
78         return NULL;
79     return m_str.c_str();
80 }
81
82 void
83 DNBError::LogThreadedIfError(const char *format, ...) const
84 {
85     if (Fail())
86     {
87         char *arg_msg = NULL;
88         va_list args;
89         va_start (args, format);
90         ::vasprintf (&arg_msg, format, args);
91         va_end (args);
92
93         if (arg_msg != NULL)
94         {
95             const char *err_str = AsString();
96             if (err_str == NULL)
97                 err_str = "???";
98             DNBLogThreaded ("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);
99             free (arg_msg);
100         }
101     }
102 }
103
104 void
105 DNBError::LogThreaded(const char *format, ...) const
106 {
107     char *arg_msg = NULL;
108     va_list args;
109     va_start (args, format);
110     ::vasprintf (&arg_msg, format, args);
111     va_end (args);
112
113     if (arg_msg != NULL)
114     {
115         if (Fail())
116         {
117             const char *err_str = AsString();
118             if (err_str == NULL)
119                 err_str = "???";
120             DNBLogThreaded ("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);
121         }
122         else
123         {
124             DNBLogThreaded ("%s err = 0x%8.8x", arg_msg, m_err);
125         }
126         free (arg_msg);
127     }
128 }