/root/doris/contrib/faiss/faiss/impl/FaissException.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | // -*- c++ -*- |
9 | | |
10 | | #include <faiss/impl/FaissException.h> |
11 | | #include <sstream> |
12 | | |
13 | | #ifdef __GNUG__ |
14 | | #include <cxxabi.h> |
15 | | #endif |
16 | | |
17 | | namespace faiss { |
18 | | |
19 | 0 | FaissException::FaissException(const std::string& m) : msg(m) {} |
20 | | |
21 | | FaissException::FaissException( |
22 | | const std::string& m, |
23 | | const char* funcName, |
24 | | const char* file, |
25 | 1.00k | int line) { |
26 | 1.00k | int size = snprintf( |
27 | 1.00k | nullptr, |
28 | 1.00k | 0, |
29 | 1.00k | "Error in %s at %s:%d: %s", |
30 | 1.00k | funcName, |
31 | 1.00k | file, |
32 | 1.00k | line, |
33 | 1.00k | m.c_str()); |
34 | 1.00k | msg.resize(size + 1); |
35 | 1.00k | snprintf( |
36 | 1.00k | &msg[0], |
37 | 1.00k | msg.size(), |
38 | 1.00k | "Error in %s at %s:%d: %s", |
39 | 1.00k | funcName, |
40 | 1.00k | file, |
41 | 1.00k | line, |
42 | 1.00k | m.c_str()); |
43 | 1.00k | } |
44 | | |
45 | 1.00k | const char* FaissException::what() const noexcept { |
46 | 1.00k | return msg.c_str(); |
47 | 1.00k | } |
48 | | |
49 | | void handleExceptions( |
50 | 0 | std::vector<std::pair<int, std::exception_ptr>>& exceptions) { |
51 | 0 | if (exceptions.size() == 1) { |
52 | | // throw the single received exception directly |
53 | 0 | std::rethrow_exception(exceptions.front().second); |
54 | |
|
55 | 0 | } else if (exceptions.size() > 1) { |
56 | | // multiple exceptions; aggregate them and return a single exception |
57 | 0 | std::stringstream ss; |
58 | |
|
59 | 0 | for (auto& p : exceptions) { |
60 | 0 | try { |
61 | 0 | std::rethrow_exception(p.second); |
62 | 0 | } catch (std::exception& ex) { |
63 | 0 | if (ex.what()) { |
64 | | // exception message available |
65 | 0 | ss << "Exception thrown from index " << p.first << ": " |
66 | 0 | << ex.what() << "\n"; |
67 | 0 | } else { |
68 | | // No message available |
69 | 0 | ss << "Unknown exception thrown from index " << p.first |
70 | 0 | << "\n"; |
71 | 0 | } |
72 | 0 | } catch (...) { |
73 | 0 | ss << "Unknown exception thrown from index " << p.first << "\n"; |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | 0 | throw FaissException(ss.str()); |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | // From |
82 | | // https://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname |
83 | | |
84 | 0 | std::string demangle_cpp_symbol(const char* name) { |
85 | 0 | #ifdef __GNUG__ |
86 | 0 | int status = -1; |
87 | 0 | const char* res = abi::__cxa_demangle(name, nullptr, nullptr, &status); |
88 | 0 | std::string sres; |
89 | 0 | if (status == 0) { |
90 | 0 | sres = res; |
91 | 0 | } |
92 | 0 | free((void*)res); |
93 | 0 | return sres; |
94 | | #else |
95 | | // don't know how to do this on other platforms |
96 | | return std::string(name); |
97 | | #endif |
98 | 0 | } |
99 | | |
100 | | } // namespace faiss |