| 1 | // |
| 2 | // Bugcheck.cpp |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Core |
| 6 | // Module: Bugcheck |
| 7 | // |
| 8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Bugcheck.h" |
| 16 | #include "Poco/Debugger.h" |
| 17 | #include "Poco/NestedDiagnosticContext.h" |
| 18 | #include "Poco/Exception.h" |
| 19 | #include <sstream> |
| 20 | |
| 21 | |
| 22 | namespace Poco { |
| 23 | |
| 24 | |
| 25 | void Bugcheck::assertion(const char* cond, const char* file, int line, const char* text) |
| 26 | { |
| 27 | std::string message("Assertion violation: " ); |
| 28 | message += cond; |
| 29 | if (text) |
| 30 | { |
| 31 | message += " (" ; |
| 32 | message += text; |
| 33 | message += ")" ; |
| 34 | } |
| 35 | Debugger::enter(message, file, line); |
| 36 | throw AssertionViolationException(what(cond, file, line, text)); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void Bugcheck::nullPointer(const char* ptr, const char* file, int line) |
| 41 | { |
| 42 | Debugger::enter(std::string("NULL pointer: " ) + ptr, file, line); |
| 43 | throw NullPointerException(what(ptr, file, line)); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void Bugcheck::bugcheck(const char* file, int line) |
| 48 | { |
| 49 | Debugger::enter("Bugcheck" , file, line); |
| 50 | throw BugcheckException(what(0, file, line)); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void Bugcheck::bugcheck(const char* msg, const char* file, int line) |
| 55 | { |
| 56 | std::string m("Bugcheck" ); |
| 57 | if (msg) |
| 58 | { |
| 59 | m.append(": " ); |
| 60 | m.append(msg); |
| 61 | } |
| 62 | Debugger::enter(m, file, line); |
| 63 | throw BugcheckException(what(msg, file, line)); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void Bugcheck::unexpected(const char* file, int line) |
| 68 | { |
| 69 | #ifdef _DEBUG |
| 70 | try |
| 71 | { |
| 72 | std::string msg("Unexpected exception in noexcept function or destructor: " ); |
| 73 | try |
| 74 | { |
| 75 | throw; |
| 76 | } |
| 77 | catch (Poco::Exception& exc) |
| 78 | { |
| 79 | msg += exc.displayText(); |
| 80 | } |
| 81 | catch (std::exception& exc) |
| 82 | { |
| 83 | msg += exc.what(); |
| 84 | } |
| 85 | catch (...) |
| 86 | { |
| 87 | msg += "unknown exception" ; |
| 88 | } |
| 89 | Debugger::enter(msg, file, line); |
| 90 | } |
| 91 | catch (...) |
| 92 | { |
| 93 | } |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void Bugcheck::debugger(const char* file, int line) |
| 99 | { |
| 100 | Debugger::enter(file, line); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void Bugcheck::debugger(const char* msg, const char* file, int line) |
| 105 | { |
| 106 | Debugger::enter(msg, file, line); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | std::string Bugcheck::what(const char* msg, const char* file, int line, const char* text) |
| 111 | { |
| 112 | std::ostringstream str; |
| 113 | if (msg) str << msg << " " ; |
| 114 | if (text != NULL) str << "(" << text << ") " ; |
| 115 | str << "in file \"" << file << "\", line " << line; |
| 116 | #ifdef POCO_EXCEPTION_BACKTRACE |
| 117 | str << std::endl << NDC::backtrace(3, 2); |
| 118 | #endif // POCO_EXCEPTION_BACKTRACE |
| 119 | return str.str(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | } // namespace Poco |
| 124 | |