#include #include #include #include #include #include #include #include "log_parsing.h" parse::parse(const std::smatch& match) { Entry entry; entry.ip = match[1].str(); entry.timestamp = match[2].str(); entry.request = match[3].str(); entry.status = match[4].str(); entry.bytes = match[5].str(); entry.referer = match[6].str(); std::string raw_ua = match[7].str(); if (raw_ua.find("Windows NT 10.0") != std::string::npos) entry.os = "Windows 10/11"; else if (raw_ua.find("Windows NT 6.1") != std::string::npos) entry.os = "Windows 7"; else if (raw_ua.find("iPhone") != std::string::npos) entry.os = "iOS (iPhone)"; else if (raw_ua.find("iPad") != std::string::npos) entry.os = "iOS (iPad)"; else if (raw_ua.find("Android") != std::string::npos) entry.os = "Android"; else if (raw_ua.find("Macintosh") != std::string::npos) entry.os = "macOS"; else if (raw_ua.find("Linux") != std::string::npos) entry.os = "Linux"; else entry.os = "Unknown OS"; if (raw_ua.find("Edg/") != std::string::npos) entry.browser = "Microsoft Edge"; else if (raw_ua.find("OPR/") != std::string::npos) entry.browser = "Opera"; else if (raw_ua.find("Chrome/") != std::string::npos) entry.browser = "Google Chrome"; else if (raw_ua.find("Safari/") != std::string::npos) entry.browser = "Apple Safari"; else if (raw_ua.find("Firefox/") != std::string::npos) entry.browser = "Mozilla Firefox"; else if (raw_ua.find("curl/") != std::string::npos) entry.browser = "curl (CLI Tool)"; else entry.browser = "Unknown Browser/Bot"; } std::vector parse_file(std::string log_url) { std::ifstream file(log_url); if(!file.is_open()) { std::cerr << "Error loading " << log_url << std::endl; EXIT_FAILURE; } std::vector lines; std::string line; while(std::getline(file, line)){ lines.push_back(std::move(line)); } file.close(); std::vector all_logs; std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")"); for(const auto& log_line : lines) { std::smatch match; if(std::regex_search(log_line, match, log_pattern)) { all_logs.emplace_back(match); } } return all_logs; }