Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 1 | #include <fstream> |
| 2 | #include <iostream> |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 3 | #include <chrono> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 4 | #include <cstdio> |
| 5 | #include <string> |
| 6 | #include <vector> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 7 | #include <sdbusplus/vtable.hpp> |
| 8 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 9 | #include <systemd/sd-journal.h> |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 10 | #include "elog-lookup.cpp" |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors-HostEvent.hpp> |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 12 | #include "config.h" |
| 13 | #include "elog_entry.hpp" |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 14 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 15 | #include "log_manager.hpp" |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 16 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace logging |
| 20 | { |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 21 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 22 | void Manager::commit(uint64_t transactionId, std::string errMsg) |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 23 | { |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 24 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 25 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 26 | sd_journal *j = nullptr; |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 27 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 28 | if (rc < 0) |
| 29 | { |
| 30 | logging::log<logging::level::ERR>("Failed to open journal", |
| 31 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 32 | return; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 35 | std::string transactionIdStr = std::to_string(transactionId); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 36 | auto& metalist = g_errMetaMap[errMsg]; |
| 37 | const auto& metalistHostEvent = g_errMetaMapHostEvent[errMsg]; |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 38 | std::vector<std::string> additionalData; |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 39 | |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 40 | // TODO Remove once host event error header file is auto-generated. |
| 41 | // Also make metalist a const variable. |
| 42 | // Tracking with issue openbmc/phosphor-logging#4 |
| 43 | for (auto& metaVarStrHostEvent : metalistHostEvent) |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 44 | { |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 45 | metalist.push_back(metaVarStrHostEvent); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 46 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 47 | |
| 48 | // Search for each metadata variable in the journal. |
| 49 | for (auto& metaVarStr : metalist) |
| 50 | { |
| 51 | const char *metadata = nullptr; |
| 52 | |
| 53 | // Read the journal from the end to get the most recent entry first. |
| 54 | // The result from the sd_journal_get_data() is of the form VARIABLE=value. |
| 55 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
| 56 | { |
| 57 | const char *data = nullptr; |
| 58 | size_t length = 0; |
| 59 | metadata = nullptr; |
| 60 | |
| 61 | // Search for the metadata variables in the current journal entry |
| 62 | rc = sd_journal_get_data(j, metaVarStr.c_str(), |
| 63 | (const void **)&metadata, &length); |
| 64 | if (rc < 0) |
| 65 | { |
| 66 | // Metadata value not found, continue to next journal entry. |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | // Look for the transaction id metadata variable |
| 71 | rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data, |
| 72 | &length); |
| 73 | if (rc < 0) |
| 74 | { |
| 75 | // This journal entry does not have the transaction id, |
| 76 | // continue to next entry |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | std::string result(data); |
| 81 | if (result.find(transactionIdStr) == std::string::npos) |
| 82 | { |
| 83 | // Requested transaction id not found, |
| 84 | // continue to next journal entry. |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | // Metadata matching the transaction id found, save it |
| 89 | // and break out of the journal search loop |
| 90 | additionalData.push_back(std::string(metadata)); |
| 91 | break; |
| 92 | } |
| 93 | if (!metadata) |
| 94 | { |
| 95 | // Metadata value not found in the journal. |
| 96 | logging::log<logging::level::INFO>("Failed to find metadata", |
| 97 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
| 98 | continue; |
| 99 | } |
| 100 | } |
| 101 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 102 | sd_journal_close(j); |
| 103 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 104 | // Create error Entry dbus object |
| 105 | entryId++; |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 106 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 107 | std::chrono::system_clock::now().time_since_epoch()).count(); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 108 | auto objPath = std::string(OBJ_ENTRY) + '/' + |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 109 | std::to_string(entryId); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 110 | entries.insert(std::make_pair(entryId, std::make_unique<Entry>( |
| 111 | busLog, |
| 112 | objPath, |
| 113 | entryId, |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 114 | ms, // Milliseconds since 1970 |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 115 | (Entry::Level)g_errLevelMap[errMsg], |
| 116 | std::move(errMsg), |
| 117 | std::move(additionalData)))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 118 | return; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 121 | } // namespace logging |
| 122 | } // namepsace phosphor |