1 //===-- DNBError.h ----------------------------------------------*- 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 #ifndef __DNBError_h__
15 #define __DNBError_h__
16 
17 #include <errno.h>
18 #include <mach/mach.h>
19 #include <stdio.h>
20 #include <string>
21 
22 class DNBError
23 {
24 public:
25     typedef uint32_t ValueType;
26     typedef enum
27     {
28         Generic = 0,
29         MachKernel = 1,
30         POSIX = 2
31 #ifdef WITH_SPRINGBOARD
32         , SpringBoard = 3
33 #endif
34 #ifdef WITH_BKS
35         , BackBoard = 4
36 #endif
37     } FlavorType;
38 
39     explicit DNBError(    ValueType err = 0,
40                             FlavorType flavor = Generic) :
41         m_err(err),
42         m_flavor(flavor)
43     {
44     }
45 
46     const char * AsString() const;
47     void Clear() { m_err = 0; m_flavor = Generic; m_str.clear(); }
48     ValueType Error() const { return m_err; }
49     FlavorType Flavor() const { return m_flavor; }
50 
51     ValueType operator = (kern_return_t err)
52     {
53         m_err = err;
54         m_flavor = MachKernel;
55         m_str.clear();
56         return m_err;
57     }
58 
59     void SetError(kern_return_t err)
60     {
61         m_err = err;
62         m_flavor = MachKernel;
63         m_str.clear();
64     }
65 
66     void SetErrorToErrno()
67     {
68         m_err = errno;
69         m_flavor = POSIX;
70         m_str.clear();
71     }
72 
73     void SetError(ValueType err, FlavorType flavor)
74     {
75         m_err = err;
76         m_flavor = flavor;
77         m_str.clear();
78     }
79 
80     // Generic errors can set their own string values
81     void SetErrorString(const char *err_str)
82     {
83         if (err_str && err_str[0])
84             m_str = err_str;
85         else
86             m_str.clear();
87     }
88     bool Success() const { return m_err == 0; }
89     bool Fail() const { return m_err != 0; }
90     void LogThreadedIfError(const char *format, ...) const;
91     void LogThreaded(const char *format, ...) const;
92 protected:
93     ValueType    m_err;
94     FlavorType    m_flavor;
95     mutable std::string m_str;
96 };
97 
98 
99 #endif    // #ifndef __DNBError_h__
100