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 *
AsString() const24 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         default:
52             break;
53         }
54         if (s)
55             m_str.assign(s);
56     }
57     if (m_str.empty())
58         return NULL;
59     return m_str.c_str();
60 }
61 
62 void
LogThreadedIfError(const char * format,...) const63 DNBError::LogThreadedIfError(const char *format, ...) const
64 {
65     if (Fail())
66     {
67         char *arg_msg = NULL;
68         va_list args;
69         va_start (args, format);
70         ::vasprintf (&arg_msg, format, args);
71         va_end (args);
72 
73         if (arg_msg != NULL)
74         {
75             const char *err_str = AsString();
76             if (err_str == NULL)
77                 err_str = "???";
78             DNBLogThreaded ("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);
79             free (arg_msg);
80         }
81     }
82 }
83 
84 void
LogThreaded(const char * format,...) const85 DNBError::LogThreaded(const char *format, ...) const
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         if (Fail())
96         {
97             const char *err_str = AsString();
98             if (err_str == NULL)
99                 err_str = "???";
100             DNBLogThreaded ("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);
101         }
102         else
103         {
104             DNBLogThreaded ("%s err = 0x%8.8x", arg_msg, m_err);
105         }
106         free (arg_msg);
107     }
108 }
109