blob: a3e49e97e0ad27c3c8a1d61a5e594e4c98706c9d [file] [log] [blame]
Adriana Kobylakd311bc82016-10-16 09:54:40 -05001#include <fstream>
2#include <iostream>
3#include <cstdio>
4#include <string>
5#include <vector>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -05006#include <sdbusplus/vtable.hpp>
7#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05008#include <systemd/sd-journal.h>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -05009#include "log.hpp"
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060010#include "log_manager.hpp"
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050011
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060012namespace phosphor
13{
14namespace logging
15{
Adriana Kobylakd311bc82016-10-16 09:54:40 -050016
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060017void Manager::commit(uint64_t transactionId, std::string errMsg)
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050018{
Adriana Kobylakd311bc82016-10-16 09:54:40 -050019 // TODO Change /tmp path to a permanent location on flash
20 constexpr const auto path = "/tmp/elog";
21 constexpr const auto msgIdStr = "_PID";
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050022
Adriana Kobylakd311bc82016-10-16 09:54:40 -050023 // 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 Kobylak8f7941e2016-11-14 14:46:23 -060027 // on flash is determined. Ex: ../transactionId/1
Adriana Kobylakd311bc82016-10-16 09:54:40 -050028 std::ofstream efile;
29 efile.open(filename);
30 efile << "{" << std::endl;
31
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060032 //const char* metaVar = nullptr;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050033 std::vector<const char*> metaList;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050034
35 sd_journal *j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060036 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050037 if (rc < 0)
38 {
39 logging::log<logging::level::ERR>("Failed to open journal",
40 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060041 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050042 }
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 Kobylak8f7941e2016-11-14 14:46:23 -060059// 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 Kobylakd311bc82016-10-16 09:54:40 -050064 {
65 // Match not found, continue to next journal entry
66 continue;
67 }
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060068#endif
Adriana Kobylakd311bc82016-10-16 09:54:40 -050069 // 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 Kobylak8f7941e2016-11-14 14:46:23 -0600108 return;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500109}
110
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600111Manager::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 Kobylak1db1bd32016-10-10 11:39:20 -0500117{
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600118 _bus.request_name(busname);
119}
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500120
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600121void Manager::run() noexcept
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500122{
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600123 while(true)
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500124 {
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600125 try
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500126 {
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600127 _bus.process_discard();
128 _bus.wait();
129 }
130 catch (std::exception &e)
131 {
132 std::cerr << e.what() << std::endl;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500133 }
134 }
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500135}
136
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600137} // namespace logging
138} // namepsace phosphor