1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "exception.hh" |
---|
20 | #ifdef USE_EXECINFO |
---|
21 | #include <execinfo.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <errno.h> |
---|
25 | #include <sstream> |
---|
26 | #include <typeinfo> |
---|
27 | |
---|
28 | using std::string; |
---|
29 | |
---|
30 | namespace hadoop { |
---|
31 | |
---|
32 | /** |
---|
33 | * Create an exception. |
---|
34 | * @param message The message to give to the user. |
---|
35 | * @param reason The exception that caused the new exception. |
---|
36 | */ |
---|
37 | Exception::Exception(const string& message, |
---|
38 | const string& component, |
---|
39 | const string& location, |
---|
40 | const Exception* reason |
---|
41 | ): mMessage(message), |
---|
42 | mComponent(component), |
---|
43 | mLocation(location), |
---|
44 | mReason(reason) |
---|
45 | |
---|
46 | { |
---|
47 | #ifdef USE_EXECINFO |
---|
48 | mCalls = backtrace(mCallStack, sMaxCallStackDepth); |
---|
49 | #else |
---|
50 | mCalls = 0; |
---|
51 | #endif |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Copy the exception. |
---|
56 | * Clones the reason, if there is one. |
---|
57 | */ |
---|
58 | Exception::Exception(const Exception& other |
---|
59 | ): mMessage(other.mMessage), |
---|
60 | mComponent(other.mComponent), |
---|
61 | mLocation(other.mLocation), |
---|
62 | mCalls(other.mCalls) |
---|
63 | { |
---|
64 | for(int i=0; i < mCalls; ++i) { |
---|
65 | mCallStack[i] = other.mCallStack[i]; |
---|
66 | } |
---|
67 | if (other.mReason) { |
---|
68 | mReason = other.mReason->clone(); |
---|
69 | } else { |
---|
70 | mReason = NULL; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | Exception::~Exception() throw () { |
---|
75 | delete mReason; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Print all of the information about the exception. |
---|
80 | */ |
---|
81 | void Exception::print(std::ostream& stream) const { |
---|
82 | stream << "Exception " << getTypename(); |
---|
83 | if (mComponent.size() != 0) { |
---|
84 | stream << " (" << mComponent << ")"; |
---|
85 | } |
---|
86 | stream << ": " << mMessage << "\n"; |
---|
87 | if (mLocation.size() != 0) { |
---|
88 | stream << " thrown at " << mLocation << "\n"; |
---|
89 | } |
---|
90 | #ifdef USE_EXECINFO |
---|
91 | printCallStack(stream); |
---|
92 | #endif |
---|
93 | if (mReason) { |
---|
94 | stream << "caused by: "; |
---|
95 | mReason->print(stream); |
---|
96 | } |
---|
97 | stream.flush(); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Result of print() as a string. |
---|
102 | */ |
---|
103 | string Exception::toString() const { |
---|
104 | std::ostringstream stream; |
---|
105 | print(stream); |
---|
106 | return stream.str(); |
---|
107 | } |
---|
108 | |
---|
109 | #ifdef USE_EXECINFO |
---|
110 | /** |
---|
111 | * Print the call stack where the exception was created. |
---|
112 | */ |
---|
113 | void Exception::printCallStack(std::ostream& stream) const { |
---|
114 | char ** symbols = backtrace_symbols(mCallStack, mCalls); |
---|
115 | for(int i=0; i < mCalls; ++i) { |
---|
116 | stream << " "; |
---|
117 | if (i == 0) { |
---|
118 | stream << "at "; |
---|
119 | } else { |
---|
120 | stream << "from "; |
---|
121 | } |
---|
122 | stream << symbols[i] << "\n"; |
---|
123 | } |
---|
124 | free(symbols); |
---|
125 | } |
---|
126 | #endif |
---|
127 | |
---|
128 | const char* Exception::getTypename() const { |
---|
129 | return "Exception"; |
---|
130 | } |
---|
131 | |
---|
132 | Exception* Exception::clone() const { |
---|
133 | return new Exception(*this); |
---|
134 | } |
---|
135 | |
---|
136 | IOException::IOException(const string& message, |
---|
137 | const string& component, |
---|
138 | const string& location, |
---|
139 | const Exception* reason |
---|
140 | ): Exception(message, component, location, reason) |
---|
141 | { |
---|
142 | } |
---|
143 | |
---|
144 | const char* IOException::getTypename() const { |
---|
145 | return "IOException"; |
---|
146 | } |
---|
147 | |
---|
148 | IOException* IOException::clone() const { |
---|
149 | return new IOException(*this); |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|