diff --git a/.env b/.env index 7d2f7bb..9eb107d 100644 --- a/.env +++ b/.env @@ -1,2 +1,8 @@ -DB_PATH=/home/rapturate/Code/cpp/parselog_cli/data/GeoLite2-City.mmdb -LOG_PATH=/home/rapturate/Code/cpp/parselog_cli/test_logs/access.log.txt +# ParseLogCLI Global Environment Configuration +# Generated on 2026-06-09 + +# Path to the MaxMind GeoLite2 City Database binary +DB_PATH=data/GeoLite2-City.mmdb + +# Absolute or relative path to the server access logs target file +LOG_PATH=test_logs/access.log.txt diff --git a/.gitignore b/.gitignore index 68ad2ee..f64b612 100644 --- a/.gitignore +++ b/.gitignore @@ -46,5 +46,5 @@ compile_commands.json # ========================================== # Sensitive Files # ========================================== -.env +*.env .env diff --git a/ip_to_geo/ip_to_geo.cpp b/ip_to_geo/ip_to_geo.cpp index e524350..f920e38 100644 --- a/ip_to_geo/ip_to_geo.cpp +++ b/ip_to_geo/ip_to_geo.cpp @@ -1,9 +1,12 @@ #include -#include #include #include #include +#include +#include +#include +#include #include "ip_to_geo.hpp" @@ -13,6 +16,20 @@ loc_data iplookup(const std::string& ip) { const char* env_db_path = std::getenv("DB_PATH"); std::string db_path = (env_db_path != nullptr) ? env_db_path : "data/GeoLite2-City.mmdb"; + // Verify if the database file physically exists on the disk + if (!std::filesystem::exists(db_path)) { + 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") << "] " + << "Error: Database file missing at path: " << db_path << " during IP lookup for: " << ip << std::endl; + error_file.close(); + } + + return loc_data{}; + } loc_data location; @@ -26,7 +43,17 @@ loc_data iplookup(const std::string& ip) { location.longitude = geo_data["longitude"]; } catch (const std::exception& e) { - //std::cerr << "Database failed to load: " << e.what() << std::endl; + 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") << "] " + << "Database failed to load: " << e.what() << std::endl; + + error_file.close(); + } } + return location; }; \ No newline at end of file diff --git a/log_parsing/log_parsing.cpp b/log_parsing/log_parsing.cpp index f14f63a..6a17bfc 100644 --- a/log_parsing/log_parsing.cpp +++ b/log_parsing/log_parsing.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include #include @@ -15,7 +17,16 @@ p_logs::p_logs(std::string log_path) { } std::ifstream file(log_path); if (!file.is_open()) { - std::cerr << "Error loading " << log_path << std::endl; + 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; }