Shawn McCarney | 55f496c | 2020-04-09 16:19:14 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "exception_utils.hpp" |
| 18 | |
| 19 | namespace phosphor::power::regulators::exception_utils |
| 20 | { |
| 21 | |
| 22 | std::vector<std::string> getMessages(const std::exception& e) |
| 23 | { |
| 24 | std::vector<std::string> messages{}; |
| 25 | internal::getMessages(e, messages); |
| 26 | return messages; |
| 27 | } |
| 28 | |
| 29 | void log(const std::exception& e) |
| 30 | { |
| 31 | std::vector<std::string> messages = getMessages(e); |
| 32 | for (const std::string& message : messages) |
| 33 | { |
| 34 | journal::logErr(message); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | namespace internal |
| 39 | { |
| 40 | |
| 41 | void getMessages(const std::exception& e, std::vector<std::string>& messages) |
| 42 | { |
| 43 | // If this exception is nested, get messages from inner exception(s) |
| 44 | try |
| 45 | { |
| 46 | std::rethrow_if_nested(e); |
| 47 | } |
| 48 | catch (const std::exception& inner) |
| 49 | { |
| 50 | getMessages(inner, messages); |
| 51 | } |
| 52 | catch (...) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | // Append error message from this exception |
| 57 | messages.emplace_back(e.what()); |
| 58 | } |
| 59 | |
| 60 | } // namespace internal |
| 61 | |
| 62 | } // namespace phosphor::power::regulators::exception_utils |