1 #pragma once 2 #include <cstdint> 3 #include <sstream> 4 #include <string> 5 6 namespace phosphor 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 { 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; 43 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 } 56 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()) 98 { 99 all += '|'; 100 } 101 } 102 103 /** 104 * The string containing all name/value pairs 105 */ 106 std::string all; 107 }; 108 109 } // namespace util 110 } // namespace power 111 } // namespace phosphor 112