From f4916f76a29adb127186965500e2e48e971498c2 Mon Sep 17 00:00:00 2001 From: rapturate Date: Wed, 24 Jun 2026 12:58:05 -0400 Subject: [PATCH] General update during the refactoring process --- log_parsing/log_parsing.cpp | 138 +++++++++++++++++++++--------------- log_parsing/log_parsing.hpp | 12 +++- 2 files changed, 91 insertions(+), 59 deletions(-) diff --git a/log_parsing/log_parsing.cpp b/log_parsing/log_parsing.cpp index 6a17bfc..1d2b15f 100644 --- a/log_parsing/log_parsing.cpp +++ b/log_parsing/log_parsing.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -11,72 +13,94 @@ #include "log_parsing.hpp" #include "../ip_to_geo/ip_to_geo.hpp" -p_logs::p_logs(std::string log_path) { - if(log_path.empty()){ - log_path = std::getenv("LOG_PATH"); - } - std::ifstream file(log_path); - if (!file.is_open()) { - 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); +p_logs::p_logs(std::string dir_path) { + if(dir_path.empty()){ + 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 file failed to load at: " << log_path << std::endl; - - error_file.close(); - } - return; - } - - std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")"); - - std::string line; - std::smatch match; - - while (getline(file, line)) { - if (regex_search(line, match, log_pattern)) { - - Entry current_entry; - current_entry.ip = match[1].str(); - current_entry.timestamp = match[2].str(); - current_entry.request = match[3].str(); - current_entry.status = match[4].str(); - current_entry.bytes = match[5].str(); - current_entry.referer = match[6].str(); - - std::string raw_ua = match[7].str(); - - if (raw_ua.find("Windows NT 10.0") != std::string::npos) current_entry.os = "Windows 10/11"; - else if (raw_ua.find("Windows NT 6.1") != std::string::npos) current_entry.os = "Windows 7"; - else if (raw_ua.find("iPhone") != std::string::npos) current_entry.os = "iOS (iPhone)"; - else if (raw_ua.find("iPad") != std::string::npos) current_entry.os = "iOS (iPad)"; - else if (raw_ua.find("Android") != std::string::npos) current_entry.os = "Android"; - else if (raw_ua.find("Macintosh") != std::string::npos) current_entry.os = "macOS"; - else if (raw_ua.find("Linux") != std::string::npos) current_entry.os = "Linux"; - else current_entry.os = "Unknown OS"; - - if (raw_ua.find("Edg/") != std::string::npos) current_entry.browser = "Microsoft Edge"; - else if (raw_ua.find("OPR/") != std::string::npos) current_entry.browser = "Opera"; - else if (raw_ua.find("Chrome/") != std::string::npos) current_entry.browser = "Google Chrome"; - else if (raw_ua.find("Safari/") != std::string::npos) current_entry.browser = "Apple Safari"; - else if (raw_ua.find("Firefox/") != std::string::npos) current_entry.browser = "Mozilla Firefox"; - else if (raw_ua.find("curl/") != std::string::npos) current_entry.browser = "curl (CLI Tool)"; - else current_entry.browser = "Unknown Browser/Bot"; - - current_entry.location = iplookup(current_entry.ip); - - logs.push_back(current_entry); + 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; } } - file.close(); + + for(const auto& log_path : p_logs::log_paths){ + + std::ifstream file(log_path); + if (!file.is_open()) { + 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 file failed to load at: " << log_path << std::endl; + + error_file.close(); + } + continue; + } + + std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")"); + + std::string line; + std::smatch match; + + while (getline(file, line)) { + if (regex_search(line, match, log_pattern)) { + + Entry current_entry; + current_entry.ip = match[1].str(); + current_entry.timestamp = match[2].str(); + current_entry.request = match[3].str(); + current_entry.status = match[4].str(); + current_entry.bytes = match[5].str(); + current_entry.referer = match[6].str(); + + std::string raw_ua = match[7].str(); + + if (raw_ua.find("Windows NT 10.0") != std::string::npos) current_entry.os = "Windows 10/11"; + else if (raw_ua.find("Windows NT 6.1") != std::string::npos) current_entry.os = "Windows 7"; + else if (raw_ua.find("iPhone") != std::string::npos) current_entry.os = "iOS (iPhone)"; + else if (raw_ua.find("iPad") != std::string::npos) current_entry.os = "iOS (iPad)"; + else if (raw_ua.find("Android") != std::string::npos) current_entry.os = "Android"; + else if (raw_ua.find("Macintosh") != std::string::npos) current_entry.os = "macOS"; + else if (raw_ua.find("Linux") != std::string::npos) current_entry.os = "Linux"; + else current_entry.os = "Unknown OS"; + + if (raw_ua.find("Edg/") != std::string::npos) current_entry.browser = "Microsoft Edge"; + else if (raw_ua.find("OPR/") != std::string::npos) current_entry.browser = "Opera"; + else if (raw_ua.find("Chrome/") != std::string::npos) current_entry.browser = "Google Chrome"; + else if (raw_ua.find("Safari/") != std::string::npos) current_entry.browser = "Apple Safari"; + else if (raw_ua.find("Firefox/") != std::string::npos) current_entry.browser = "Mozilla Firefox"; + else if (raw_ua.find("curl/") != std::string::npos) current_entry.browser = "curl (CLI Tool)"; + else current_entry.browser = "Unknown Browser/Bot"; + + current_entry.location = iplookup(current_entry.ip); + + logs.push_back(current_entry); + } + } + file.close(); + } } std::vector p_logs::get_all_logs(){ return logs; } +std::vector p_logs::get_log_paths(){ + return log_paths; +} + std::string p_logs::entryx_ip(int x){ return logs[x].ip; } diff --git a/log_parsing/log_parsing.hpp b/log_parsing/log_parsing.hpp index 7e99441..c6b3586 100644 --- a/log_parsing/log_parsing.hpp +++ b/log_parsing/log_parsing.hpp @@ -10,6 +10,7 @@ */ #pragma once +#include #include #include #include "../ip_to_geo/ip_to_geo.hpp" @@ -30,16 +31,23 @@ class p_logs { private: Entry entry; std::vector logs; - std::string url; + std::vector log_paths; public: /** * @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 */ p_logs(std::string); std::vector 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::vector get_log_paths(); /** * @brief Getter function for a specific Entry's IP