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 #ifdef WITH_FBS
38         , FrontBoard = 5
39 #endif
40     } FlavorType;
41 
42     explicit DNBError(    ValueType err = 0,
43                             FlavorType flavor = Generic) :
44         m_err(err),
45         m_flavor(flavor)
46     {
47     }
48 
49     const char * AsString() const;
50     void Clear() { m_err = 0; m_flavor = Generic; m_str.clear(); }
51     ValueType Error() const { return m_err; }
52     FlavorType Flavor() const { return m_flavor; }
53 
54     ValueType operator = (kern_return_t err)
55     {
56         m_err = err;
57         m_flavor = MachKernel;
58         m_str.clear();
59         return m_err;
60     }
61 
62     void SetError(kern_return_t err)
63     {
64         m_err = err;
65         m_flavor = MachKernel;
66         m_str.clear();
67     }
68 
69     void SetErrorToErrno()
70     {
71         m_err = errno;
72         m_flavor = POSIX;
73         m_str.clear();
74     }
75 
76     void SetError(ValueType err, FlavorType flavor)
77     {
78         m_err = err;
79         m_flavor = flavor;
80         m_str.clear();
81     }
82 
83     // Generic errors can set their own string values
84     void SetErrorString(const char *err_str)
85     {
86         if (err_str && err_str[0])
87             m_str = err_str;
88         else
89             m_str.clear();
90     }
91     bool Success() const { return m_err == 0; }
92     bool Fail() const { return m_err != 0; }
93     void LogThreadedIfError(const char *format, ...) const;
94     void LogThreaded(const char *format, ...) const;
95 protected:
96     ValueType    m_err;
97     FlavorType    m_flavor;
98     mutable std::string m_str;
99 };
100 
101 
102 #endif    // #ifndef __DNBError_h__
103