xref: /openbmc/phosphor-logging/elog_meta.hpp (revision 66491c61)
1 #pragma once
2 
3 #include "config.h"
4 
5 #include "callouts-gen.hpp"
6 #include "elog_entry.hpp"
7 
8 #include <algorithm>
9 #include <cstring>
10 #include <phosphor-logging/elog-errors.hpp>
11 #include <string>
12 #include <tuple>
13 #include <vector>
14 
15 namespace phosphor
16 {
17 namespace logging
18 {
19 namespace metadata
20 {
21 
22 using Metadata = std::string;
23 
24 namespace associations
25 {
26 
27 using Type = void(const std::string&, const std::vector<std::string>&,
28                   AssociationList& list);
29 
30 /** @brief Pull out metadata name and value from the string
31  *         <metadata name>=<metadata value>
32  *  @param [in] data - metadata key=value entries
33  *  @param [out] metadata - map of metadata name:value
34  */
35 inline void parse(const std::vector<std::string>& data,
36                   std::map<std::string, std::string>& metadata)
37 {
38     constexpr auto separator = '=';
39     for (const auto& entryItem : data)
40     {
41         auto pos = entryItem.find(separator);
42         if (std::string::npos != pos)
43         {
44             auto key = entryItem.substr(0, entryItem.find(separator));
45             auto value = entryItem.substr(entryItem.find(separator) + 1);
46             metadata.emplace(std::move(key), std::move(value));
47         }
48     }
49 }
50 
51 /** @brief Combine the metadata keys and values from the map
52  *         into a vector of strings that look like:
53  *            "<metadata name>=<metadata value>"
54  *  @param [in] data - metadata key:value map
55  *  @param [out] metadata - vector of "key=value" strings
56  */
57 inline void combine(const std::map<std::string, std::string>& data,
58                     std::vector<std::string>& metadata)
59 {
60     for (const auto& [key, value] : data)
61     {
62         std::string line{key};
63         line += "=" + value;
64         metadata.push_back(std::move(line));
65     }
66 }
67 
68 /** @brief Build error associations specific to metadata. Specialize this
69  *         template for handling a specific type of metadata.
70  *  @tparam M - type of metadata
71  *  @param [in] match - metadata to be handled
72  *  @param [in] data - metadata key=value entries
73  *  @param [out] list - list of error association objects
74  */
75 template <typename M>
76 void build(const std::string& match, const std::vector<std::string>& data,
77            AssociationList& list) = delete;
78 
79 // Example template specialization - we don't want to do anything
80 // for this metadata.
81 using namespace example::xyz::openbmc_project::Example::Elog;
82 template <>
83 inline void
84     build<TestErrorTwo::DEV_ID>(const std::string& /*match*/,
85                                 const std::vector<std::string>& /*data*/,
86                                 AssociationList& /*list*/)
87 {
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