blob: 0a728f05fad42f756ca0229d037709bd33fd5163 [file] [log] [blame]
Matt Spinlerd96fa602022-12-15 11:11:26 -06001/**
2 * Copyright © 2023 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "journal.hpp"
17
18#include <phosphor-logging/log.hpp>
19
20#include <stdexcept>
21#include <thread>
22
23namespace openpower::pels
24{
25
26using namespace phosphor::logging;
27
28/**
29 * @class JournalCloser
30 *
31 * Closes the journal on destruction
32 */
33class JournalCloser
34{
35 public:
36 JournalCloser() = delete;
37 JournalCloser(const JournalCloser&) = delete;
38 JournalCloser(JournalCloser&&) = delete;
39 JournalCloser& operator=(const JournalCloser&) = delete;
40 JournalCloser& operator=(JournalCloser&&) = delete;
41
42 explicit JournalCloser(sd_journal* journal) : journal{journal} {}
43
44 ~JournalCloser()
45 {
46 sd_journal_close(journal);
47 }
48
49 private:
50 sd_journal* journal{nullptr};
51};
52
53std::vector<std::string> Journal::getMessages(const std::string& syslogID,
54 size_t maxMessages) const
55{
56 // The message registry JSON schema will also fail if a zero is in the JSON
57 if (0 == maxMessages)
58 {
59 log<level::ERR>(
60 "maxMessages value of zero passed into Journal::getMessages");
61 return std::vector<std::string>{};
62 }
63
64 sd_journal* journal;
65 int rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
66 if (rc < 0)
67 {
68 throw std::runtime_error{std::string{"Failed to open journal: "} +
69 strerror(-rc)};
70 }
71
72 JournalCloser closer{journal};
73
74 if (!syslogID.empty())
75 {
76 std::string match{"SYSLOG_IDENTIFIER=" + syslogID};
77
78 rc = sd_journal_add_match(journal, match.c_str(), 0);
79 if (rc < 0)
80 {
81 throw std::runtime_error{
82 std::string{"Failed to add journal match: "} + strerror(-rc)};
83 }
84 }
85
86 // Loop through matching entries from newest to oldest
87 std::vector<std::string> messages;
88 messages.reserve(maxMessages);
89 std::string sID, pid, message, timeStamp, line;
90
91 SD_JOURNAL_FOREACH_BACKWARDS(journal)
92 {
93 timeStamp = getTimeStamp(journal);
94 sID = getFieldValue(journal, "SYSLOG_IDENTIFIER");
95 pid = getFieldValue(journal, "_PID");
96 message = getFieldValue(journal, "MESSAGE");
97
98 line = timeStamp + " " + sID + "[" + pid + "]: " + message;
99 messages.emplace(messages.begin(), line);
100
101 if (messages.size() >= maxMessages)
102 {
103 break;
104 }
105 }
106
107 return messages;
108}
109
110std::string Journal::getFieldValue(sd_journal* journal,
111 const std::string& field) const
112{
113 std::string value{};
114
115 const void* data{nullptr};
116 size_t length{0};
117 int rc = sd_journal_get_data(journal, field.c_str(), &data, &length);
118 if (rc < 0)
119 {
120 if (-rc == ENOENT)
121 {
122 // Current entry does not include this field; return empty value
123 return value;
124 }
125 else
126 {
127 throw std::runtime_error{
128 std::string{"Failed to read journal entry field: "} +
129 strerror(-rc)};
130 }
131 }
132
133 // Get value from field data. Field data in format "FIELD=value".
134 std::string dataString{static_cast<const char*>(data), length};
135 std::string::size_type pos = dataString.find('=');
136 if ((pos != std::string::npos) && ((pos + 1) < dataString.size()))
137 {
138 // Value is substring after the '='
139 value = dataString.substr(pos + 1);
140 }
141
142 return value;
143}
144
145std::string Journal::getTimeStamp(sd_journal* journal) const
146{
147 // Get realtime (wallclock) timestamp of current journal entry. The
148 // timestamp is in microseconds since the epoch.
149 uint64_t usec{0};
150 int rc = sd_journal_get_realtime_usec(journal, &usec);
151 if (rc < 0)
152 {
153 throw std::runtime_error{
154 std::string{"Failed to get journal entry timestamp: "} +
155 strerror(-rc)};
156 }
157
158 // Convert to number of seconds since the epoch
159 time_t secs = usec / 1000000;
160
161 // Convert seconds to tm struct required by strftime()
162 struct tm* timeStruct = localtime(&secs);
163 if (timeStruct == nullptr)
164 {
165 throw std::runtime_error{
166 std::string{"Invalid journal entry timestamp: "} + strerror(errno)};
167 }
168
169 // Convert tm struct into a date/time string
170 char timeStamp[80];
171 strftime(timeStamp, sizeof(timeStamp), "%b %d %H:%M:%S", timeStruct);
172
173 return timeStamp;
174}
175
176} // namespace openpower::pels