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