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 | #pragma once |
| 17 | |
| 18 | #include "journal.hpp" |
| 19 | |
| 20 | #include <exception> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | /** |
| 25 | * @namespace exception_utils |
| 26 | * |
| 27 | * Contains utility functions for handling exceptions. |
| 28 | */ |
| 29 | namespace phosphor::power::regulators::exception_utils |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Gets the error messages from the specified exception and any nested inner |
| 34 | * exceptions. |
| 35 | * |
| 36 | * If the exception contains nested inner exceptions, the messages in the |
| 37 | * returned vector will be ordered from innermost exception to outermost |
| 38 | * exception. |
| 39 | * |
| 40 | * @param e exception |
| 41 | * @return error messages from exceptions |
| 42 | */ |
| 43 | std::vector<std::string> getMessages(const std::exception& e); |
| 44 | |
| 45 | /** |
| 46 | * Logs the specified exception to the systemd journal. |
| 47 | * |
| 48 | * Gets the error messages from the specified exception and any nested inner |
| 49 | * exceptions. |
| 50 | * |
| 51 | * Logs each error message to the journal with a priority value of 'ERR'. |
| 52 | * |
| 53 | * @param e exception |
| 54 | */ |
| 55 | void log(const std::exception& e); |
| 56 | |
| 57 | /* |
| 58 | * Internal implementation details |
| 59 | */ |
| 60 | namespace internal |
| 61 | { |
| 62 | |
| 63 | /** |
| 64 | * Gets the error messages from the specified exception and any nested inner |
| 65 | * exceptions. |
| 66 | * |
| 67 | * Stores the error messages in the specified vector, from innermost exception |
| 68 | * to outermost exception. |
| 69 | * |
| 70 | * @param e exception |
| 71 | * @param messages vector where error messages will be stored |
| 72 | */ |
| 73 | void getMessages(const std::exception& e, std::vector<std::string>& messages); |
| 74 | |
| 75 | } // namespace internal |
| 76 | |
| 77 | } // namespace phosphor::power::regulators::exception_utils |