blob: 716a99968ea8529bb0ed1ba5db04793b5c6ea9c1 [file] [log] [blame]
Bob Kingca08a792020-09-30 14:30:13 +08001/**
2 * Copyright © 2020 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
17#include "journal.hpp"
18
Shawn McCarneya528a282021-02-15 18:07:07 -060019#include <errno.h>
20#include <stdint.h>
Bob Kingca08a792020-09-30 14:30:13 +080021#include <string.h>
22#include <time.h>
23
Shawn McCarneya528a282021-02-15 18:07:07 -060024#include <chrono>
Bob Kingca08a792020-09-30 14:30:13 +080025#include <stdexcept>
Shawn McCarneya528a282021-02-15 18:07:07 -060026#include <thread>
Bob Kingca08a792020-09-30 14:30:13 +080027
28namespace phosphor::power::regulators
29{
30
Shawn McCarneya528a282021-02-15 18:07:07 -060031/**
32 * @class JournalCloser
33 *
34 * Automatically closes the journal when the object goes out of scope.
35 */
Bob Kingca08a792020-09-30 14:30:13 +080036class JournalCloser
37{
38 public:
Shawn McCarneya528a282021-02-15 18:07:07 -060039 // Specify which compiler-generated methods we want
Bob Kingca08a792020-09-30 14:30:13 +080040 JournalCloser() = delete;
41 JournalCloser(const JournalCloser&) = delete;
42 JournalCloser(JournalCloser&&) = delete;
43 JournalCloser& operator=(const JournalCloser&) = delete;
44 JournalCloser& operator=(JournalCloser&&) = delete;
45
Patrick Williams48781ae2023-05-10 07:50:50 -050046 explicit JournalCloser(sd_journal* journal) : journal{journal} {}
Shawn McCarneya528a282021-02-15 18:07:07 -060047
Bob Kingca08a792020-09-30 14:30:13 +080048 ~JournalCloser()
49 {
50 sd_journal_close(journal);
51 }
52
53 private:
54 sd_journal* journal{nullptr};
55};
56
Bob Kingca08a792020-09-30 14:30:13 +080057std::vector<std::string>
58 SystemdJournal::getMessages(const std::string& field,
59 const std::string& fieldValue, unsigned int max)
60{
Shawn McCarneya528a282021-02-15 18:07:07 -060061 // Sleep 100ms; otherwise recent journal entries sometimes not available
62 using namespace std::chrono_literals;
63 std::this_thread::sleep_for(100ms);
Bob Kingca08a792020-09-30 14:30:13 +080064
Shawn McCarneya528a282021-02-15 18:07:07 -060065 // Open the journal
66 sd_journal* journal;
Bob Kingca08a792020-09-30 14:30:13 +080067 int rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
68 if (rc < 0)
69 {
70 throw std::runtime_error{std::string{"Failed to open journal: "} +
71 strerror(-rc)};
72 }
73
Shawn McCarneya528a282021-02-15 18:07:07 -060074 // Create object to automatically close journal
Bob Kingca08a792020-09-30 14:30:13 +080075 JournalCloser closer{journal};
76
Shawn McCarneya528a282021-02-15 18:07:07 -060077 // Add match so we only loop over entries with specified field value
78 std::string match{field + '=' + fieldValue};
79 rc = sd_journal_add_match(journal, match.c_str(), 0);
80 if (rc < 0)
81 {
82 throw std::runtime_error{std::string{"Failed to add journal match: "} +
83 strerror(-rc)};
84 }
85
86 // Loop through matching entries from newest to oldest
87 std::vector<std::string> messages;
88 messages.reserve((max != 0) ? max : 10);
89 std::string syslogID, pid, message, timeStamp, line;
Bob Kingca08a792020-09-30 14:30:13 +080090 SD_JOURNAL_FOREACH_BACKWARDS(journal)
91 {
Shawn McCarneya528a282021-02-15 18:07:07 -060092 // Get relevant journal entry fields
93 timeStamp = getTimeStamp(journal);
94 syslogID = getFieldValue(journal, "SYSLOG_IDENTIFIER");
95 pid = getFieldValue(journal, "_PID");
96 message = getFieldValue(journal, "MESSAGE");
Bob Kingca08a792020-09-30 14:30:13 +080097
Shawn McCarneya528a282021-02-15 18:07:07 -060098 // Build one line string containing field values
99 line = timeStamp + " " + syslogID + "[" + pid + "]: " + message;
100 messages.emplace(messages.begin(), line);
Bob Kingca08a792020-09-30 14:30:13 +0800101
Shawn McCarneya528a282021-02-15 18:07:07 -0600102 // Stop looping if a max was specified and we have reached it
Bob Kingca08a792020-09-30 14:30:13 +0800103 if ((max != 0) && (messages.size() >= max))
104 {
105 break;
106 }
107 }
108
109 return messages;
110}
111
Shawn McCarneya528a282021-02-15 18:07:07 -0600112std::string SystemdJournal::getFieldValue(sd_journal* journal,
113 const std::string& field)
114{
115 std::string value{};
116
117 // Get field data from current journal entry
118 const void* data{nullptr};
119 size_t length{0};
120 int rc = sd_journal_get_data(journal, field.c_str(), &data, &length);
121 if (rc < 0)
122 {
123 if (-rc == ENOENT)
124 {
125 // Current entry does not include this field; return empty value
126 return value;
127 }
128 else
129 {
130 throw std::runtime_error{
131 std::string{"Failed to read journal entry field: "} +
132 strerror(-rc)};
133 }
134 }
135
136 // Get value from field data. Field data in format "FIELD=value".
137 std::string dataString{static_cast<const char*>(data), length};
138 std::string::size_type pos = dataString.find('=');
139 if ((pos != std::string::npos) && ((pos + 1) < dataString.size()))
140 {
141 // Value is substring after the '='
142 value = dataString.substr(pos + 1);
143 }
144
145 return value;
146}
147
148std::string SystemdJournal::getTimeStamp(sd_journal* journal)
149{
150 // Get realtime (wallclock) timestamp of current journal entry. The
151 // timestamp is in microseconds since the epoch.
152 uint64_t usec{0};
153 int rc = sd_journal_get_realtime_usec(journal, &usec);
154 if (rc < 0)
155 {
156 throw std::runtime_error{
157 std::string{"Failed to get journal entry timestamp: "} +
158 strerror(-rc)};
159 }
160
161 // Convert to number of seconds since the epoch
162 time_t secs = usec / 1000000;
163
164 // Convert seconds to tm struct required by strftime()
165 struct tm* timeStruct = localtime(&secs);
166 if (timeStruct == nullptr)
167 {
168 throw std::runtime_error{
169 std::string{"Invalid journal entry timestamp: "} + strerror(errno)};
170 }
171
172 // Convert tm struct into a date/time string
173 char timeStamp[80];
174 strftime(timeStamp, sizeof(timeStamp), "%b %d %H:%M:%S", timeStruct);
175
176 return timeStamp;
177}
178
179} // namespace phosphor::power::regulators