Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <vector> |
| 4 | #include <string> |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 5 | #include <tuple> |
| 6 | #include <algorithm> |
| 7 | #include <cstring> |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include "elog_entry.hpp" |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 10 | #include "callouts-gen.hpp" |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace logging |
| 15 | { |
| 16 | namespace metadata |
| 17 | { |
| 18 | |
| 19 | using Metadata = std::string; |
| 20 | |
| 21 | namespace associations |
| 22 | { |
| 23 | |
| 24 | using Type = void(const std::string&, |
| 25 | const std::vector<std::string>&, |
| 26 | AssociationList& list); |
| 27 | |
Deepak Kodihalli | 66c46eb | 2017-03-02 03:07:10 -0600 | [diff] [blame] | 28 | /** @brief Pull out metadata name and value from the string |
| 29 | * <metadata name>=<metadata value> |
| 30 | * @param [in] data - metadata key=value entries |
| 31 | * @param [out] metadata - map of metadata name:value |
| 32 | */ |
| 33 | inline void parse(const std::vector<std::string>& data, |
| 34 | std::map<std::string, std::string>& metadata) |
| 35 | { |
| 36 | constexpr auto separator = '='; |
| 37 | for(const auto& entry: data) |
| 38 | { |
| 39 | auto pos = entry.find(separator); |
| 40 | if(std::string::npos != pos) |
| 41 | { |
| 42 | auto key = entry.substr(0, entry.find(separator)); |
| 43 | auto value = entry.substr(entry.find(separator) + 1); |
| 44 | metadata.emplace(std::move(key), std::move(value)); |
| 45 | } |
| 46 | } |
| 47 | }; |
| 48 | |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 49 | /** @brief Build error associations specific to metadata. Specialize this |
| 50 | * template for handling a specific type of metadata. |
| 51 | * @tparam M - type of metadata |
Deepak Kodihalli | 327e580 | 2017-02-28 02:39:56 -0600 | [diff] [blame] | 52 | * @param [in] match - metadata to be handled |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 53 | * @param [in] data - metadata key=value entries |
| 54 | * @param [out] list - list of error association objects |
| 55 | */ |
| 56 | template <typename M> |
| 57 | void build(const std::string& match, |
| 58 | const std::vector<std::string>& data, |
| 59 | AssociationList& list) = delete; |
| 60 | |
Deepak Kodihalli | 327e580 | 2017-02-28 02:39:56 -0600 | [diff] [blame] | 61 | // Example template specialization - we don't want to do anything |
| 62 | // for this metadata. |
| 63 | using namespace example::xyz::openbmc_project::Example::Elog; |
| 64 | template <> |
| 65 | inline void build<TestErrorTwo::DEV_ID>(const std::string& match, |
| 66 | const std::vector<std::string>& data, |
| 67 | AssociationList& list) |
| 68 | { |
| 69 | } |
| 70 | |
Deepak Kodihalli | 739e925 | 2017-03-05 23:23:50 -0600 | [diff] [blame] | 71 | template <> |
| 72 | inline void build<example::xyz::openbmc_project:: |
| 73 | Example::Device::Callout::CALLOUT_DEVICE_PATH_TEST>( |
| 74 | const std::string& match, |
| 75 | const std::vector<std::string>& data, |
| 76 | AssociationList& list) |
| 77 | { |
| 78 | constexpr auto ROOT = "/xyz/openbmc_project/inventory"; |
| 79 | std::map<std::string, std::string> metadata; |
| 80 | parse(data, metadata); |
| 81 | auto iter = metadata.find(match); |
| 82 | if(metadata.end() != iter) |
| 83 | { |
| 84 | auto comp = [](const auto& first, const auto& second) |
| 85 | { |
| 86 | return (strcmp(std::get<0>(first), second) < 0); |
| 87 | }; |
| 88 | auto callout = std::lower_bound(callouts.begin(), |
| 89 | callouts.end(), |
| 90 | (iter->second).c_str(), |
| 91 | comp); |
| 92 | if((callouts.end() != callout) && |
| 93 | !strcmp((iter->second).c_str(), std::get<0>(*callout))) |
| 94 | { |
| 95 | list.push_back(std::make_tuple("callout", |
| 96 | "fault", |
| 97 | std::string(ROOT) + |
| 98 | std::get<1>(*callout))); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Deepak Kodihalli | 5edacf3 | 2017-03-16 07:04:07 -0500 | [diff] [blame] | 103 | // The PROCESS_META flag is needed to get out of tree builds working. Such |
| 104 | // builds will have access only to internal error interfaces, hence handlers |
| 105 | // for out dbus error interfaces won't compile. This flag is not set by default, |
| 106 | // the phosphor-logging recipe enabled it. |
Deepak Kodihalli | 682326a | 2017-03-06 05:26:53 -0600 | [diff] [blame] | 107 | #if defined PROCESS_META |
| 108 | |
| 109 | template <> |
| 110 | void build<xyz::openbmc_project::Common:: |
| 111 | Callout::Device::CALLOUT_DEVICE_PATH>( |
Deepak Kodihalli | a5d8d15 | 2017-03-16 05:10:05 -0500 | [diff] [blame] | 112 | const std::string& match, |
Deepak Kodihalli | 682326a | 2017-03-06 05:26:53 -0600 | [diff] [blame] | 113 | const std::vector<std::string>& data, |
| 114 | AssociationList& list); |
| 115 | |
| 116 | #endif // PROCESS_META |
| 117 | |
| 118 | |
Deepak Kodihalli | 16e8754 | 2017-02-27 07:07:33 -0600 | [diff] [blame] | 119 | } // namespace associations |
| 120 | } // namespace metadata |
| 121 | } // namespace logging |
| 122 | } // namespace phosphor |