2023-01-19 16:34:03 +00:00
|
|
|
#include <climits>
|
2023-01-20 14:41:06 +00:00
|
|
|
#include <cstring>
|
2023-01-19 16:34:03 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2023-03-29 10:08:31 +00:00
|
|
|
#include "config.h"
|
2023-01-19 16:34:03 +00:00
|
|
|
#include "logcat_entry.h"
|
|
|
|
#include "pcre2_wrapper.h"
|
|
|
|
|
2023-01-20 14:41:06 +00:00
|
|
|
static const Pcre2Regex LogcatEntryRegex("^\\s*(\\d+)(?:\\.\\d+)?(?:\\s+([\\w\\d._-]+))?\\s+(\\d+)\\s+(\\d+)\\s+([A-Z])\\s+(.+?)\\s*:\\s(.*)$");
|
|
|
|
static const Pcre2Regex BufferRegex("^--------- (?:beginning of|switch to) (\\w+)$");
|
2023-01-19 16:34:03 +00:00
|
|
|
|
2023-02-09 07:44:34 +00:00
|
|
|
const char* to_string(Priority priority) {
|
2023-01-19 16:34:03 +00:00
|
|
|
switch (priority) {
|
|
|
|
case Priority::Verbose: return "Verbose";
|
|
|
|
case Priority::Debug: return "Debug";
|
|
|
|
case Priority::Info: return "Info";
|
|
|
|
case Priority::Warn: return "Warning";
|
|
|
|
case Priority::Error: return "Error";
|
|
|
|
case Priority::Fatal: return "Fatal";
|
2023-02-09 07:44:34 +00:00
|
|
|
case Priority::Unknown: return "Unknown";
|
2023-01-19 16:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:44:34 +00:00
|
|
|
const char* to_string(Buffer buffer) {
|
2023-01-19 16:34:03 +00:00
|
|
|
switch (buffer) {
|
|
|
|
case Buffer::Main: return "Main";
|
|
|
|
case Buffer::System: return "System";
|
|
|
|
case Buffer::Radio: return "Radio";
|
|
|
|
case Buffer::Events: return "Events";
|
|
|
|
case Buffer::Crash: return "Crash";
|
2023-02-09 07:44:34 +00:00
|
|
|
case Buffer::Unknown: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 14:28:31 +00:00
|
|
|
const char* to_string_lower(Buffer buffer) {
|
|
|
|
switch (buffer) {
|
|
|
|
case Buffer::Main: return "main";
|
|
|
|
case Buffer::System: return "system";
|
|
|
|
case Buffer::Radio: return "radio";
|
|
|
|
case Buffer::Events: return "events";
|
|
|
|
case Buffer::Crash: return "crash";
|
|
|
|
case Buffer::Unknown: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:27:39 +00:00
|
|
|
static std::string leftpad(const std::string& str, size_t characters) {
|
|
|
|
return str.size() >= characters
|
|
|
|
? str
|
|
|
|
: std::string(characters - str.size(), ' ') + str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char to_char(Priority priority) {
|
|
|
|
switch (priority) {
|
|
|
|
case Priority::Verbose: return 'V';
|
|
|
|
case Priority::Debug: return 'D';
|
|
|
|
case Priority::Info: return 'I';
|
|
|
|
case Priority::Warn: return 'W';
|
|
|
|
case Priority::Error: return 'E';
|
|
|
|
case Priority::Fatal: return 'F';
|
|
|
|
case Priority::Unknown: return 'U';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline std::string rightpad(const std::string& str, size_t characters) {
|
|
|
|
return str.size() >= characters
|
|
|
|
? str
|
|
|
|
: str + std::string(characters - str.size(), ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string to_string(const LogcatEntry& logcat_entry) {
|
|
|
|
char logcat_style_time_as_str[32] = {0};
|
|
|
|
struct tm tm;
|
|
|
|
strftime(logcat_style_time_as_str, 31 * sizeof(char), "%Y-%m-%d %H:%M:%S", localtime_r(&logcat_entry.time, &tm));
|
|
|
|
|
|
|
|
return std::string(logcat_style_time_as_str)
|
|
|
|
+ ' ' + leftpad(logcat_entry.user.value_or(" "), 5)
|
|
|
|
+ ' ' + leftpad(std::to_string(logcat_entry.pid), 5)
|
|
|
|
+ ' ' + leftpad(std::to_string(logcat_entry.tid), 5)
|
|
|
|
+ ' ' + to_char(logcat_entry.priority)
|
|
|
|
+ ' ' + rightpad(logcat_entry.tag, 5)
|
|
|
|
+ ": " + logcat_entry.message;
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:44:34 +00:00
|
|
|
static inline Priority priority_from(char c) {
|
|
|
|
switch (c) {
|
|
|
|
case 'V': return Priority::Verbose;
|
|
|
|
case 'D': return Priority::Debug;
|
|
|
|
case 'I': return Priority::Info;
|
|
|
|
case 'W': return Priority::Warn;
|
|
|
|
case 'E': return Priority::Error;
|
|
|
|
case 'F': return Priority::Fatal;
|
|
|
|
default: return Priority::Unknown;
|
2023-01-19 16:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline long to_long(const char* str, const char* expected_end) {
|
|
|
|
char* endptr;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
long res = strtol(str, &endptr, 10);
|
|
|
|
if (endptr != expected_end) {
|
|
|
|
throw std::invalid_argument(std::string(str) + " has trailing text");
|
|
|
|
} else if (res == LONG_MAX && errno == ERANGE) {
|
|
|
|
throw std::overflow_error(std::string(str) + " is too big");
|
|
|
|
} else if (res == LONG_MIN && errno == ERANGE) {
|
|
|
|
throw std::underflow_error(std::string(str) + " is too small");
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long long to_unsigned_long_long(const char* str, const char* expected_end) {
|
|
|
|
char* endptr;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
unsigned long long res = strtoull(str, &endptr, 10);
|
|
|
|
if (endptr != expected_end) {
|
|
|
|
throw std::invalid_argument(std::string(str) + " has trailing text");
|
|
|
|
} else if (res == ULLONG_MAX && errno == ERANGE) {
|
|
|
|
throw std::overflow_error(std::string(str) + " is too big");
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<LogcatEntry> try_parse_logcat_entry(char* buf, size_t length, Buffer buffer) {
|
|
|
|
regmatch_t matches[8];
|
|
|
|
matches[0].rm_so = 0;
|
|
|
|
matches[0].rm_eo = static_cast<regoff_t>(length);
|
|
|
|
if (static_cast<size_t>(matches[0].rm_eo) != length) {
|
|
|
|
throw std::range_error("Logcat entry line too long for int");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!LogcatEntryRegex.match(buf, sizeof(matches) / sizeof(regmatch_t), matches, REG_STARTEND)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> user;
|
|
|
|
if (matches[2].rm_so > -1 && matches[2].rm_eo > -1) {
|
|
|
|
user = std::string(&buf[matches[2].rm_so], static_cast<size_t>(matches[2].rm_eo - matches[2].rm_so));
|
|
|
|
}
|
|
|
|
// if pcre2 gives us negative offsets then i'll die
|
|
|
|
LogcatEntry logcat_entry = {
|
|
|
|
.buffer = buffer,
|
|
|
|
.time = to_long(&buf[matches[1].rm_so], &buf[matches[1].rm_eo]),
|
|
|
|
.user = std::move(user),
|
|
|
|
.pid = to_unsigned_long_long(&buf[matches[3].rm_so], &buf[matches[3].rm_eo]),
|
|
|
|
.tid = to_unsigned_long_long(&buf[matches[4].rm_so], &buf[matches[4].rm_eo]),
|
|
|
|
.priority = priority_from(buf[matches[5].rm_so]),
|
|
|
|
.tag = std::string(&buf[matches[6].rm_so], static_cast<size_t>(matches[6].rm_eo - matches[6].rm_so)),
|
|
|
|
.message = std::string(&buf[matches[7].rm_so], static_cast<size_t>(matches[7].rm_eo - matches[7].rm_so)),
|
|
|
|
};
|
|
|
|
return std::move(logcat_entry);
|
|
|
|
}
|
2023-01-20 14:41:06 +00:00
|
|
|
|
|
|
|
std::optional<Buffer> try_parse_buffer(char* buf, size_t length) {
|
|
|
|
regmatch_t matches[2];
|
|
|
|
matches[0].rm_so = 0;
|
|
|
|
matches[0].rm_eo = static_cast<regoff_t>(length);
|
|
|
|
if (static_cast<size_t>(matches[0].rm_eo) != length) {
|
|
|
|
throw std::range_error("Buffer line too long for int");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BufferRegex.match(buf, sizeof(matches) / sizeof(regmatch_t), matches, REG_STARTEND)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* buffer_str = &buf[matches[1].rm_so];
|
2023-01-23 14:29:37 +00:00
|
|
|
if (!strncmp(buffer_str, "main", 4)) return Buffer::Main;
|
|
|
|
if (!strncmp(buffer_str, "system", 6)) return Buffer::System;
|
|
|
|
if (!strncmp(buffer_str, "radio", 5)) return Buffer::Radio;
|
|
|
|
if (!strncmp(buffer_str, "events", 6)) return Buffer::Events;
|
|
|
|
if (!strncmp(buffer_str, "crash", 5)) return Buffer::Crash;
|
2023-01-20 14:41:06 +00:00
|
|
|
return Buffer::Unknown;
|
|
|
|
}
|
2023-01-31 14:40:04 +00:00
|
|
|
|
|
|
|
|
2023-03-29 10:08:31 +00:00
|
|
|
void LogcatEntries::push_back(LogcatEntry entry) {
|
|
|
|
this->_logcat_entries.push_back(std::move(entry));
|
|
|
|
if (matches(this->_logcat_entries.back(), this->_config.filters, this->_config.exclusions)) {
|
|
|
|
this->_filtered_offsets.push_back(this->_logcat_entries.size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogcatEntries::refresh() {
|
|
|
|
this->_filtered_offsets.clear();
|
|
|
|
for (size_t i = 0; i < this->_logcat_entries.size(); i++) {
|
|
|
|
if (matches(this->_logcat_entries[i], this->_config.filters, this->_config.exclusions)) {
|
|
|
|
this->_filtered_offsets.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-31 14:40:04 +00:00
|
|
|
void from_json(const nlohmann::json& j, Buffer& buffer) {
|
|
|
|
const std::string& str = j.get_ref<const nlohmann::json::string_t&>();
|
|
|
|
|
|
|
|
if (str == "UNKNOWN") buffer = Buffer::Unknown;
|
|
|
|
else if (str == "MAIN") buffer = Buffer::Main;
|
|
|
|
else if (str == "SYSTEM") buffer = Buffer::System;
|
|
|
|
else if (str == "RADIO") buffer = Buffer::Radio;
|
|
|
|
else if (str == "EVENTS") buffer = Buffer::Events;
|
|
|
|
else if (str == "CRASH") buffer = Buffer::Crash;
|
|
|
|
else throw std::invalid_argument(std::string("Unknown buffer value: ") + str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void to_json(nlohmann::json& j, const Buffer& buffer) {
|
|
|
|
switch (buffer) {
|
|
|
|
case Buffer::Unknown: j = "UNKNOWN"; break;
|
|
|
|
case Buffer::Main: j = "MAIN"; break;
|
|
|
|
case Buffer::System: j = "SYSTEM"; break;
|
|
|
|
case Buffer::Radio: j = "RADIO"; break;
|
|
|
|
case Buffer::Events: j = "EVENTS"; break;
|
|
|
|
case Buffer::Crash: j = "CRASH"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void from_json(const nlohmann::json& j, Priority& priority) {
|
|
|
|
const std::string& str = j.get_ref<const nlohmann::json::string_t&>();
|
|
|
|
|
|
|
|
if (str == "UNKNOWN") priority = Priority::Unknown;
|
|
|
|
else if (str == "VERBOSE") priority = Priority::Verbose;
|
|
|
|
else if (str == "DEBUG") priority = Priority::Debug;
|
|
|
|
else if (str == "INFO") priority = Priority::Info;
|
|
|
|
else if (str == "WARN") priority = Priority::Warn;
|
|
|
|
else if (str == "ERROR") priority = Priority::Error;
|
|
|
|
else if (str == "FATAL") priority = Priority::Fatal;
|
|
|
|
else throw std::invalid_argument(std::string("Unknown priority value: ") + str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void to_json(nlohmann::json& j, const Priority& priority) {
|
|
|
|
switch (priority) {
|
|
|
|
case Priority::Unknown: j = "UNKNOWN"; break;
|
|
|
|
case Priority::Verbose: j = "VERBOSE"; break;
|
|
|
|
case Priority::Debug: j = "DEBUG"; break;
|
|
|
|
case Priority::Info: j = "INFO"; break;
|
|
|
|
case Priority::Warn: j = "WARN"; break;
|
|
|
|
case Priority::Error: j = "ERROR"; break;
|
|
|
|
case Priority::Fatal: j = "FATAL"; break;
|
|
|
|
}
|
|
|
|
}
|