xref: /openbmc/phosphor-logging/elog_meta.hpp (revision 739e9256)
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 #include <tuple>
6 #include <algorithm>
7 #include <cstring>
8 #include <phosphor-logging/elog-errors.hpp>
9 #include "elog_entry.hpp"
10 #include "callouts-gen.hpp"
11 
12 namespace phosphor
13 {
14 namespace logging
15 {
16 namespace metadata
17 {
18 
19 using Metadata = std::string;
20 
21 namespace associations
22 {
23 
24 using Type = void(const std::string&,
25                   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,
58            const std::vector<std::string>& data,
59            AssociationList& list) = delete;
60 
61 // Example template specialization - we don't want to do anything
62 // for this metadata.
63 using namespace example::xyz::openbmc_project::Example::Elog;
64 template <>
65 inline void build<TestErrorTwo::DEV_ID>(const std::string& match,
66                                         const std::vector<std::string>& data,
67                                         AssociationList& list)
68 {
69 }
70 
71 template <>
72 inline void build<example::xyz::openbmc_project::
73                   Example::Device::Callout::CALLOUT_DEVICE_PATH_TEST>(
74     const std::string& match,
75     const std::vector<std::string>& data,
76     AssociationList& list)
77 {
78     constexpr auto ROOT = "/xyz/openbmc_project/inventory";
79     std::map<std::string, std::string> metadata;
80     parse(data, metadata);
81     auto iter = metadata.find(match);
82     if(metadata.end() != iter)
83     {
84         auto comp = [](const auto& first, const auto& second)
85         {
86             return (strcmp(std::get<0>(first), second) < 0);
87         };
88         auto callout = std::lower_bound(callouts.begin(),
89                                         callouts.end(),
90                                         (iter->second).c_str(),
91                                         comp);
92         if((callouts.end() != callout) &&
93            !strcmp((iter->second).c_str(), std::get<0>(*callout)))
94         {
95             list.push_back(std::make_tuple("callout",
96                                            "fault",
97                                            std::string(ROOT) +
98                                            std::get<1>(*callout)));
99         }
100     }
101 }
102 
103 } // namespace associations
104 } // namespace metadata
105 } // namespace logging
106 } // namespace phosphor
107