xref: /openbmc/phosphor-logging/elog_meta.hpp (revision 869bc56c77cac72faabaab27ac2f31e3c3269b30)
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 build<TestErrorTwo::DEV_ID>(
85     const std::string& /*match*/, const std::vector<std::string>& /*data*/,
86     AssociationList& /*list*/)
87 {}
88 
89 template <>
90 inline void build<example::xyz::openbmc_project::example::device::Callout::
91                       CALLOUT_DEVICE_PATH_TEST>(
92     const std::string& match, 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