improve project structure

This commit is contained in:
raf 2023-11-30 21:31:41 +03:00
parent 18b5585f0d
commit 63f6943477
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
7 changed files with 72 additions and 58 deletions

3
.gitignore vendored
View file

@ -30,3 +30,6 @@
*.exe *.exe
*.out *.out
*.app *.app
# Build artifacts
Hyprdoctor

View file

@ -1,21 +1,19 @@
# compiler settings
CXX = g++ CXX = g++
CXXFLAGS = -Wall -std=c++11 CXXFLAGS = -Wall -std=c++11
TARGET = Hyprdoctor
SRCS = main.cpp src/utils/environment.cpp src/utils/file_parser.cpp
OBJS = $(SRCS:.cpp=.o)
# general variables all: $(TARGET);
TARGET = main
SRC = main.cpp
OBJ = $(SRC:.cpp=.o)
# Default target $(TARGET): $(OBJS)
all: $(TARGET) $(CXX) $(CXXFLAGS) -o $@ $^
# compile
%.o: %.cpp %.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< $(CXX) $(CXXFLAGS) -c $< -o $@
run:
./$(TARGET)
# cleanup
.PHONY: clean .PHONY: clean
clean: clean:
rm -f $(OBJ) $(TARGET) rm -f $(OBJS) $(TARGET)

View file

@ -1,57 +1,16 @@
#include <cstdlib> #include "src/utils/environment.h"
#include <fstream> #include "src/utils/file_parser.h"
#include <iostream> #include <iostream>
#include <string>
#include <vector>
// value of an environment variable
const char *getEnvVar(const char *name) {
const char *value = std::getenv(name);
if (value == nullptr) {
throw std::runtime_error(std::string("Environment variable ") + name +
" is not set.");
}
return value;
}
// print the value of an environment variable
void printEnvVar(const char *name) {
const char *value = getEnvVar(name);
std::cout << name << "=" << value << std::endl;
}
// read the logs and return the lines that start with [ERR]
// which are standard errors
std::vector<std::string> readErrLines(const std::string &filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("Unable to open file at " + filePath);
}
std::string line;
std::vector<std::string> errLines;
while (std::getline(file, line)) {
if (line.substr(0, 5) == "[ERR]") {
errLines.push_back(line);
}
}
file.close();
return errLines;
}
int main() { int main() {
const char *envVar = getEnvVar("HYPRLAND_INSTANCE_SIGNATURE"); const char *envVar = getEnvVar("PATH");
std::cout << "Variable HYPRLAND_INSTANCE_SIGNATURE is set to " << envVar
<< std::endl;
// 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 error lines // print [ERR] lines, which are caught errors that we prioritize
for (const auto &line : errLines) { for (const auto &line : errLines) {
std::cout << line << std::endl; std::cout << line << std::endl;
} }

16
src/utils/environment.cpp Normal file
View file

@ -0,0 +1,16 @@
#include "environment.h"
#include <cstdlib>
#include <iostream>
const char* getEnvVar(const char* name) {
const char* value = std::getenv(name);
if (value == nullptr) {
throw std::runtime_error(std::string("Environment variable ") + name + " is not set.");
}
return value;
}
void printEnvVar(const char* name) {
const char* value = getEnvVar(name);
std::cout << name << "=" << value << std::endl;
}

7
src/utils/environment.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef UTILS_ENVIRONMENT_H
#define UTILS_ENVIRONMENT_H
const char* getEnvVar(const char* name);
void printEnvVar(const char* name);
#endif

22
src/utils/file_parser.cpp Normal file
View file

@ -0,0 +1,22 @@
#include "file_parser.h"
#include <fstream>
#include <stdexcept>
std::vector<std::string> readErrLines(const std::string &filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
throw std::runtime_error("Unable to open file at " + filePath);
}
std::string line;
std::vector<std::string> errLines;
while (std::getline(file, line)) {
if (line.substr(0, 5) == "[ERR]") {
errLines.push_back(line);
}
}
file.close();
return errLines;
}

9
src/utils/file_parser.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef UTILS_FILE_PARSER_H
#define UTILS_FILE_PARSER_H
#include <string>
#include <vector>
std::vector<std::string> readErrLines(const std::string &filePath);
#endif