General update during the refactoring process

This commit is contained in:
rapturate
2026-06-24 12:58:05 -04:00
parent 77ea44b03d
commit f4916f76a2
2 changed files with 91 additions and 59 deletions
+28 -4
View File
@@ -1,3 +1,5 @@
#include <algorithm>
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@@ -11,10 +13,27 @@
#include "log_parsing.hpp" #include "log_parsing.hpp"
#include "../ip_to_geo/ip_to_geo.hpp" #include "../ip_to_geo/ip_to_geo.hpp"
p_logs::p_logs(std::string log_path) { p_logs::p_logs(std::string dir_path) {
if(log_path.empty()){ if(dir_path.empty()){
log_path = std::getenv("LOG_PATH"); const char* dir_val = std::getenv("LOG_PATH");
if(dir_val != nullptr){
dir_path = dir_val;
} else {
std::ofstream error_file("error.log", std::ios::app);
if (error_file.is_open()) {
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
error_file << "[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S") << "] "
<< "Log files failed to load in: " << dir_path << std::endl;
error_file.close();
} }
return;
}
}
for(const auto& log_path : p_logs::log_paths){
std::ifstream file(log_path); std::ifstream file(log_path);
if (!file.is_open()) { if (!file.is_open()) {
std::ofstream error_file("error.log", std::ios::app); std::ofstream error_file("error.log", std::ios::app);
@@ -27,7 +46,7 @@ p_logs::p_logs(std::string log_path) {
error_file.close(); error_file.close();
} }
return; continue;
} }
std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")"); std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")");
@@ -71,12 +90,17 @@ p_logs::p_logs(std::string log_path) {
} }
} }
file.close(); file.close();
}
} }
std::vector<Entry> p_logs::get_all_logs(){ std::vector<Entry> p_logs::get_all_logs(){
return logs; return logs;
} }
std::vector<std::filesystem::path> p_logs::get_log_paths(){
return log_paths;
}
std::string p_logs::entryx_ip(int x){ std::string p_logs::entryx_ip(int x){
return logs[x].ip; return logs[x].ip;
} }
+10 -2
View File
@@ -10,6 +10,7 @@
*/ */
#pragma once #pragma once
#include <filesystem>
#include <string> #include <string>
#include <vector> #include <vector>
#include "../ip_to_geo/ip_to_geo.hpp" #include "../ip_to_geo/ip_to_geo.hpp"
@@ -30,17 +31,24 @@ class p_logs {
private: private:
Entry entry; Entry entry;
std::vector<Entry> logs; std::vector<Entry> logs;
std::string url; std::vector<std::filesystem::path> log_paths;
public: public:
/** /**
* @brief Construct a new parsed logs object * @brief Construct a new parsed logs object
* Detailed explanation: This function takes in the absolute path to a .log file and parses the lines into individual "Entry"s. These Entrys are then stored into a vector that can then be accessed via getter functions. * Detailed explanation: This function takes in the path to the access log directory parses the lines into individual "Entry"s. These Entrys are then stored into a vector that can then be accessed via getter functions.
* @param string * @param string
*/ */
p_logs(std::string); p_logs(std::string);
std::vector<Entry> get_all_logs(); std::vector<Entry> get_all_logs();
/**
* @brief Get the log paths object
* Detailed explanation: This function returns all of the paths to the log files in the user provided directory.
* @return std::vector<std::filesystem::path>
*/
std::vector<std::filesystem::path> get_log_paths();
/** /**
* @brief Getter function for a specific Entry's IP * @brief Getter function for a specific Entry's IP
* *