exception.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "exception.hh"
  19. #ifdef USE_EXECINFO
  20. #include <execinfo.h>
  21. #endif
  22. #include <errno.h>
  23. #include <sstream>
  24. #include <typeinfo>
  25. using std::string;
  26. namespace hadoop {
  27. /**
  28. * Create an exception.
  29. * @param message The message to give to the user.
  30. * @param reason The exception that caused the new exception.
  31. */
  32. Exception::Exception(const string& message,
  33. const string& component,
  34. const string& location,
  35. const Exception* reason
  36. ): mMessage(message),
  37. mComponent(component),
  38. mLocation(location),
  39. mReason(reason)
  40. {
  41. #ifdef USE_EXECINFO
  42. mCalls = backtrace(mCallStack, sMaxCallStackDepth);
  43. #else
  44. mCalls = 0;
  45. #endif
  46. }
  47. /**
  48. * Copy the exception.
  49. * Clones the reason, if there is one.
  50. */
  51. Exception::Exception(const Exception& other
  52. ): mMessage(other.mMessage),
  53. mComponent(other.mComponent),
  54. mLocation(other.mLocation),
  55. mCalls(other.mCalls)
  56. {
  57. for(int i=0; i < mCalls; ++i) {
  58. mCallStack[i] = other.mCallStack[i];
  59. }
  60. if (other.mReason) {
  61. mReason = other.mReason->clone();
  62. } else {
  63. mReason = NULL;
  64. }
  65. }
  66. Exception::~Exception() throw () {
  67. delete mReason;
  68. }
  69. /**
  70. * Print all of the information about the exception.
  71. */
  72. void Exception::print(std::ostream& stream) const {
  73. stream << "Exception " << getTypename();
  74. if (mComponent.size() != 0) {
  75. stream << " (" << mComponent << ")";
  76. }
  77. stream << ": " << mMessage << "\n";
  78. if (mLocation.size() != 0) {
  79. stream << " thrown at " << mLocation << "\n";
  80. }
  81. #ifdef USE_EXECINFO
  82. printCallStack(stream);
  83. #endif
  84. if (mReason) {
  85. stream << "caused by: ";
  86. mReason->print(stream);
  87. }
  88. stream.flush();
  89. }
  90. /**
  91. * Result of print() as a string.
  92. */
  93. string Exception::toString() const {
  94. std::ostringstream stream;
  95. print(stream);
  96. return stream.str();
  97. }
  98. #ifdef USE_EXECINFO
  99. /**
  100. * Print the call stack where the exception was created.
  101. */
  102. void Exception::printCallStack(std::ostream& stream) const {
  103. char ** symbols = backtrace_symbols(mCallStack, mCalls);
  104. for(int i=0; i < mCalls; ++i) {
  105. stream << " ";
  106. if (i == 0) {
  107. stream << "at ";
  108. } else {
  109. stream << "from ";
  110. }
  111. stream << symbols[i] << "\n";
  112. }
  113. free(symbols);
  114. }
  115. #endif
  116. const char* Exception::getTypename() const {
  117. return "Exception";
  118. }
  119. Exception* Exception::clone() const {
  120. return new Exception(*this);
  121. }
  122. IOException::IOException(const string& message,
  123. const string& component,
  124. const string& location,
  125. const Exception* reason
  126. ): Exception(message, component, location, reason)
  127. {
  128. }
  129. const char* IOException::getTypename() const {
  130. return "IOException";
  131. }
  132. IOException* IOException::clone() const {
  133. return new IOException(*this);
  134. }
  135. }