ctkException.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if (WhatMsg.empty())
  87. {
  88. WhatMsg = std::string(name());
  89. if (!Msg.isEmpty())
  90. {
  91. WhatMsg += ": ";
  92. WhatMsg += Msg.toStdString();
  93. }
  94. }
  95. return WhatMsg.c_str();
  96. }
  97. // --------------------------------------------------------------------------
  98. QString ctkException::message() const throw()
  99. {
  100. return Msg;
  101. }
  102. // --------------------------------------------------------------------------
  103. ctkException::TraceManipulator ctkException::printStackTrace() const
  104. {
  105. return TraceManipulator(this);
  106. }
  107. // --------------------------------------------------------------------------
  108. ctkException* ctkException::clone() const
  109. {
  110. return new ctkException(*this);
  111. }
  112. // --------------------------------------------------------------------------
  113. void ctkException::rethrow() const
  114. {
  115. throw *this;
  116. }
  117. // --------------------------------------------------------------------------
  118. QDebug ctkException::printStackTrace(QDebug dbg) const
  119. {
  120. QSet<const ctkException*> dejaVu;
  121. dejaVu.insert(this);
  122. // Print our stack trace
  123. dbg.nospace() << this->what() << '\n';
  124. QList<QString> trace = stackTrace();
  125. foreach(QString traceElement, trace)
  126. {
  127. dbg.nospace() << "\tat " << qPrintable(traceElement) << '\n';
  128. }
  129. // Print cause, if any
  130. if (NestedException)
  131. {
  132. NestedException->printEnclosedStackTrace(dbg, trace, "Caused by: ", "", dejaVu);
  133. }
  134. return dbg;
  135. }
  136. // --------------------------------------------------------------------------
  137. void ctkException::printEnclosedStackTrace(QDebug dbg, const QList<QString>& enclosingTrace,
  138. const QString& caption, const QString& prefix,
  139. QSet<const ctkException*>& dejaVu)
  140. {
  141. if (dejaVu.contains(this))
  142. {
  143. dbg.nospace() << "\t[CIRCULAR REFERENCE:" << this->what() << "]\n";
  144. }
  145. else
  146. {
  147. dejaVu.insert(this);
  148. // Compute number of frames in common between this and enclosing trace
  149. QList<QString> trace = stackTrace();
  150. int m = trace.size() - 1;
  151. int n = enclosingTrace.size() - 1;
  152. while (m >= 0 && n >=0 && trace[m] == enclosingTrace[n])
  153. {
  154. m--; n--;
  155. }
  156. int framesInCommon = trace.size() - 1 - m;
  157. // Print our stack trace
  158. dbg.nospace() << qPrintable(prefix) << qPrintable(caption) << this->what() << '\n';
  159. for (int i = 0; i <= m; i++)
  160. {
  161. dbg.nospace() << qPrintable(prefix) << "\tat " << qPrintable(trace[i]) << '\n';
  162. }
  163. if (framesInCommon != 0)
  164. {
  165. dbg.nospace() << qPrintable(prefix) << "\t... " << framesInCommon << " more\n";
  166. }
  167. // Print cause, if any
  168. if (NestedException)
  169. {
  170. NestedException->printEnclosedStackTrace(dbg, trace, "Caused by: ", prefix, dejaVu);
  171. }
  172. }
  173. }
  174. //----------------------------------------------------------------------------
  175. QDebug operator<<(QDebug dbg, const ctkException& exc)
  176. {
  177. dbg << exc.what();
  178. return dbg.maybeSpace();
  179. }
  180. //----------------------------------------------------------------------------
  181. QDebug operator<<(QDebug dbg, const ctkException::TraceManipulator& trace)
  182. {
  183. return trace.print(dbg);
  184. }
  185. CTK_IMPLEMENT_EXCEPTION(ctkUnsupportedOperationException, ctkException, "ctkUnsupportedOperationException")
  186. CTK_IMPLEMENT_EXCEPTION(ctkRuntimeException, ctkException, "ctkRuntimeException")
  187. CTK_IMPLEMENT_EXCEPTION(ctkInvalidArgumentException, ctkRuntimeException, "ctkInvalidArgumentException")
  188. CTK_IMPLEMENT_EXCEPTION(ctkIllegalStateException, ctkRuntimeException, "ctkIllegalStateException")