summaryrefslogtreecommitdiffstats
path: root/extensions
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2020-01-10 13:49:22 -0600
committerMatt Spinler <spinler@us.ibm.com>2020-01-22 16:26:01 +0000
commitacb7c106a0ded9b9ef96e3c39ce573c919fb9425 (patch)
tree59bd4712440012f3dc2ae3d5c962825e9fdeae6d /extensions
parentebffe1c2fbfc93626624f0a0f87f71b6918c76ce (diff)
downloadphosphor-logging-acb7c106a0ded9b9ef96e3c39ce573c919fb9425.tar.gz
phosphor-logging-acb7c106a0ded9b9ef96e3c39ce573c919fb9425.zip
PEL: Print the JSON formatted UserData
Override the getJSON() function in the UserData section so it will support peltool pretty printing when the data has been stored as JSON (component phosphor-logging, subtype 1 as defined in user_data_formats.hpp). It first converts the original JSON string to a nlohmann::json object to validate it is valid JSON and then uses nlohmann::json's stream << operator to convert it into a pretty string. This also sets up a framework where other UserData parsing functions can be called from, and it is all only compiled into peltool, and not into phosphor-log-manager. The UserData section created out of the OpenBMC event log's AdditionalData property already makes use of this format. Here are some example outputs. Note that the 'Data' key is added by this code for JSON that isn't already an object (dict) to make it one. "User Data": { "OPERATION": "something", "REGISTER_FFDC": "REG1=0x8|REG2=0x9|REG3=0x55", "RETURN_VALUE": "-12", "TIMEOUT_IN_MSEC": "100", "_PID": "993" }, "User Data": { "Data": [ "OPERATION", "REGISTER_FFDC", "RETURN_VALUE", "TIMEOUT_IN_MSEC", "_PID" ] }, "User Data": { "Data": "This is a string" } Signed-off-by: Matt Spinler <spinler@us.ibm.com> Change-Id: I7b034ac24339b5b019db3b57d8e71cb03559363e
Diffstat (limited to 'extensions')
-rw-r--r--extensions/openpower-pels/openpower-pels.mk10
-rw-r--r--extensions/openpower-pels/pel.cpp50
-rw-r--r--extensions/openpower-pels/pel.hpp13
-rw-r--r--extensions/openpower-pels/user_data.cpp14
-rw-r--r--extensions/openpower-pels/user_data.hpp8
-rw-r--r--extensions/openpower-pels/user_data_json.cpp153
-rw-r--r--extensions/openpower-pels/user_data_json.hpp25
7 files changed, 265 insertions, 8 deletions
diff --git a/extensions/openpower-pels/openpower-pels.mk b/extensions/openpower-pels/openpower-pels.mk
index d974e06..f028293 100644
--- a/extensions/openpower-pels/openpower-pels.mk
+++ b/extensions/openpower-pels/openpower-pels.mk
@@ -1,7 +1,8 @@
phosphor_log_manager_SOURCES += \
extensions/openpower-pels/entry_points.cpp \
extensions/openpower-pels/manager.cpp \
- extensions/openpower-pels/repository.cpp
+ extensions/openpower-pels/repository.cpp \
+ extensions/openpower-pels/user_data.cpp
phosphor_log_manager_LDADD = \
libpel.la
@@ -32,7 +33,6 @@ libpel_la_SOURCES = \
extensions/openpower-pels/src.cpp \
extensions/openpower-pels/section_factory.cpp \
extensions/openpower-pels/severity.cpp \
- extensions/openpower-pels/user_data.cpp \
extensions/openpower-pels/user_header.cpp
libpel_ldflags = \
@@ -56,5 +56,9 @@ registry_DATA = extensions/openpower-pels/registry/message_registry.json
bin_PROGRAMS += peltool
-peltool_SOURCES = extensions/openpower-pels/tools/peltool.cpp
+peltool_SOURCES = \
+ extensions/openpower-pels/tools/peltool.cpp \
+ extensions/openpower-pels/user_data.cpp \
+ extensions/openpower-pels/user_data_json.cpp
peltool_LDADD = libpel.la
+peltool_CXXFLAGS = "-DPELTOOL"
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
index 74d46bd..262865f 100644
--- a/extensions/openpower-pels/pel.cpp
+++ b/extensions/openpower-pels/pel.cpp
@@ -214,7 +214,8 @@ std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
} // namespace util
-void PEL::printSectionInJSON(const Section& section, std::string& buf) const
+void PEL::printSectionInJSON(const Section& section, std::string& buf,
+ std::map<uint16_t, size_t>& pluralSections) const
{
char tmpB[5];
uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
@@ -224,6 +225,15 @@ void PEL::printSectionInJSON(const Section& section, std::string& buf) const
std::string sectionName = pv::sectionTitles.count(sectionID)
? pv::sectionTitles.at(sectionID)
: "Unknown Section";
+
+ // Add a count if there are multiple of this type of section
+ auto count = pluralSections.find(section.header().id);
+ if (count != pluralSections.end())
+ {
+ sectionName += " " + std::to_string(count->second);
+ count->second++;
+ }
+
if (section.valid())
{
auto json = section.getJSON();
@@ -248,14 +258,46 @@ void PEL::printSectionInJSON(const Section& section, std::string& buf) const
}
}
+std::map<uint16_t, size_t> PEL::getPluralSections() const
+{
+ std::map<uint16_t, size_t> sectionCounts;
+
+ for (const auto& section : optionalSections())
+ {
+ if (sectionCounts.find(section->header().id) == sectionCounts.end())
+ {
+ sectionCounts[section->header().id] = 1;
+ }
+ else
+ {
+ sectionCounts[section->header().id]++;
+ }
+ }
+
+ std::map<uint16_t, size_t> sections;
+ for (const auto& [id, count] : sectionCounts)
+ {
+ if (count > 1)
+ {
+ // Start with 0 here and printSectionInJSON()
+ // will increment it as it goes.
+ sections.emplace(id, 0);
+ }
+ }
+
+ return sections;
+}
+
void PEL::toJSON() const
{
+ auto sections = getPluralSections();
+
std::string buf = "{";
- printSectionInJSON(*(_ph.get()), buf);
- printSectionInJSON(*(_uh.get()), buf);
+ printSectionInJSON(*(_ph.get()), buf, sections);
+ printSectionInJSON(*(_uh.get()), buf, sections);
for (auto& section : this->optionalSections())
{
- printSectionInJSON(*(section.get()), buf);
+ printSectionInJSON(*(section.get()), buf, sections);
}
buf += "}";
std::size_t found = buf.rfind(",");
diff --git a/extensions/openpower-pels/pel.hpp b/extensions/openpower-pels/pel.hpp
index 139fda8..ccbef91 100644
--- a/extensions/openpower-pels/pel.hpp
+++ b/extensions/openpower-pels/pel.hpp
@@ -286,6 +286,14 @@ class PEL
void checkRulesAndFix();
/**
+ * @brief Returns a map of the section IDs that appear more than once
+ * in the PEL. The data value for each entry will be set to 0.
+ *
+ * @return std::map<uint16_t, size_t>
+ */
+ std::map<uint16_t, size_t> getPluralSections() const;
+
+ /**
* @brief The PEL Private Header section
*/
std::unique_ptr<PrivateHeader> _ph;
@@ -304,8 +312,11 @@ class PEL
* @brief helper function for printing PELs.
* @param[in] Section& - section object reference
* @param[in] std::string - PEL string
+ * @param[in|out] pluralSections - Map used to track sections counts for
+ * when there is more than 1.
*/
- void printSectionInJSON(const Section& section, std::string& buf) const;
+ void printSectionInJSON(const Section& section, std::string& buf,
+ std::map<uint16_t, size_t>& pluralSections) const;
};
namespace util
diff --git a/extensions/openpower-pels/user_data.cpp b/extensions/openpower-pels/user_data.cpp
index 8036131..70acbbb 100644
--- a/extensions/openpower-pels/user_data.cpp
+++ b/extensions/openpower-pels/user_data.cpp
@@ -15,7 +15,12 @@
*/
#include "user_data.hpp"
+#include "json_utils.hpp"
#include "pel_types.hpp"
+#include "user_data_formats.hpp"
+#ifdef PELTOOL
+#include "user_data_json.hpp"
+#endif
#include <phosphor-logging/log.hpp>
@@ -90,5 +95,14 @@ void UserData::validate()
}
}
+std::optional<std::string> UserData::getJSON() const
+{
+#ifdef PELTOOL
+ return user_data::getJSON(_header.componentID, _header.subType,
+ _header.version, _data);
+#endif
+ return std::nullopt;
+}
+
} // namespace pels
} // namespace openpower
diff --git a/extensions/openpower-pels/user_data.hpp b/extensions/openpower-pels/user_data.hpp
index ce691b8..3594849 100644
--- a/extensions/openpower-pels/user_data.hpp
+++ b/extensions/openpower-pels/user_data.hpp
@@ -80,6 +80,14 @@ class UserData : public Section
return _data;
}
+ /**
+ * @brief Get the section contents in JSON
+ *
+ * @return The JSON as a string if a parser was found,
+ * otherwise std::nullopt.
+ */
+ std::optional<std::string> getJSON() const override;
+
private:
/**
* @brief Fills in the object from the stream data
diff --git a/extensions/openpower-pels/user_data_json.cpp b/extensions/openpower-pels/user_data_json.cpp
new file mode 100644
index 0000000..a86ccb3
--- /dev/null
+++ b/extensions/openpower-pels/user_data_json.cpp
@@ -0,0 +1,153 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "user_data_json.hpp"
+
+#include "pel_types.hpp"
+#include "user_data_formats.hpp"
+
+#include <fifo_map.hpp>
+#include <iomanip>
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
+#include <sstream>
+
+namespace openpower::pels::user_data
+{
+
+using namespace phosphor::logging;
+
+// Use fifo_map as nlohmann::json's map. We are just ignoring the 'less'
+// compare. With this map the keys are kept in FIFO order.
+template <class K, class V, class dummy_compare, class A>
+using fifoMap = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
+using fifoJSON = nlohmann::basic_json<fifoMap>;
+
+/**
+ * @brief Returns a JSON string for use by PEL::printSectionInJSON().
+ *
+ * The returning string will contain a JSON object, but without
+ * the outer {}. If the input JSON isn't a JSON object (dict), then
+ * one will be created with the input added to a 'Data' key.
+ *
+ * @param[in] json - The JSON to convert to a string
+ *
+ * @return std::string - The JSON string
+ */
+std::string prettyJSON(uint16_t componentID, uint8_t subType, uint8_t version,
+ const fifoJSON& json)
+{
+ fifoJSON output;
+ output["Section Version"] = std::to_string(version);
+ output["Sub-section type"] = std::to_string(subType);
+
+ char value[10];
+ sprintf(value, "0x%04X", componentID);
+ output["Created by"] = std::string{value};
+
+ if (!json.is_object())
+ {
+ output["Data"] = json;
+ }
+ else
+ {
+ for (const auto& [key, value] : json.items())
+ {
+ output[key] = value;
+ }
+ }
+
+ // Let nlohmann do the pretty printing.
+ std::stringstream stream;
+ stream << std::setw(4) << output;
+
+ auto jsonString = stream.str();
+
+ // Now it looks like:
+ // {
+ // "Section Version": ...
+ // ...
+ // }
+
+ // Since PEL::printSectionInJSON() will supply the outer { }s,
+ // remove the existing ones.
+
+ // Replace the { and the following newline, and the } and its
+ // preceeding newline.
+ jsonString.erase(0, 2);
+
+ auto pos = jsonString.find_last_of('}');
+ jsonString.erase(pos - 1);
+
+ return jsonString;
+}
+
+/**
+ * @brief Convert to an appropriate JSON string as the data is one of
+ * the formats that we natively support.
+ *
+ * @param[in] componentID - The comp ID from the UserData section header
+ * @param[in] subType - The subtype from the UserData section header
+ * @param[in] version - The version from the UserData section header
+ * @param[in] data - The data itself
+ *
+ * @return std::optional<std::string> - The JSON string if it could be created,
+ * else std::nullopt.
+ */
+std::optional<std::string>
+ getBuiltinFormatJSON(uint16_t componentID, uint8_t subType, uint8_t version,
+ const std::vector<uint8_t>& data)
+{
+ switch (subType)
+ {
+ case static_cast<uint8_t>(UserDataFormat::json):
+ {
+ std::string jsonString{data.begin(), data.begin() + data.size()};
+
+ fifoJSON json = nlohmann::json::parse(jsonString);
+
+ return prettyJSON(componentID, subType, version, json);
+ }
+ default:
+ break;
+ }
+ return std::nullopt;
+}
+
+std::optional<std::string> getJSON(uint16_t componentID, uint8_t subType,
+ uint8_t version,
+ const std::vector<uint8_t>& data)
+{
+ try
+ {
+ switch (componentID)
+ {
+ case static_cast<uint16_t>(ComponentID::phosphorLogging):
+ return getBuiltinFormatJSON(componentID, subType, version,
+ data);
+ default:
+ break;
+ }
+ }
+ catch (std::exception& e)
+ {
+ log<level::ERR>("Failed parsing UserData", entry("ERROR=%s", e.what()));
+ }
+
+ return std::nullopt;
+}
+
+} // namespace openpower::pels::user_data
diff --git a/extensions/openpower-pels/user_data_json.hpp b/extensions/openpower-pels/user_data_json.hpp
new file mode 100644
index 0000000..64fac79
--- /dev/null
+++ b/extensions/openpower-pels/user_data_json.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <optional>
+#include <string>
+#include <vector>
+
+namespace openpower::pels::user_data
+{
+
+/**
+ * @brief Returns the UserData contents as a formatted JSON string.
+ *
+ * @param[in] componentID - The comp ID from the UserData section header
+ * @param[in] subType - The subtype from the UserData section header
+ * @param[in] version - The version from the UserData section header
+ * @param[in] data - The section data
+ *
+ * @return std::optional<std::string> - The JSON string if it could be created,
+ * else std::nullopt.
+ */
+std::optional<std::string> getJSON(uint16_t componentID, uint8_t subType,
+ uint8_t version,
+ const std::vector<uint8_t>& data);
+
+} // namespace openpower::pels::user_data
OpenPOWER on IntegriCloud