blob: 9c2756a611f4ffa1b5561f77b55fe0cba2fc0153 [file] [log] [blame]
Brandon Wyman83abbd62017-09-25 16:41:39 -05001#pragma once
Patrick Williams8c324c92023-05-31 21:59:39 -05002#include <cstdint>
Matt Spinler3b090d12017-09-08 15:07:05 -05003#include <sstream>
4#include <string>
5
Lei YUab093322019-10-09 16:43:22 +08006namespace phosphor
Matt Spinler3b090d12017-09-08 15:07:05 -05007{
8namespace power
9{
10namespace util
11{
12
13/**
14 * @class NamesValues
15 *
16 * Builds up a string of name=value pairs for use in
17 * situations like error log metadata.
18 *
19 * Names and values can be added to an instance of this
20 * class, and then calling get() will return a string
21 * that appends them all together.
22 *
23 * Currently, just uint64_t values that will be displayed
24 * in hex are supported.
25 *
26 * For example:
27 * NamesValues m;
28 * uint8_t d = 0xFF;
29 *
30 * m.add("STATUS_VOUT", d);
31 * m.add("STATUS_WORD", 0xABCD);
32 *
33 * m.get() //returns: "STATUS_VOUT=0xFF|STATUS_WORD=0xABCD"
34 */
35class NamesValues
36{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050037 public:
38 NamesValues() = default;
39 NamesValues(const NamesValues&) = default;
40 NamesValues& operator=(const NamesValues&) = default;
41 NamesValues(NamesValues&&) = default;
42 NamesValues& operator=(NamesValues&&) = default;
Matt Spinler3b090d12017-09-08 15:07:05 -050043
Matt Spinlerf0f02b92018-10-25 16:12:43 -050044 /**
45 * Adds a name/value pair to the object
46 *
47 * @param name - the name to add
48 * @param value - the value to add
49 */
50 void add(const std::string& name, uint64_t value)
51 {
52 addDivider();
53 addName(name);
54 addValue(value);
55 }
Matt Spinler3b090d12017-09-08 15:07:05 -050056
Matt Spinlerf0f02b92018-10-25 16:12:43 -050057 /**
58 * Returns a formatted concatenation of all of the names and
59 * their values.
60 *
61 * @return string - "<name1>=<value1>|<name2>=<value2>..etc"
62 */
63 const std::string& get() const
64 {
65 return all;
66 }
67
68 private:
69 /**
70 * Adds a name to the object
71 *
72 * @param name - the name to add
73 */
74 void addName(const std::string& name)
75 {
76 all += name + '=';
77 }
78
79 /**
80 * Adds a value to the object
81 *
82 * @param value - the value to add
83 */
84 void addValue(uint64_t value)
85 {
86 std::ostringstream stream;
87 stream << "0x" << std::hex << value;
88 all += stream.str();
89 }
90
91 /**
92 * Adds a divider to the summary string to
93 * break up the name/value pairs
94 */
95 void addDivider()
96 {
97 if (!all.empty())
Matt Spinler3b090d12017-09-08 15:07:05 -050098 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050099 all += '|';
Matt Spinler3b090d12017-09-08 15:07:05 -0500100 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500101 }
Matt Spinler3b090d12017-09-08 15:07:05 -0500102
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500103 /**
104 * The string containing all name/value pairs
105 */
106 std::string all;
Matt Spinler3b090d12017-09-08 15:07:05 -0500107};
108
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500109} // namespace util
110} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800111} // namespace phosphor