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