1 #pragma once 2 #include <sstream> 3 #include <string> 4 5 namespace phosphor 6 { 7 namespace power 8 { 9 namespace 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 */ 34 class NamesValues 35 { 36 public: 37 NamesValues() = default; 38 NamesValues(const NamesValues&) = default; 39 NamesValues& operator=(const NamesValues&) = default; 40 NamesValues(NamesValues&&) = default; 41 NamesValues& operator=(NamesValues&&) = default; 42 43 /** 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 } 55 56 /** 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()) 97 { 98 all += '|'; 99 } 100 } 101 102 /** 103 * The string containing all name/value pairs 104 */ 105 std::string all; 106 }; 107 108 } // namespace util 109 } // namespace power 110 } // namespace phosphor 111