xref: /openbmc/phosphor-logging/elog_meta.hpp (revision f18bf836)
1 #pragma once
2 
3 #include "callouts-gen.hpp"
4 #include "elog_entry.hpp"
5 
6 #include <algorithm>
7 #include <cstring>
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <string>
10 #include <tuple>
11 #include <vector>
12 
13 namespace phosphor
14 {
15 namespace logging
16 {
17 namespace metadata
18 {
19 
20 using Metadata = std::string;
21 
22 namespace associations
23 {
24 
25 using Type = void(const std::string&, const std::vector<std::string>&,
26                   AssociationList& list);
27 
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 
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
52  *  @param [in] match - metadata to be handled
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, const std::vector<std::string>& data,
58            AssociationList& list) = delete;
59 
60 // Example template specialization - we don't want to do anything
61 // for this metadata.
62 using namespace example::xyz::openbmc_project::Example::Elog;
63 template <>
64 inline void build<TestErrorTwo::DEV_ID>(const std::string& match,
65                                         const std::vector<std::string>& data,
66                                         AssociationList& list)
67 {
68 }
69 
70 template <>
71 inline void
72     build<example::xyz::openbmc_project::Example::Device::Callout::
73               CALLOUT_DEVICE_PATH_TEST>(const std::string& match,
74                                         const std::vector<std::string>& data,
75                                         AssociationList& list)
76 {
77     constexpr auto ROOT = "/xyz/openbmc_project/inventory";
78     std::map<std::string, std::string> metadata;
79     parse(data, metadata);
80     auto iter = metadata.find(match);
81     if (metadata.end() != iter)
82     {
83         auto comp = [](const auto& first, const auto& second) {
84             return (strcmp(std::get<0>(first), second) < 0);
85         };
86         auto callout = std::lower_bound(callouts.begin(), callouts.end(),
87                                         (iter->second).c_str(), comp);
88         if ((callouts.end() != callout) &&
89             !strcmp((iter->second).c_str(), std::get<0>(*callout)))
90         {
91             list.push_back(std::make_tuple(
92                 "callout", "fault", std::string(ROOT) + std::get<1>(*callout)));
93         }
94     }
95 }
96 
97 // The PROCESS_META flag is needed to get out of tree builds working. Such
98 // builds will have access only to internal error interfaces, hence handlers
99 // for out dbus error interfaces won't compile. This flag is not set by default,
100 // the phosphor-logging recipe enabled it.
101 #if defined PROCESS_META
102 
103 template <>
104 void build<xyz::openbmc_project::Common::Callout::Device::CALLOUT_DEVICE_PATH>(
105     const std::string& match, const std::vector<std::string>& data,
106     AssociationList& list);
107 
108 template <>
109 void build<
110     xyz::openbmc_project::Common::Callout::Inventory::CALLOUT_INVENTORY_PATH>(
111     const std::string& match, const std::vector<std::string>& data,
112     AssociationList& list);
113 
114 #endif // PROCESS_META
115 
116 } // namespace associations
117 } // namespace metadata
118 } // namespace logging
119 } // namespace phosphor
120