blob: c9d8ebd58d89463a74a5907b2077a4fd287f7029 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Jason Molenda42999a42012-02-22 02:18:59 +000019#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include <SpringBoardServices/SpringBoardServer.h>
21#endif
22
23const char *
24DNBError::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
Jason Molenda42999a42012-02-22 02:18:59 +000042#ifdef WITH_SPRINGBOARD
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043 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
Jason Molendaa3329782014-03-29 18:54:20 +000051#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.
Jason Molendac611a742015-10-23 02:49:51 +000057 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.");
Jason Molendaa3329782014-03-29 18:54:20 +000068 }
69 break;
70#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 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
82void
83DNBError::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
104void
105DNBError::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}