cross-reference found errors against error database

This commit is contained in:
raf 2023-11-30 22:22:09 +03:00
parent 00eba3c0a6
commit 0601487ff5
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
5 changed files with 59 additions and 6 deletions

View file

@ -1,7 +1,7 @@
CXX = g++ CXX = g++
CXXFLAGS = -Wall -std=c++11 CXXFLAGS = -Wall -std=c++17
TARGET = Hyprdoctor 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) OBJS = $(SRCS:.cpp=.o)
all: $(TARGET); all: $(TARGET);

View file

@ -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"]
}

View file

@ -1,18 +1,23 @@
#include "src/utils/environment.h" #include "src/utils/environment.h"
#include "src/utils/file_parser.h" #include "src/utils/file_parser.h"
#include "src/utils/json_parser.h"
#include <iostream> #include <iostream>
int main() { int main() {
const char *envVar = getEnvVar("PATH"); const char *envVar = getEnvVar("HYPRLAND_INSTANCE_SIGNATURE");
// try opening the log file // try opening the log file
try { try {
std::string filePath = "/tmp/hypr/" + std::string(envVar); std::string filePath = "/tmp/hypr/" + std::string(envVar);
std::vector<std::string> errLines = readErrLines(filePath); std::vector<std::string> errLines = readErrLines(filePath);
// print [ERR] lines, which are caught errors that we prioritize // associate each error with the issue ID and name
for (const auto &line : errLines) { std::vector<std::pair<int, std::string>> issues =
std::cout << line << std::endl; findErrorsInJsonFiles(errLines, "database");
for (const auto &[id, name] : issues) {
std::cout << "Issue ID: " << id << ", Name: " << name << std::endl;
} }
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;

29
src/utils/json_parser.cpp Normal file
View file

@ -0,0 +1,29 @@
#include "json_parser.h"
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include <vector>
std::vector<std::pair<int, std::string>>
findErrorsInJsonFiles(const std::vector<std::string> &errLines,
const std::string &dirPath) {
std::vector<std::pair<int, std::string>> 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;
}

12
src/utils/json_parser.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef JSON_PARSER_H
#define JSON_PARSER_H
#include <string>
#include <utility>
#include <vector>
std::vector<std::pair<int, std::string>>
findErrorsInJsonFiles(const std::vector<std::string> &errLines,
const std::string &dirPath);
#endif