Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 1 | #include <fstream> |
| 2 | #include <iostream> |
| 3 | #include <cstdio> |
| 4 | #include <string> |
| 5 | #include <vector> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 6 | #include <sdbusplus/vtable.hpp> |
| 7 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 8 | #include <systemd/sd-journal.h> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 9 | #include "log.hpp" |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 10 | #include "log_manager.hpp" |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 11 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace logging |
| 15 | { |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 16 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 17 | void Manager::commit(uint64_t transactionId, std::string errMsg) |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 18 | { |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 19 | // TODO Change /tmp path to a permanent location on flash |
| 20 | constexpr const auto path = "/tmp/elog"; |
| 21 | constexpr const auto msgIdStr = "_PID"; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 22 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 23 | // Create log file |
| 24 | std::string filename{}; |
| 25 | filename.append(path); |
| 26 | // TODO Create error logs in their own separate dir once permanent location |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 27 | // on flash is determined. Ex: ../transactionId/1 |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 28 | std::ofstream efile; |
| 29 | efile.open(filename); |
| 30 | efile << "{" << std::endl; |
| 31 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 32 | //const char* metaVar = nullptr; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 33 | std::vector<const char*> metaList; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 34 | |
| 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 | |
| 44 | // Read the journal from the end to get the most recent entry first. |
| 45 | // The result from the sd_journal_get_data() is of the form VARIABLE=value. |
| 46 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
| 47 | { |
| 48 | const char *data = nullptr; |
| 49 | size_t length = 0; |
| 50 | |
| 51 | // Search for the msg id |
| 52 | rc = sd_journal_get_data(j, msgIdStr, (const void **)&data, &length); |
| 53 | if (rc < 0) |
| 54 | { |
| 55 | // Instance not found, continue to next journal entry |
| 56 | continue; |
| 57 | } |
| 58 | std::string result(data); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 59 | // TODO String msgid is now an int transaction id. This piece will be |
| 60 | // uncommented and reworked with the upcoming change to read the metadata |
| 61 | // fields from the header file. |
| 62 | #if 0 |
| 63 | if (result.find(msgid) == std::string::npos) |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 64 | { |
| 65 | // Match not found, continue to next journal entry |
| 66 | continue; |
| 67 | } |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 68 | #endif |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 69 | // Match found, write to file |
| 70 | // TODO This is a draft format based on the redfish event logs written |
| 71 | // in json, the final openbmc format is to be determined |
| 72 | efile << "\t{" << std::endl; |
| 73 | efile << "\t\"@" << data << "\"," << std::endl; |
| 74 | |
| 75 | // Include the journal message |
| 76 | rc = sd_journal_get_data(j, "MESSAGE", (const void **)&data, &length); |
| 77 | if (rc < 0) |
| 78 | { |
| 79 | continue; |
| 80 | } |
| 81 | efile << "\t\"@" << data << "\"," << std::endl; |
| 82 | |
| 83 | // Search for the metadata variables in the current journal entry |
| 84 | for (auto i : metaList) |
| 85 | { |
| 86 | rc = sd_journal_get_data(j, i, (const void **)&data, &length); |
| 87 | if (rc < 0) |
| 88 | { |
| 89 | // Not found, continue to next metadata variable |
| 90 | logging::log<logging::level::INFO>("Failed to find metadata", |
| 91 | logging::entry("META_FIELD=%s", i)); |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | // Metatdata variable found, write to file |
| 96 | efile << "\t\"@" << data << "\"," << std::endl; |
| 97 | } |
| 98 | efile << "\t}" << std::endl; |
| 99 | |
| 100 | // TODO Break only once all metadata fields have been found. Implement |
| 101 | // once this function reads the metadata fields from the header file. |
| 102 | break; |
| 103 | } |
| 104 | sd_journal_close(j); |
| 105 | |
| 106 | efile << "}" << std::endl; |
| 107 | efile.close(); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 108 | return; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 111 | Manager::Manager(sdbusplus::bus::bus &&bus, |
| 112 | const char* busname, |
| 113 | const char* obj) : |
| 114 | details::ServerObject<details::ManagerIface>(bus, obj), |
| 115 | _bus(std::move(bus)), |
| 116 | _manager(sdbusplus::server::manager::manager(_bus, obj)) |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 117 | { |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 118 | _bus.request_name(busname); |
| 119 | } |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 120 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 121 | void Manager::run() noexcept |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 122 | { |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 123 | while(true) |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 124 | { |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 125 | try |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 126 | { |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 127 | _bus.process_discard(); |
| 128 | _bus.wait(); |
| 129 | } |
| 130 | catch (std::exception &e) |
| 131 | { |
| 132 | std::cerr << e.what() << std::endl; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 133 | } |
| 134 | } |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 137 | } // namespace logging |
| 138 | } // namepsace phosphor |