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