/src/rdkit/Code/RDGeneral/Invariant.cpp
Line | Count | Source |
1 | | // |
2 | | // Copyright (C) 2001-2020 Greg Landrum, Randal M. Henne, and Rational Discovery |
3 | | // LLC |
4 | | // |
5 | | // @@ All Rights Reserved @@ |
6 | | // This file is part of the RDKit. |
7 | | // The contents are covered by the terms of the BSD license |
8 | | // which is included in the file license.txt, found at the root |
9 | | // of the RDKit source tree. |
10 | | // |
11 | | |
12 | | #include "Invariant.h" |
13 | | |
14 | | #include <string> |
15 | | #include <boost/lexical_cast.hpp> |
16 | | #include "versions.h" |
17 | | |
18 | | #ifdef RDK_USE_BOOST_STACKTRACE |
19 | | #include <boost/stacktrace.hpp> |
20 | | #include <sstream> |
21 | | #endif |
22 | | |
23 | | namespace Invar { |
24 | | |
25 | 0 | std::ostream &operator<<(std::ostream &s, const Invariant &inv) { |
26 | 0 | return s << inv.toString().c_str(); |
27 | 0 | } |
28 | | |
29 | 0 | std::string Invariant::toString() const { |
30 | 0 | std::string line = std::to_string(this->getLine()); |
31 | |
|
32 | 0 | std::string stringRep = |
33 | 0 | this->prefix_d + "\n" + this->what() + "\nViolation occurred on line " + |
34 | 0 | line + " in file " + this->getFile() + |
35 | 0 | "\nFailed Expression: " + this->getExpression() + "\n"; |
36 | | #ifdef RDK_USE_BOOST_STACKTRACE |
37 | | std::stringstream sstr; |
38 | | sstr << "----------\n" |
39 | | << "Stacktrace:\n" |
40 | | << boost::stacktrace::stacktrace() << "----------\n"; |
41 | | stringRep += sstr.str(); |
42 | | #endif |
43 | 0 | return stringRep; |
44 | 0 | } |
45 | | |
46 | 0 | std::string Invariant::toUserString() const { |
47 | 0 | std::string line = std::to_string(this->getLine()); |
48 | |
|
49 | 0 | std::string filename = this->getFile(); |
50 | |
|
51 | 0 | std::size_t pos = filename.find("Code"); // strip out build directory info |
52 | 0 | if (pos != std::string::npos) { |
53 | 0 | filename = filename.substr(pos); |
54 | 0 | } |
55 | |
|
56 | 0 | std::string stringRep = this->prefix_d + "\n\t" + this->what() + |
57 | 0 | "\n\tViolation occurred on line " + line + |
58 | 0 | " in file " + filename + |
59 | 0 | "\n\tFailed Expression: " + this->getExpression() + |
60 | 0 | "\n\t" + "RDKIT: " + RDKit::rdkitVersion + "\n\t" + |
61 | 0 | "BOOST: " + RDKit::boostVersion + "\n"; |
62 | |
|
63 | | #ifdef SHOW_BACKTRACES_WITH_INVARIANT_ERRORS |
64 | | void *arr[10]; |
65 | | size_t sz; |
66 | | sz = backtrace(arr, 10); |
67 | | std::cerr << " STACK TRACE\n--------------\n" << std::endl; |
68 | | backtrace_symbols_fd(arr, sz, 2); |
69 | | std::cerr << "\n--------------\n" << std::endl; |
70 | | #endif |
71 | |
|
72 | 0 | return stringRep; |
73 | 0 | } |
74 | | |
75 | | }; // namespace Invar |