Parse buffer lines

This commit is contained in:
blankie 2023-01-20 21:41:06 +07:00
parent d5cf2584db
commit 3219ebf65e
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
4 changed files with 44 additions and 3 deletions

View File

@ -1,11 +1,13 @@
#include <climits>
#include <cstring>
#include <optional>
#include <stdexcept>
#include "logcat_entry.h"
#include "pcre2_wrapper.h"
const Pcre2Regex LogcatEntryRegex("^\\s*(\\d+)(?:\\.\\d+)?(?:\\s+([\\w\\d._-]+))?\\s+(\\d+)\\s+(\\d+)\\s+([A-Z])\\s+(.+?)\\s*:\\s(.*)$");
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+)$");
Priority priority_from(char c) {
switch (c) {
@ -99,3 +101,29 @@ std::optional<LogcatEntry> try_parse_logcat_entry(char* buf, size_t length, Buff
};
return std::move(logcat_entry);
}
static bool substr_equal(const char* lhs_str, size_t lhs_length, const char* rhs_str) {
return lhs_length == strlen(rhs_str) && memcmp(lhs_str, rhs_str, lhs_length * sizeof(char)) == 0;
}
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];
size_t buffer_length = static_cast<size_t>(matches[1].rm_eo - matches[1].rm_so);
if (substr_equal(buffer_str, buffer_length, "main")) return Buffer::Main;
if (substr_equal(buffer_str, buffer_length, "system")) return Buffer::System;
if (substr_equal(buffer_str, buffer_length, "radio")) return Buffer::Radio;
if (substr_equal(buffer_str, buffer_length, "events")) return Buffer::Events;
if (substr_equal(buffer_str, buffer_length, "crash")) return Buffer::Crash;
return Buffer::Unknown;
}

View File

@ -37,3 +37,4 @@ Priority priority_from(char c);
const char* priority_to(Priority priority);
const char* buffer_to(Buffer buffer);
std::optional<LogcatEntry> try_parse_logcat_entry(char* buf, size_t length, Buffer buffer);
std::optional<Buffer> try_parse_buffer(char* buf, size_t length);

View File

@ -162,7 +162,7 @@ void LogcatThread::_handle_line(char* buf, size_t length, bool is_stdout) {
std::optional<LogcatEntry> logcat_entry;
try {
logcat_entry = try_parse_logcat_entry(buf, length, Buffer::Unknown);
logcat_entry = try_parse_logcat_entry(buf, length, this->_current_buffer);
} catch (const std::exception& e) {
std::string log_entry = format_log(std::string("Failed to parse logcat entry: ") + e.what());
printf("%s\n", log_entry.c_str());
@ -172,7 +172,18 @@ void LogcatThread::_handle_line(char* buf, size_t length, bool is_stdout) {
this->atomic_ring_buffer.put_and_increment_write(std::move(*logcat_entry));
return;
}
// TODO handle buffers
std::optional<Buffer> new_buffer;
try {
new_buffer = try_parse_buffer(buf, length);
} catch (const std::exception& e) {
std::string log_entry = format_log(std::string("Failed to parse buffer line: ") + e.what());
printf("%s\n", log_entry.c_str());
this->atomic_ring_buffer.put_and_increment_write(std::move(log_entry));
}
if (new_buffer) {
this->_current_buffer = *new_buffer;
return;
}
std::string log_entry = format_log(std::string("Cannot parse logcat stdout: ") + std::string(buf, length));
printf("%s\n", log_entry.c_str());

View File

@ -38,6 +38,7 @@ private:
size_t _stdout_buf_used = 0;
char _stderr_buf[NEWLINE_BUF_SIZE];
size_t _stderr_buf_used = 0;
Buffer _current_buffer = Buffer::Unknown;
std::stop_source _stop_source;
std::thread _thread;
};