/src/quantlib/ql/errors.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2003, 2004, 2005 StatPro Italia srl |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <http://quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | #include <ql/errors.hpp> |
21 | | #include <stdexcept> |
22 | | |
23 | | namespace { |
24 | | |
25 | | #if defined(_MSC_VER) |
26 | | // allow Visual Studio integration |
27 | | std::string format_error( |
28 | | #ifdef QL_ERROR_LINES |
29 | | const std::string& file, long line, |
30 | | #else |
31 | | const std::string&, long, |
32 | | #endif |
33 | | #ifdef QL_ERROR_FUNCTIONS |
34 | | const std::string& function, |
35 | | #else |
36 | | const std::string&, |
37 | | #endif |
38 | | const std::string& message) { |
39 | | std::ostringstream msg; |
40 | | #ifdef QL_ERROR_FUNCTIONS |
41 | | if (function != "(unknown)") |
42 | | msg << function << ": "; |
43 | | #endif |
44 | | #ifdef QL_ERROR_LINES |
45 | | msg << "\n " << file << "(" << line << "): \n"; |
46 | | #endif |
47 | | msg << message; |
48 | | return msg.str(); |
49 | | } |
50 | | #else |
51 | | // use gcc format (e.g. for integration with Emacs) |
52 | | std::string format_error(const std::string& file, long line, |
53 | | const std::string& function, |
54 | 478 | const std::string& message) { |
55 | 478 | std::ostringstream msg; |
56 | | #ifdef QL_ERROR_LINES |
57 | | msg << "\n" << file << ":" << line << ": "; |
58 | | #endif |
59 | | #ifdef QL_ERROR_FUNCTIONS |
60 | | if (function != "(unknown)") |
61 | | msg << "In function `" << function << "': \n"; |
62 | | #endif |
63 | 478 | msg << message; |
64 | 478 | return msg.str(); |
65 | 478 | } |
66 | | #endif |
67 | | |
68 | | } |
69 | | |
70 | | namespace boost { |
71 | | |
72 | | // must be defined by the user |
73 | | void assertion_failed(char const * expr, char const * function, |
74 | 0 | char const * file, long line) { |
75 | 0 | throw std::runtime_error(format_error(file, line, function, |
76 | 0 | "Boost assertion failed: " + |
77 | 0 | std::string(expr))); |
78 | 0 | } |
79 | | |
80 | | void assertion_failed_msg(char const * expr, char const * msg, |
81 | | char const * function, char const * file, |
82 | 0 | long line) { |
83 | 0 | throw std::runtime_error(format_error(file, line, function, |
84 | 0 | "Boost assertion failed: " + |
85 | 0 | std::string(expr) + ": " + |
86 | 0 | std::string(msg))); |
87 | 0 | } |
88 | | |
89 | | } |
90 | | |
91 | | namespace QuantLib { |
92 | | |
93 | | Error::Error(const std::string& file, long line, |
94 | | const std::string& function, |
95 | 478 | const std::string& message) { |
96 | 478 | message_ = ext::make_shared<std::string>( |
97 | 478 | format_error(file, line, function, message)); |
98 | 478 | } |
99 | | |
100 | 0 | const char* Error::what() const noexcept { return message_->c_str(); } |
101 | | } |
102 | | |