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 public: 24 typedef uint32_t ValueType; 25 typedef enum { 26 Generic = 0, 27 MachKernel = 1, 28 POSIX = 2 29 #ifdef WITH_SPRINGBOARD 30 , 31 SpringBoard = 3 32 #endif 33 #ifdef WITH_BKS 34 , 35 BackBoard = 4 36 #endif 37 #ifdef WITH_FBS 38 , 39 FrontBoard = 5 40 #endif 41 } FlavorType; 42 43 explicit DNBError(ValueType err = 0, FlavorType flavor = Generic) 44 : m_err(err), m_flavor(flavor) {} 45 46 const char *AsString() const; 47 void Clear() { 48 m_err = 0; 49 m_flavor = Generic; 50 m_str.clear(); 51 } 52 ValueType Error() const { return m_err; } 53 FlavorType Flavor() const { return m_flavor; } 54 55 ValueType operator=(kern_return_t err) { 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 m_err = err; 64 m_flavor = MachKernel; 65 m_str.clear(); 66 } 67 68 void SetErrorToErrno() { 69 m_err = errno; 70 m_flavor = POSIX; 71 m_str.clear(); 72 } 73 74 void SetError(ValueType err, FlavorType flavor) { 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 if (err_str && err_str[0]) 83 m_str = err_str; 84 else 85 m_str.clear(); 86 } 87 bool Success() const { return m_err == 0; } 88 bool Fail() const { return m_err != 0; } 89 void LogThreadedIfError(const char *format, ...) const; 90 void LogThreaded(const char *format, ...) const; 91 92 protected: 93 ValueType m_err; 94 FlavorType m_flavor; 95 mutable std::string m_str; 96 }; 97 98 #endif // #ifndef __DNBError_h__ 99