Brandon Wyman | 83abbd6 | 2017-09-25 16:41:39 -0500 | [diff] [blame] | 1 | #pragma once |
Patrick Williams | 8c324c9 | 2023-05-31 21:59:39 -0500 | [diff] [blame] | 2 | #include <cstdint> |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 3 | #include <sstream> |
| 4 | #include <string> |
| 5 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 6 | namespace phosphor |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 7 | { |
| 8 | namespace power |
| 9 | { |
| 10 | namespace 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 | */ |
| 35 | class NamesValues |
| 36 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 37 | 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 Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 43 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 44 | /** |
| 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 Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 56 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 57 | /** |
| 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 Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 98 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 99 | all += '|'; |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 100 | } |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 101 | } |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 102 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 103 | /** |
| 104 | * The string containing all name/value pairs |
| 105 | */ |
| 106 | std::string all; |
Matt Spinler | 3b090d1 | 2017-09-08 15:07:05 -0500 | [diff] [blame] | 107 | }; |
| 108 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 109 | } // namespace util |
| 110 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 111 | } // namespace phosphor |