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
OBJS = $(SRCS:.cpp=.o)
all: $(TARGET);
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^
$(CXX) $(CXXFLAGS) -o $@ $^ -lboost_program_options
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
run:
./$(TARGET)

View file

@ -1,24 +1,46 @@
#include "src/utils/environment.h"
#include "src/utils/file_parser.h"
#include "src/utils/json_parser.h"
#include <boost/program_options.hpp>
#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");
// 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 {
if (envVar != nullptr) {
std::string filePath = "/tmp/hypr/" + std::string(envVar);
std::vector<std::string> errLines = readErrLines(filePath);
// associate each error with the issue ID and name
std::vector<std::pair<int, std::string>> issues =
findErrorsInJsonFiles(errLines, "database");
findErrorsInJsonFiles(errLines, "database", vm.count("verbose"));
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;
return 1;

View file

@ -2,14 +2,18 @@
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>
std::vector<std::pair<int, std::string>>
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;
for (const auto &entry : std::filesystem::directory_iterator(dirPath)) {
if (verbose) {
std::cout << "Processing file: " << entry.path() << std::endl;
}
std::ifstream file(entry.path());
nlohmann::json j;
file >> j;
@ -22,6 +26,8 @@ findErrorsInJsonFiles(const std::vector<std::string> &errLines,
break;
}
}
} else {
throw std::runtime_error("Invalid JSON file");
}
}

View file

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