add --verbose for a little more debugging info

This commit is contained in:
raf 2023-11-30 22:46:59 +03:00
parent 1ebaab6294
commit 03929055f8
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
4 changed files with 59 additions and 29 deletions

View file

@ -4,13 +4,14 @@ TARGET = Hyprdoctor
SRCS = main.cpp src/utils/environment.cpp src/utils/file_parser.cpp src/utils/json_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)
$(TARGET): $(OBJS) $(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^ $(CXX) $(CXXFLAGS) -o $@ $^ -lboost_program_options
%.o: %.cpp %.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@ $(CXX) $(CXXFLAGS) -c $< -o $@
run: run:
./$(TARGET) ./$(TARGET)

View file

@ -1,23 +1,45 @@
#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 "src/utils/json_parser.h"
#include <boost/program_options.hpp>
#include <iostream> #include <iostream>
int main() { namespace po = boost::program_options;
int main(int argc, char *argv[]) {
po::options_description desc("Allowed options");
desc.add_options()("help", "Produce help message")("verbose",
"Enable verbose output");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << "\n";
return 1;
}
const char *envVar = getEnvVar("HYPRLAND_INSTANCE_SIGNATURE"); const char *envVar = getEnvVar("HYPRLAND_INSTANCE_SIGNATURE");
// if --verbose flag is set, print the environment variable
if (vm.count("verbose")) {
std::cout << "HYPRLAND_INSTANCE_SIGNATURE: " << envVar << std::endl;
}
// try opening the log file // try opening the log file
try { try {
std::string filePath = "/tmp/hypr/" + std::string(envVar); if (envVar != nullptr) {
std::vector<std::string> errLines = readErrLines(filePath); std::string filePath = "/tmp/hypr/" + std::string(envVar);
std::vector<std::string> errLines = readErrLines(filePath);
// associate each error with the issue ID and name // associate each error with the issue ID and name
std::vector<std::pair<int, std::string>> issues = std::vector<std::pair<int, std::string>> issues =
findErrorsInJsonFiles(errLines, "database"); findErrorsInJsonFiles(errLines, "database", vm.count("verbose"));
for (const auto &[id, name] : issues) { for (const auto &[id, name] : issues) {
std::cout << "Issue ID: " << id << ", Name: " << name << std::endl; 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;

View file

@ -2,28 +2,34 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string>
#include <vector> #include <vector>
std::vector<std::pair<int, std::string>> std::vector<std::pair<int, std::string>>
findErrorsInJsonFiles(const std::vector<std::string> &errLines, findErrorsInJsonFiles(const std::vector<std::string> &errLines,
const std::string &dirPath) { const std::string &dirPath, bool verbose) {
std::vector<std::pair<int, std::string>> issues; std::vector<std::pair<int, std::string>> issues;
for (const auto &entry : std::filesystem::directory_iterator(dirPath)) { for (const auto &entry : std::filesystem::directory_iterator(dirPath)) {
std::ifstream file(entry.path()); if (verbose) {
nlohmann::json j; std::cout << "Processing file: " << entry.path() << std::endl;
file >> j; }
std::ifstream file(entry.path());
nlohmann::json j;
file >> j;
if (j.contains("errors") && j["errors"].is_array()) { if (j.contains("errors") && j["errors"].is_array()) {
for (const auto &error : j["errors"]) { for (const auto &error : j["errors"]) {
if (std::find(errLines.begin(), errLines.end(), error) != if (std::find(errLines.begin(), errLines.end(), error) !=
errLines.end()) { errLines.end()) {
issues.push_back({j["ID"], j["name"]}); issues.push_back({j["ID"], j["name"]});
break; break;
} }
} }
} } else {
} throw std::runtime_error("Invalid JSON file");
}
}
return issues; return issues;
} }

View file

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