1 /** 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 19 #include <errno.h> 20 #include <stdint.h> 21 #include <string.h> 22 #include <time.h> 23 24 #include <chrono> 25 #include <stdexcept> 26 #include <thread> 27 28 namespace phosphor::power::regulators 29 { 30 31 /** 32 * @class JournalCloser 33 * 34 * Automatically closes the journal when the object goes out of scope. 35 */ 36 class JournalCloser 37 { 38 public: 39 // Specify which compiler-generated methods we want 40 JournalCloser() = delete; 41 JournalCloser(const JournalCloser&) = delete; 42 JournalCloser(JournalCloser&&) = delete; 43 JournalCloser& operator=(const JournalCloser&) = delete; 44 JournalCloser& operator=(JournalCloser&&) = delete; 45 46 explicit JournalCloser(sd_journal* journal) : journal{journal} {} 47 48 ~JournalCloser() 49 { 50 sd_journal_close(journal); 51 } 52 53 private: 54 sd_journal* journal{nullptr}; 55 }; 56 57 std::vector<std::string> SystemdJournal::getMessages( 58 const std::string& field, const std::string& fieldValue, unsigned int max) 59 { 60 // Sleep 100ms; otherwise recent journal entries sometimes not available 61 using namespace std::chrono_literals; 62 std::this_thread::sleep_for(100ms); 63 64 // Open the journal 65 sd_journal* journal; 66 int rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY); 67 if (rc < 0) 68 { 69 throw std::runtime_error{ 70 std::string{"Failed to open journal: "} + strerror(-rc)}; 71 } 72 73 // Create object to automatically close journal 74 JournalCloser closer{journal}; 75 76 // 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 { 81 throw std::runtime_error{ 82 std::string{"Failed to add journal match: "} + strerror(-rc)}; 83 } 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; 89 SD_JOURNAL_FOREACH_BACKWARDS(journal) 90 { 91 // 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"); 96 97 // Build one line string containing field values 98 line = timeStamp + " " + syslogID + "[" + pid + "]: " + message; 99 messages.emplace(messages.begin(), line); 100 101 // Stop looping if a max was specified and we have reached it 102 if ((max != 0) && (messages.size() >= max)) 103 { 104 break; 105 } 106 } 107 108 return messages; 109 } 110 111 std::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 147 std::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 179