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" |
Adriana Kobylak | 1f36a88 | 2017-02-16 08:36:55 -0600 | [diff] [blame] | 11 | #include "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" |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 14 | #include "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 | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 24 | // TODO Change /tmp path to a permanent location on flash |
| 25 | constexpr const auto path = "/tmp/elog"; |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 26 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 27 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 28 | std::string filename{}; |
| 29 | filename.append(path); |
| 30 | // TODO Create error logs in their own separate dir once permanent location |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 31 | // on flash is determined. Ex: ../transactionId/1 |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 32 | std::ofstream efile; |
| 33 | efile.open(filename); |
| 34 | efile << "{" << std::endl; |
| 35 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 36 | sd_journal *j = nullptr; |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 37 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 38 | if (rc < 0) |
| 39 | { |
| 40 | logging::log<logging::level::ERR>("Failed to open journal", |
| 41 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 42 | return; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 45 | std::string transactionIdStr = std::to_string(transactionId); |
| 46 | std::vector<std::string> metalist = g_errMetaMap[errMsg]; |
Adriana Kobylak | 1f36a88 | 2017-02-16 08:36:55 -0600 | [diff] [blame] | 47 | std::vector<std::string> metalistHostEvent = g_errMetaMapHostEvent[errMsg]; |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 48 | std::vector<std::string> additionalData; |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 49 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 50 | // 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) |
| 53 | { |
| 54 | const char *data = nullptr; |
| 55 | size_t length = 0; |
| 56 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 57 | // Look for the transaction id metadata variable |
| 58 | rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data, |
| 59 | &length); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 60 | if (rc < 0) |
| 61 | { |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 62 | // This journal entry does not have the transaction id, |
| 63 | // continue to next entry |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 64 | continue; |
| 65 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 66 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 67 | std::string result(data); |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 68 | if (result.find(transactionIdStr) == std::string::npos) |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 69 | { |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 70 | // Requested transaction id not found, |
| 71 | // continue to next journal entry. |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 72 | continue; |
| 73 | } |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 74 | // Match found, write to file |
| 75 | // TODO This is a draft format based on the redfish event logs written |
| 76 | // in json, the final openbmc format is to be determined |
| 77 | efile << "\t{" << std::endl; |
| 78 | efile << "\t\"@" << data << "\"," << std::endl; |
| 79 | |
| 80 | // Include the journal message |
| 81 | rc = sd_journal_get_data(j, "MESSAGE", (const void **)&data, &length); |
| 82 | if (rc < 0) |
| 83 | { |
| 84 | continue; |
| 85 | } |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 86 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 87 | efile << "\t\"@" << data << "\"," << std::endl; |
| 88 | |
| 89 | // Search for the metadata variables in the current journal entry |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 90 | for (auto metaVarStr : metalist) |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 91 | { |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 92 | rc = sd_journal_get_data(j, metaVarStr.c_str(), |
| 93 | (const void **)&data, &length); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 94 | if (rc < 0) |
| 95 | { |
| 96 | // Not found, continue to next metadata variable |
| 97 | logging::log<logging::level::INFO>("Failed to find metadata", |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 98 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 99 | continue; |
| 100 | } |
| 101 | |
| 102 | // Metatdata variable found, write to file |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 103 | additionalData.push_back(std::string(data)); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 104 | efile << "\t\"@" << data << "\"," << std::endl; |
| 105 | } |
| 106 | efile << "\t}" << std::endl; |
| 107 | |
Adriana Kobylak | 1f36a88 | 2017-02-16 08:36:55 -0600 | [diff] [blame] | 108 | // TODO Remove once host event error header file is auto-generated. |
| 109 | // Tracking with issue openbmc/phosphor-logging#4 |
| 110 | for (auto metaVarStrHostEvent : metalistHostEvent) |
| 111 | { |
| 112 | rc = sd_journal_get_data(j, metaVarStrHostEvent.c_str(), |
| 113 | (const void **)&data, &length); |
| 114 | if (rc < 0) |
| 115 | { |
| 116 | // Not found, continue to next metadata variable |
| 117 | logging::log<logging::level::INFO>("Failed to find metadata", |
| 118 | logging::entry("META_FIELD=%s", |
| 119 | metaVarStrHostEvent.c_str())); |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | // Metatdata variable found, write to file |
| 124 | additionalData.push_back(std::string(data)); |
| 125 | efile << "\t\"@" << data << "\"," << std::endl; |
| 126 | } |
| 127 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 128 | // TODO Break only once all metadata fields have been found. Implement |
| 129 | // once this function reads the metadata fields from the header file. |
| 130 | break; |
| 131 | } |
| 132 | sd_journal_close(j); |
| 133 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 134 | // Create error Entry dbus object |
| 135 | entryId++; |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 136 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 137 | std::chrono::system_clock::now().time_since_epoch()).count(); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 138 | auto objPath = std::string(OBJ_ENTRY) + '/' + |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 139 | std::to_string(entryId); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 140 | entries.insert(std::make_pair(entryId, std::make_unique<Entry>( |
| 141 | busLog, |
| 142 | objPath, |
| 143 | entryId, |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 144 | ms, // Milliseconds since 1970 |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 145 | (Entry::Level)g_errLevelMap[errMsg], |
| 146 | std::move(errMsg), |
| 147 | std::move(additionalData)))); |
| 148 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 149 | efile << "}" << std::endl; |
| 150 | efile.close(); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 151 | return; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 154 | } // namespace logging |
| 155 | } // namepsace phosphor |