blob: 94a9f5279795d5ed8174af6b195594dd17ca1491 [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
Patrick Williamsf5402192024-08-16 15:20:53 -040057std::vector<std::string> SystemdJournal::getMessages(
58 const std::string& field, const std::string& fieldValue, unsigned int max)
Bob Kingca08a792020-09-30 14:30:13 +080059{
Shawn McCarneya528a282021-02-15 18:07:07 -060060 // Sleep 100ms; otherwise recent journal entries sometimes not available
61 using namespace std::chrono_literals;
62 std::this_thread::sleep_for(100ms);
Bob Kingca08a792020-09-30 14:30:13 +080063
Shawn McCarneya528a282021-02-15 18:07:07 -060064 // Open the journal
65 sd_journal* journal;
Bob Kingca08a792020-09-30 14:30:13 +080066 int rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
67 if (rc < 0)
68 {
Patrick Williamsf5402192024-08-16 15:20:53 -040069 throw std::runtime_error{
70 std::string{"Failed to open journal: "} + strerror(-rc)};
Bob Kingca08a792020-09-30 14:30:13 +080071 }
72
Shawn McCarneya528a282021-02-15 18:07:07 -060073 // Create object to automatically close journal
Bob Kingca08a792020-09-30 14:30:13 +080074 JournalCloser closer{journal};
75
Shawn McCarneya528a282021-02-15 18:07:07 -060076 // Add match so we only loop over entries with specified field value
77 std::string match{field + '=' + fieldValue};
78 rc = sd_journal_add_match(journal, match.c_str(), 0);
79 if (rc < 0)
80 {
Patrick Williamsf5402192024-08-16 15:20:53 -040081 throw std::runtime_error{
82 std::string{"Failed to add journal match: "} + strerror(-rc)};
Shawn McCarneya528a282021-02-15 18:07:07 -060083 }
84
85 // Loop through matching entries from newest to oldest
86 std::vector<std::string> messages;
87 messages.reserve((max != 0) ? max : 10);
88 std::string syslogID, pid, message, timeStamp, line;
Bob Kingca08a792020-09-30 14:30:13 +080089 SD_JOURNAL_FOREACH_BACKWARDS(journal)
90 {
Shawn McCarneya528a282021-02-15 18:07:07 -060091 // Get relevant journal entry fields
92 timeStamp = getTimeStamp(journal);
93 syslogID = getFieldValue(journal, "SYSLOG_IDENTIFIER");
94 pid = getFieldValue(journal, "_PID");
95 message = getFieldValue(journal, "MESSAGE");
Bob Kingca08a792020-09-30 14:30:13 +080096
Shawn McCarneya528a282021-02-15 18:07:07 -060097 // Build one line string containing field values
98 line = timeStamp + " " + syslogID + "[" + pid + "]: " + message;
99 messages.emplace(messages.begin(), line);
Bob Kingca08a792020-09-30 14:30:13 +0800100
Shawn McCarneya528a282021-02-15 18:07:07 -0600101 // Stop looping if a max was specified and we have reached it
Bob Kingca08a792020-09-30 14:30:13 +0800102 if ((max != 0) && (messages.size() >= max))
103 {
104 break;
105 }
106 }
107
108 return messages;
109}
110
Shawn McCarneya528a282021-02-15 18:07:07 -0600111std::string SystemdJournal::getFieldValue(sd_journal* journal,
112 const std::string& field)
113{
114 std::string value{};
115
116 // Get field data from current journal entry
117 const void* data{nullptr};
118 size_t length{0};
119 int rc = sd_journal_get_data(journal, field.c_str(), &data, &length);
120 if (rc < 0)
121 {
122 if (-rc == ENOENT)
123 {
124 // Current entry does not include this field; return empty value
125 return value;
126 }
127 else
128 {
129 throw std::runtime_error{
130 std::string{"Failed to read journal entry field: "} +
131 strerror(-rc)};
132 }
133 }
134
135 // Get value from field data. Field data in format "FIELD=value".
136 std::string dataString{static_cast<const char*>(data), length};
137 std::string::size_type pos = dataString.find('=');
138 if ((pos != std::string::npos) && ((pos + 1) < dataString.size()))
139 {
140 // Value is substring after the '='
141 value = dataString.substr(pos + 1);
142 }
143
144 return value;
145}
146
147std::string SystemdJournal::getTimeStamp(sd_journal* journal)
148{
149 // Get realtime (wallclock) timestamp of current journal entry. The
150 // timestamp is in microseconds since the epoch.
151 uint64_t usec{0};
152 int rc = sd_journal_get_realtime_usec(journal, &usec);
153 if (rc < 0)
154 {
155 throw std::runtime_error{
156 std::string{"Failed to get journal entry timestamp: "} +
157 strerror(-rc)};
158 }
159
160 // Convert to number of seconds since the epoch
161 time_t secs = usec / 1000000;
162
163 // Convert seconds to tm struct required by strftime()
164 struct tm* timeStruct = localtime(&secs);
165 if (timeStruct == nullptr)
166 {
167 throw std::runtime_error{
168 std::string{"Invalid journal entry timestamp: "} + strerror(errno)};
169 }
170
171 // Convert tm struct into a date/time string
172 char timeStamp[80];
173 strftime(timeStamp, sizeof(timeStamp), "%b %d %H:%M:%S", timeStruct);
174
175 return timeStamp;
176}
177
178} // namespace phosphor::power::regulators