From 0601487ff5c94de8b7d381ef04ae36548c4b6a44 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 30 Nov 2023 22:22:09 +0300 Subject: [PATCH] cross-reference found errors against error database --- Makefile | 4 ++-- database/exampleIssue.json | 7 +++++++ main.cpp | 13 +++++++++---- src/utils/json_parser.cpp | 29 +++++++++++++++++++++++++++++ src/utils/json_parser.h | 12 ++++++++++++ 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 database/exampleIssue.json create mode 100644 src/utils/json_parser.cpp create mode 100644 src/utils/json_parser.h diff --git a/Makefile b/Makefile index 1dc4025..4e9520d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CXX = g++ -CXXFLAGS = -Wall -std=c++11 +CXXFLAGS = -Wall -std=c++17 TARGET = Hyprdoctor -SRCS = main.cpp src/utils/environment.cpp src/utils/file_parser.cpp +SRCS = main.cpp src/utils/environment.cpp src/utils/file_parser.cpp src/utils/json_parser.cpp OBJS = $(SRCS:.cpp=.o) all: $(TARGET); diff --git a/database/exampleIssue.json b/database/exampleIssue.json new file mode 100644 index 0000000..747dda3 --- /dev/null +++ b/database/exampleIssue.json @@ -0,0 +1,7 @@ +{ + "ID": 1, + "name": "Example Issue", + "resources": ["resource1", "resource2", "resource3"], + "description": "This is a long string that can contain multiple sentences or paragraphs.", + "errors": ["error1", "error2", "error3"] +} diff --git a/main.cpp b/main.cpp index 9169e23..1a8b59e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,18 +1,23 @@ + #include "src/utils/environment.h" #include "src/utils/file_parser.h" +#include "src/utils/json_parser.h" #include int main() { - const char *envVar = getEnvVar("PATH"); + const char *envVar = getEnvVar("HYPRLAND_INSTANCE_SIGNATURE"); // try opening the log file try { std::string filePath = "/tmp/hypr/" + std::string(envVar); std::vector errLines = readErrLines(filePath); - // print [ERR] lines, which are caught errors that we prioritize - for (const auto &line : errLines) { - std::cout << line << std::endl; + // associate each error with the issue ID and name + std::vector> issues = + findErrorsInJsonFiles(errLines, "database"); + + for (const auto &[id, name] : issues) { + std::cout << "Issue ID: " << id << ", Name: " << name << std::endl; } } catch (const std::runtime_error &e) { std::cerr << "Error: " << e.what() << std::endl; diff --git a/src/utils/json_parser.cpp b/src/utils/json_parser.cpp new file mode 100644 index 0000000..fa0e00d --- /dev/null +++ b/src/utils/json_parser.cpp @@ -0,0 +1,29 @@ +#include "json_parser.h" +#include +#include +#include +#include + +std::vector> +findErrorsInJsonFiles(const std::vector &errLines, + const std::string &dirPath) { + std::vector> issues; + + for (const auto &entry : std::filesystem::directory_iterator(dirPath)) { + std::ifstream file(entry.path()); + nlohmann::json j; + file >> j; + + if (j.contains("errors") && j["errors"].is_array()) { + for (const auto &error : j["errors"]) { + if (std::find(errLines.begin(), errLines.end(), error) != + errLines.end()) { + issues.push_back({j["ID"], j["name"]}); + break; + } + } + } + } + + return issues; +} diff --git a/src/utils/json_parser.h b/src/utils/json_parser.h new file mode 100644 index 0000000..13a14c2 --- /dev/null +++ b/src/utils/json_parser.h @@ -0,0 +1,12 @@ +#ifndef JSON_PARSER_H +#define JSON_PARSER_H + +#include +#include +#include + +std::vector> +findErrorsInJsonFiles(const std::vector &errLines, + const std::string &dirPath); + +#endif