blob: 507ff25beeffc82a6e18f6615df0e07be92e228c [file] [log] [blame]
Adriana Kobylakd311bc82016-10-16 09:54:40 -05001#include <fstream>
2#include <iostream>
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -06003#include <chrono>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05004#include <cstdio>
Adriana Kobylakfbe88722017-02-22 16:49:59 -06005#include <set>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05006#include <string>
7#include <vector>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -05008#include <sdbusplus/vtable.hpp>
9#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050010#include <systemd/sd-journal.h>
Adriana Kobylak7298dc22017-01-24 12:21:50 -060011#include "elog-lookup.cpp"
Saqib Khan2bb15192017-02-13 13:19:55 -060012#include <phosphor-logging/elog-errors-HostEvent.hpp>
Adriana Kobylak4ea7f312017-01-10 12:52:34 -060013#include "config.h"
14#include "elog_entry.hpp"
Saqib Khan2bb15192017-02-13 13:19:55 -060015#include <phosphor-logging/log.hpp>
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060016#include "log_manager.hpp"
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050017
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060018namespace phosphor
19{
20namespace logging
21{
Adriana Kobylakd311bc82016-10-16 09:54:40 -050022
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060023void Manager::commit(uint64_t transactionId, std::string errMsg)
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050024{
Adriana Kobylak7298dc22017-01-24 12:21:50 -060025 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050026
Adriana Kobylakd311bc82016-10-16 09:54:40 -050027 sd_journal *j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060028 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050029 if (rc < 0)
30 {
31 logging::log<logging::level::ERR>("Failed to open journal",
32 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060033 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050034 }
35
Adriana Kobylak7298dc22017-01-24 12:21:50 -060036 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakfbe88722017-02-22 16:49:59 -060037 std::set<std::string> metalist(g_errMetaMap[errMsg].begin(),
38 g_errMetaMap[errMsg].end());
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060039 const auto& metalistHostEvent = g_errMetaMapHostEvent[errMsg];
Adriana Kobylak4ea7f312017-01-10 12:52:34 -060040 std::vector<std::string> additionalData;
Adriana Kobylak7298dc22017-01-24 12:21:50 -060041
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060042 // TODO Remove once host event error header file is auto-generated.
43 // Also make metalist a const variable.
44 // Tracking with issue openbmc/phosphor-logging#4
45 for (auto& metaVarStrHostEvent : metalistHostEvent)
Adriana Kobylakd311bc82016-10-16 09:54:40 -050046 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060047 metalist.insert(metaVarStrHostEvent);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050048 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060049
Adriana Kobylakfbe88722017-02-22 16:49:59 -060050 // Read the journal from the end to get the most recent entry first.
51 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
52 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060053 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060054 const char *data = nullptr;
55 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060056
Adriana Kobylakfbe88722017-02-22 16:49:59 -060057 // Look for the transaction id metadata variable
58 rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data,
59 &length);
60 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060061 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060062 // This journal entry does not have the TRANSACTION_ID
63 // metadata variable.
64 continue;
65 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060066
Adriana Kobylakfbe88722017-02-22 16:49:59 -060067 std::string result(data, length);
68 if (result.find(transactionIdStr) == std::string::npos)
69 {
70 // The value of the TRANSACTION_ID metadata is not the requested
71 // transaction id number.
72 continue;
73 }
74
75 // Search for all metadata variables in the current journal entry.
76 for (auto i = metalist.cbegin(); i != metalist.cend();)
77 {
78 rc = sd_journal_get_data(j, (*i).c_str(),
79 (const void **)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060080 if (rc < 0)
81 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060082 // Metadata variable not found, check next metadata variable.
83 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060084 continue;
85 }
86
Adriana Kobylakfbe88722017-02-22 16:49:59 -060087 // Metadata variable found, save it and remove it from the set.
88 additionalData.emplace_back(data, length);
89 i = metalist.erase(i);
90 }
91 if (metalist.empty())
92 {
93 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060094 break;
95 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -060096 }
97 if (!metalist.empty())
98 {
99 // Not all the metadata variables were found in the journal.
100 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600101 {
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600102 logging::log<logging::level::INFO>("Failed to find metadata",
103 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600104 }
105 }
106
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500107 sd_journal_close(j);
108
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600109 // Create error Entry dbus object
110 entryId++;
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600111 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
112 std::chrono::system_clock::now().time_since_epoch()).count();
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600113 auto objPath = std::string(OBJ_ENTRY) + '/' +
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600114 std::to_string(entryId);
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600115 entries.insert(std::make_pair(entryId, std::make_unique<Entry>(
116 busLog,
117 objPath,
118 entryId,
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600119 ms, // Milliseconds since 1970
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600120 (Entry::Level)g_errLevelMap[errMsg],
121 std::move(errMsg),
122 std::move(additionalData))));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600123 return;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500124}
125
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600126} // namespace logging
127} // namepsace phosphor