Wrote the print_logs funciton for debugging and changed the location of the test .log files

This commit is contained in:
2026-05-29 10:45:11 -04:00
parent 9105718e56
commit 46e8048e14
6 changed files with 42 additions and 18 deletions

View File

@@ -5,12 +5,13 @@
#include <vector>
#include <regex>
#include "log_parsing.h"
#include "log_parsing.hpp"
p_logs::p_logs(std::string log_path) {
parsed_logs::parsed_logs(std::string log_path) {
std::ifstream file(log_path);
if (!file.is_open()) {
std::cerr << "Error loading " << log_path << std::endl;
std::cerr << "Error loading " << log_path << std::endl;
return;
}
@@ -19,8 +20,8 @@ parsed_logs::parsed_logs(std::string log_path) {
std::string line;
std::smatch match;
while (std::getline(file, line)) {
if (std::regex_search(line, match, log_pattern)) {
while (getline(file, line)) {
if (regex_search(line, match, log_pattern)) {
Entry current_entry;
current_entry.ip = match[1].str();
@@ -55,34 +56,48 @@ parsed_logs::parsed_logs(std::string log_path) {
file.close();
}
std::string parsed_logs::entryx_ip(int x){
std::string p_logs::entryx_ip(int x){
return logs[x].ip;
}
std::string parsed_logs::entryx_timestamp(int x){
std::string p_logs::entryx_timestamp(int x){
return logs[x].timestamp;
}
std::string parsed_logs::entryx_request(int x){
std::string p_logs::entryx_request(int x){
return logs[x].request;
}
std::string parsed_logs::entryx_status(int x){
std::string p_logs::entryx_status(int x){
return logs[x].status;
}
std::string parsed_logs::entryx_bytes(int x){
std::string p_logs::entryx_bytes(int x){
return logs[x].bytes;
}
std::string parsed_logs::entryx_referer(int x){
std::string p_logs::entryx_referer(int x){
return logs[x].referer;
}
std::string parsed_logs::entryx_os(int x){
std::string p_logs::entryx_os(int x){
return logs[x].os;
}
std::string parsed_logs::entryx_browser(int x){
std::string p_logs::entryx_browser(int x){
return logs[x].browser;
}
void p_logs::print_logs() {
for (const auto& log : logs) {
std::cout << "IP: " << log.ip.c_str() << "\n"
<< "Timestamp: " << log.timestamp.c_str() << "\n"
<< "Request: " << log.request.c_str() << "\n"
<< "Status: " << log.status.c_str() << "\n"
<< "Bytes: " << log.bytes.c_str() << "\n"
<< "Referer: " << log.referer.c_str() << "\n"
<< "OS: " << log.os.c_str() << "\n"
<< "Browser: " << log.browser.c_str() << "\n\n"
<< "-----------------------------\n\n";
}
}

View File

@@ -24,7 +24,7 @@ struct Entry {
std::string browser;
};
class parsed_logs {
class p_logs {
private:
Entry entry;
std::vector<Entry> logs;
@@ -35,7 +35,7 @@ public:
* 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.
* @param string
*/
parsed_logs(std::string);
p_logs(std::string);
/**
* @brief Getter function for a specific Entry's IP
@@ -100,4 +100,10 @@ public:
* @return std::string
*/
std::string entryx_browser(int);
/**
* @brief Prints all parsed log entries
*
*/
void print_logs();
};

View File

@@ -1,4 +1,7 @@
#include "log_parsing/log_parsing.hpp"
int main(){
p_logs logs("C:\\Users\\lewis\\Desktop\\Code\\parselog_cli\\test_logs\\access.log");
logs.print_logs();
return 0;
}

BIN
parselog_cli.exe Normal file

Binary file not shown.