refactor metadata pack/unpack functions
Move the `parse` and `combine` functions, which are used to translate
between `map<string,string>` and `vector<string>` for the metadata.
This is in preparation for transitioning the AdditionalData field
from `vector` to `map`.
Tested: Test cases pass. Simple `log-create` call has no change in
behavior.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ib968e1fe02903072c300d5387cf91f2d8164b1b4
diff --git a/elog_meta.cpp b/elog_meta.cpp
index 0131c2f..1b8aaea 100644
--- a/elog_meta.cpp
+++ b/elog_meta.cpp
@@ -2,6 +2,8 @@
#include "elog_meta.hpp"
+#include "util.hpp"
+
namespace phosphor
{
namespace logging
@@ -18,8 +20,7 @@
const std::string& match, const std::vector<std::string>& data,
AssociationList& list)
{
- std::map<std::string, std::string> metadata;
- parse(data, metadata);
+ auto metadata = util::additional_data::parse(data);
auto iter = metadata.find(match);
if (metadata.end() != iter)
{
@@ -44,8 +45,7 @@
const std::string& match, const std::vector<std::string>& data,
AssociationList& list)
{
- std::map<std::string, std::string> metadata;
- parse(data, metadata);
+ auto metadata = util::additional_data::parse(data);
auto iter = metadata.find(match);
if (metadata.end() != iter)
{
diff --git a/elog_meta.hpp b/elog_meta.hpp
index 4273a4c..97a212f 100644
--- a/elog_meta.hpp
+++ b/elog_meta.hpp
@@ -4,6 +4,7 @@
#include "callouts-gen.hpp"
#include "elog_entry.hpp"
+#include "util.hpp"
#include <phosphor-logging/elog-errors.hpp>
@@ -28,44 +29,6 @@
using Type = void(const std::string&, const std::vector<std::string>&,
AssociationList& list);
-/** @brief Pull out metadata name and value from the string
- * <metadata name>=<metadata value>
- * @param [in] data - metadata key=value entries
- * @param [out] metadata - map of metadata name:value
- */
-inline void parse(const std::vector<std::string>& data,
- std::map<std::string, std::string>& metadata)
-{
- constexpr auto separator = '=';
- for (const auto& entryItem : data)
- {
- auto pos = entryItem.find(separator);
- if (std::string::npos != pos)
- {
- auto key = entryItem.substr(0, entryItem.find(separator));
- auto value = entryItem.substr(entryItem.find(separator) + 1);
- metadata.emplace(std::move(key), std::move(value));
- }
- }
-}
-
-/** @brief Combine the metadata keys and values from the map
- * into a vector of strings that look like:
- * "<metadata name>=<metadata value>"
- * @param [in] data - metadata key:value map
- * @param [out] metadata - vector of "key=value" strings
- */
-inline void combine(const std::map<std::string, std::string>& data,
- std::vector<std::string>& metadata)
-{
- for (const auto& [key, value] : data)
- {
- std::string line{key};
- line += "=" + value;
- metadata.push_back(std::move(line));
- }
-}
-
/** @brief Build error associations specific to metadata. Specialize this
* template for handling a specific type of metadata.
* @tparam M - type of metadata
@@ -92,8 +55,7 @@
const std::string& match, const std::vector<std::string>& data,
AssociationList& list)
{
- std::map<std::string, std::string> metadata;
- parse(data, metadata);
+ auto metadata = util::additional_data::parse(data);
auto iter = metadata.find(match);
if (metadata.end() != iter)
{
diff --git a/log_manager.cpp b/log_manager.cpp
index de3509d..125677e 100644
--- a/log_manager.cpp
+++ b/log_manager.cpp
@@ -707,8 +707,7 @@
const FFDCEntries& ffdc) -> sdbusplus::message::object_path
{
// Convert the map into a vector of "key=value" strings
- std::vector<std::string> ad;
- metadata::associations::combine(additionalData, ad);
+ auto ad = util::additional_data::combine(additionalData);
return createEntry(message, severity, ad, ffdc);
}
diff --git a/phosphor-rsyslog-config/meson.build b/phosphor-rsyslog-config/meson.build
index a24baf9..4d85b20 100644
--- a/phosphor-rsyslog-config/meson.build
+++ b/phosphor-rsyslog-config/meson.build
@@ -2,6 +2,7 @@
'main.cpp',
'server-conf.cpp',
'../elog_meta.cpp',
+ '../util.cpp',
elog_process_gen,
include_directories: include_directories('..'),
dependencies: [
diff --git a/util.cpp b/util.cpp
index da068e6..3847e0a 100644
--- a/util.cpp
+++ b/util.cpp
@@ -197,4 +197,42 @@
return;
}
+namespace additional_data
+{
+auto parse(const std::vector<std::string>& data)
+ -> std::map<std::string, std::string>
+{
+ std::map<std::string, std::string> metadata{};
+
+ constexpr auto separator = '=';
+ for (const auto& entryItem : data)
+ {
+ auto pos = entryItem.find(separator);
+ if (std::string::npos != pos)
+ {
+ auto key = entryItem.substr(0, entryItem.find(separator));
+ auto value = entryItem.substr(entryItem.find(separator) + 1);
+ metadata.emplace(std::move(key), std::move(value));
+ }
+ }
+
+ return metadata;
+}
+
+auto combine(const std::map<std::string, std::string>& data)
+ -> std::vector<std::string>
+{
+ std::vector<std::string> metadata{};
+
+ for (const auto& [key, value] : data)
+ {
+ std::string line{key};
+ line += "=" + value;
+ metadata.emplace_back(std::move(line));
+ }
+
+ return metadata;
+}
+} // namespace additional_data
+
} // namespace phosphor::logging::util
diff --git a/util.hpp b/util.hpp
index 0ac3878..405e35c 100644
--- a/util.hpp
+++ b/util.hpp
@@ -1,8 +1,10 @@
#pragma once
#include <fstream>
+#include <map>
#include <optional>
#include <string>
+#include <vector>
namespace phosphor::logging::util
{
@@ -23,4 +25,23 @@
*/
void journalSync();
+namespace additional_data
+{
+/** @brief Pull out metadata name and value from the string
+ * <metadata name>=<metadata value>
+ * @param [in] data - metadata key=value entries
+ * @return map of metadata name:value
+ */
+auto parse(const std::vector<std::string>& data)
+ -> std::map<std::string, std::string>;
+/** @brief Combine the metadata keys and values from the map
+ * into a vector of strings that look like:
+ * "<metadata name>=<metadata value>"
+ * @param [in] data - metadata key:value map
+ * @return vector of "key=value" strings
+ */
+auto combine(const std::map<std::string, std::string>& data)
+ -> std::vector<std::string>;
+} // namespace additional_data
+
} // namespace phosphor::logging::util