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