ctkException.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkException.h"
  16. #include <typeinfo>
  17. #include <QDebug>
  18. // --------------------------------------------------------------------------
  19. ctkException::TraceManipulator::TraceManipulator(const ctkException* e)
  20. : Exc(e)
  21. {
  22. }
  23. // --------------------------------------------------------------------------
  24. QDebug ctkException::TraceManipulator::print(QDebug dbg) const
  25. {
  26. if (Exc)
  27. Exc->printStackTrace(dbg);
  28. return dbg.maybeSpace();
  29. }
  30. // --------------------------------------------------------------------------
  31. ctkException::ctkException(const QString& msg)
  32. : Msg(msg), NestedException(0)
  33. {
  34. }
  35. // --------------------------------------------------------------------------
  36. ctkException::ctkException(const QString& msg, const ctkException& cause)
  37. : Msg(msg), NestedException(cause.clone())
  38. {
  39. }
  40. // --------------------------------------------------------------------------
  41. ctkException::ctkException(const ctkException& exc)
  42. : std::exception(exc), ctkBackTrace(exc), Msg(exc.Msg)
  43. {
  44. NestedException = exc.NestedException ? exc.NestedException->clone() : 0;
  45. }
  46. // --------------------------------------------------------------------------
  47. ctkException::~ctkException() throw()
  48. {
  49. delete NestedException;
  50. }
  51. // --------------------------------------------------------------------------
  52. ctkException& ctkException::operator=(const ctkException& exc)
  53. {
  54. if (&exc != this)
  55. {
  56. delete NestedException;
  57. Msg = exc.Msg;
  58. NestedException = exc.NestedException ? exc.NestedException->clone() : 0;
  59. }
  60. return *this;
  61. }
  62. // --------------------------------------------------------------------------
  63. const ctkException* ctkException::cause() const throw()
  64. {
  65. return NestedException;
  66. }
  67. // --------------------------------------------------------------------------
  68. void ctkException::setCause(const ctkException& cause)
  69. {
  70. delete NestedException;
  71. NestedException = cause.clone();
  72. }
  73. // --------------------------------------------------------------------------
  74. const char *ctkException::name() const throw()
  75. {
  76. return "ctkException";
  77. }
  78. // --------------------------------------------------------------------------
  79. const char* ctkException::className() const throw()
  80. {
  81. return typeid(*this).name();
  82. }
  83. // --------------------------------------------------------------------------
  84. const char* ctkException::what() const throw()
  85. {
  86. static std::string txt;
  87. txt = std::string(name());
  88. if (!Msg.isEmpty())
  89. {
  90. txt += ": ";
  91. txt += Msg.toStdString();
  92. }
  93. return txt.c_str();
  94. }
  95. // --------------------------------------------------------------------------
  96. QString ctkException::message() const throw()
  97. {
  98. return Msg;
  99. }
  100. // --------------------------------------------------------------------------
  101. ctkException::TraceManipulator ctkException::printStackTrace() const
  102. {
  103. return TraceManipulator(this);
  104. }
  105. // --------------------------------------------------------------------------
  106. ctkException* ctkException::clone() const
  107. {
  108. return new ctkException(*this);
  109. }
  110. // --------------------------------------------------------------------------
  111. void ctkException::rethrow() const
  112. {
  113. throw *this;
  114. }
  115. // --------------------------------------------------------------------------
  116. QDebug ctkException::printStackTrace(QDebug dbg) const
  117. {
  118. QSet<const ctkException*> dejaVu;
  119. dejaVu.insert(this);
  120. // Print our stack trace
  121. dbg.nospace() << this->what() << '\n';
  122. QList<QString> trace = stackTrace();
  123. foreach(QString traceElement, trace)
  124. {
  125. dbg.nospace() << "\tat " << qPrintable(traceElement) << '\n';
  126. }
  127. // Print cause, if any
  128. if (NestedException)
  129. {
  130. NestedException->printEnclosedStackTrace(dbg, trace, "Caused by: ", "", dejaVu);
  131. }
  132. return dbg;
  133. }
  134. // --------------------------------------------------------------------------
  135. void ctkException::printEnclosedStackTrace(QDebug dbg, const QList<QString>& enclosingTrace,
  136. const QString& caption, const QString& prefix,
  137. QSet<const ctkException*>& dejaVu)
  138. {
  139. if (dejaVu.contains(this))
  140. {
  141. dbg.nospace() << "\t[CIRCULAR REFERENCE:" << this->what() << "]\n";
  142. }
  143. else
  144. {
  145. dejaVu.insert(this);
  146. // Compute number of frames in common between this and enclosing trace
  147. QList<QString> trace = stackTrace();
  148. int m = trace.size() - 1;
  149. int n = enclosingTrace.size() - 1;
  150. while (m >= 0 && n >=0 && trace[m] == enclosingTrace[n])
  151. {
  152. m--; n--;
  153. }
  154. int framesInCommon = trace.size() - 1 - m;
  155. // Print our stack trace
  156. dbg.nospace() << qPrintable(prefix) << qPrintable(caption) << this->what() << '\n';
  157. for (int i = 0; i <= m; i++)
  158. {
  159. dbg.nospace() << qPrintable(prefix) << "\tat " << qPrintable(trace[i]) << '\n';
  160. }
  161. if (framesInCommon != 0)
  162. {
  163. dbg.nospace() << qPrintable(prefix) << "\t... " << framesInCommon << " more\n";
  164. }
  165. // Print cause, if any
  166. if (NestedException)
  167. {
  168. NestedException->printEnclosedStackTrace(dbg, trace, "Caused by: ", prefix, dejaVu);
  169. }
  170. }
  171. }
  172. //----------------------------------------------------------------------------
  173. QDebug operator<<(QDebug dbg, const ctkException& exc)
  174. {
  175. dbg << exc.what();
  176. return dbg.maybeSpace();
  177. }
  178. //----------------------------------------------------------------------------
  179. QDebug operator<<(QDebug dbg, const ctkException::TraceManipulator& trace)
  180. {
  181. return trace.print(dbg);
  182. }
  183. CTK_IMPLEMENT_EXCEPTION(ctkRuntimeException, ctkException, "ctkRuntimeException")
  184. CTK_IMPLEMENT_EXCEPTION(ctkInvalidArgumentException, ctkRuntimeException, "ctkInvalidArgumentException")
  185. CTK_IMPLEMENT_EXCEPTION(ctkIllegalStateException, ctkRuntimeException, "ctkIllegalStateException")