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> |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 5 | #include <set> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 8 | #include <sdbusplus/vtable.hpp> |
| 9 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 10 | #include <systemd/sd-journal.h> |
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" |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 16 | #include "elog_meta.hpp" |
| 17 | |
| 18 | using namespace phosphor::logging; |
| 19 | extern const std::map<metadata::Metadata, |
| 20 | std::function<metadata::associations::Type>> meta; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 21 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace logging |
| 25 | { |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 26 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 27 | void Manager::commit(uint64_t transactionId, std::string errMsg) |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 28 | { |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 29 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 30 | // Length of 'TRANSACTION_ID' string. |
Adriana Kobylak | 6721899 | 2017-02-28 12:53:37 -0600 | [diff] [blame] | 31 | constexpr const auto transactionIdVarSize = strlen(transactionIdVar); |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 32 | // Length of 'TRANSACTION_ID=' string. |
| 33 | constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 34 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 35 | sd_journal *j = nullptr; |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 36 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 37 | if (rc < 0) |
| 38 | { |
| 39 | logging::log<logging::level::ERR>("Failed to open journal", |
| 40 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 41 | return; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 44 | std::string transactionIdStr = std::to_string(transactionId); |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 45 | std::set<std::string> metalist; |
| 46 | auto metamap = g_errMetaMap.find(errMsg); |
| 47 | if (metamap != g_errMetaMap.end()) |
| 48 | { |
| 49 | metalist.insert(metamap->second.begin(), metamap->second.end()); |
| 50 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 51 | const auto& metalistHostEvent = g_errMetaMapHostEvent[errMsg]; |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 52 | std::vector<std::string> additionalData; |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 53 | |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 54 | // TODO Remove once host event error header file is auto-generated. |
| 55 | // Also make metalist a const variable. |
| 56 | // Tracking with issue openbmc/phosphor-logging#4 |
| 57 | for (auto& metaVarStrHostEvent : metalistHostEvent) |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 58 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 59 | metalist.insert(metaVarStrHostEvent); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 60 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 61 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 62 | // Read the journal from the end to get the most recent entry first. |
| 63 | // The result from the sd_journal_get_data() is of the form VARIABLE=value. |
| 64 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 65 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 66 | const char *data = nullptr; |
| 67 | size_t length = 0; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 68 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 69 | // Look for the transaction id metadata variable |
| 70 | rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data, |
| 71 | &length); |
| 72 | if (rc < 0) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 73 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 74 | // This journal entry does not have the TRANSACTION_ID |
| 75 | // metadata variable. |
| 76 | continue; |
| 77 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 78 | |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 79 | // journald does not guarantee that sd_journal_get_data() returns NULL |
| 80 | // terminated strings, so need to specify the size to use to compare, |
| 81 | // use the returned length instead of anything that relies on NULL |
| 82 | // terminators like strlen(). |
| 83 | // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove |
| 84 | // the TRANSACTION_ID characters plus the (=) sign to do the comparison. |
| 85 | // 'data + transactionIdVarOffset' will be in the form of '1234'. |
| 86 | // 'length - transactionIdVarOffset' will be the length of '1234'. |
| 87 | if ((length <= (transactionIdVarOffset)) || |
| 88 | (transactionIdStr.compare(0, |
| 89 | transactionIdStr.size(), |
| 90 | data + transactionIdVarOffset, |
| 91 | length - transactionIdVarOffset) != 0)) |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 92 | { |
| 93 | // The value of the TRANSACTION_ID metadata is not the requested |
| 94 | // transaction id number. |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | // Search for all metadata variables in the current journal entry. |
| 99 | for (auto i = metalist.cbegin(); i != metalist.cend();) |
| 100 | { |
| 101 | rc = sd_journal_get_data(j, (*i).c_str(), |
| 102 | (const void **)&data, &length); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 103 | if (rc < 0) |
| 104 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 105 | // Metadata variable not found, check next metadata variable. |
| 106 | i++; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 107 | continue; |
| 108 | } |
| 109 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 110 | // Metadata variable found, save it and remove it from the set. |
| 111 | additionalData.emplace_back(data, length); |
| 112 | i = metalist.erase(i); |
| 113 | } |
| 114 | if (metalist.empty()) |
| 115 | { |
| 116 | // All metadata variables found, break out of journal loop. |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 117 | break; |
| 118 | } |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 119 | } |
| 120 | if (!metalist.empty()) |
| 121 | { |
| 122 | // Not all the metadata variables were found in the journal. |
| 123 | for (auto& metaVarStr : metalist) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 124 | { |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 125 | logging::log<logging::level::INFO>("Failed to find metadata", |
| 126 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 130 | sd_journal_close(j); |
| 131 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 132 | // Create error Entry dbus object |
| 133 | entryId++; |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 134 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 135 | std::chrono::system_clock::now().time_since_epoch()).count(); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 136 | auto objPath = std::string(OBJ_ENTRY) + '/' + |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 137 | std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 138 | |
Deepak Kodihalli | 35b4637 | 2017-02-27 04:58:18 -0600 | [diff] [blame] | 139 | AssociationList objects {}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 140 | processMetadata(errMsg, additionalData, objects); |
| 141 | |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 142 | level reqLevel = level::INFO; // Default to INFO |
| 143 | auto levelmap = g_errLevelMap.find(errMsg); |
| 144 | if (levelmap != g_errLevelMap.end()) |
| 145 | { |
| 146 | reqLevel = levelmap->second; |
| 147 | } |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 148 | entries.insert(std::make_pair(entryId, std::make_unique<Entry>( |
| 149 | busLog, |
| 150 | objPath, |
| 151 | entryId, |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 152 | ms, // Milliseconds since 1970 |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 153 | static_cast<Entry::Level>(reqLevel), |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 154 | std::move(errMsg), |
Deepak Kodihalli | 35b4637 | 2017-02-27 04:58:18 -0600 | [diff] [blame] | 155 | std::move(additionalData), |
Deepak Kodihalli | 8110ca6 | 2017-04-10 02:11:54 -0500 | [diff] [blame] | 156 | std::move(objects), |
| 157 | *this))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 158 | return; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 161 | void Manager::processMetadata(const std::string& errorName, |
| 162 | const std::vector<std::string>& additionalData, |
| 163 | AssociationList& objects) const |
| 164 | { |
| 165 | // additionalData is a list of "metadata=value" |
| 166 | constexpr auto separator = '='; |
| 167 | for(const auto& entry: additionalData) |
| 168 | { |
| 169 | auto found = entry.find(separator); |
| 170 | if(std::string::npos != found) |
| 171 | { |
| 172 | auto metadata = entry.substr(0, found); |
| 173 | auto iter = meta.find(metadata); |
| 174 | if(meta.end() != iter) |
| 175 | { |
| 176 | (iter->second)(metadata, additionalData, objects); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 182 | void Manager::erase(uint32_t entryId) |
| 183 | { |
| 184 | auto entry = entries.find(entryId); |
| 185 | if(entries.end() != entry) |
| 186 | { |
| 187 | entries.erase(entry); |
| 188 | } |
| 189 | } |
| 190 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 191 | } // namespace logging |
| 192 | } // namepsace phosphor |