diff --git a/CMakeLists.txt b/CMakeLists.txt index 6af0822..06b9f51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.14) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_COMPILER g++-13) project(PasswordManager) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index bc7eac5..1d04ed2 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -4,10 +4,15 @@ add_library(sqlite3 STATIC ../lib/sqlite3.c) find_package(OpenSSL REQUIRED) include_directories(${OPENSSL_INCLUDE_DIR}) -add_executable(passwordManager main.cpp PasswordManager.cpp) +add_executable(passwordManager + main.cpp + PasswordManager.cpp + Common/Logger.cpp) target_include_directories(sqlite3 PUBLIC ../lib) target_link_libraries(passwordManager PRIVATE sqlite3 ${OPENSSL_LIBRARIES}) add_subdirectory(test) add_subdirectory(DataBase) + +target_include_directories(passwordManager PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/headers) diff --git a/source/Common/Logger.cpp b/source/Common/Logger.cpp new file mode 100644 index 0000000..df096c2 --- /dev/null +++ b/source/Common/Logger.cpp @@ -0,0 +1,55 @@ +#include "Logger.hpp" + +#include +#include +#include +#include +#include + +namespace common::logger { + +Logger::Logger(const std::string& prefix, const std::string& testName) + : prefix_(prefix), testName_(testName) { + std::error_code errorCode; + std::filesystem::create_directories(logDirectory_, errorCode); + if (errorCode) { + std::cerr << "failed to create log directory: " << errorCode.message() + << std::endl; + return; + } + + std::filesystem::path loggingFilePath = logDirectory_ / (testName_ + ".log"); + file_.open(loggingFilePath, std::ios::app); + if (!file_.is_open()) { + std::cerr << "unable to open logs file at " << loggingFilePath << std::endl; + } +} + +void Logger::log(const std::string& message) noexcept { + auto currentTime = std::chrono::system_clock::now(); + auto timeStamp = std::chrono::system_clock::to_time_t(currentTime); + std::tm timeInfo = *std::localtime(&timeStamp); + + std::stringstream ss; + ss << std::put_time(&timeInfo, "%Y-%m-%d %H:%M:%S"); + std::string timeStr = ss.str(); + std::vector severityVec = {"INFO", "WARNING", "DEBUG", "ERROR"}; + + std::string logMessage = std::format( + "{} [{}][{}]: {}\n", timeStr, + severityVec[static_cast(currentSeverity_)], prefix_, message); + + file_ << logMessage; +} + +Logger& Logger::operator<<(Severity severity) noexcept { + currentSeverity_ = severity; + return *this; +} + +Logger& Logger::operator<<(const std::string& message) noexcept { + log(message); + return *this; +} + +} // namespace common::logger diff --git a/source/Common/Logger.hpp b/source/Common/Logger.hpp new file mode 100644 index 0000000..5a724da --- /dev/null +++ b/source/Common/Logger.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "time.h" + +namespace common::logger { + +enum class Severity { info, warning, debug, error }; + +class Logger final { + public: + explicit Logger(const std::string& prefix, + const std::string& testName = "defaultLogs"); + ~Logger() = default; + + Logger& operator<<(Severity severity) noexcept; + Logger& operator<<(const std::string& message) noexcept; + + private: + void log(const std::string& message) noexcept; + + std::ofstream file_; + std::string prefix_; + std::string testName_; + Severity currentSeverity_; + + static inline const std::filesystem::path logDirectory_ = "ut_logs/"; +}; + +} // namespace common::logger + +constexpr common::logger::Severity info = common::logger::Severity::info; +constexpr common::logger::Severity warning = common::logger::Severity::warning; +constexpr common::logger::Severity debug = common::logger::Severity::debug; +constexpr common::logger::Severity error = common::logger::Severity::error; diff --git a/source/PasswordManager.cpp b/source/PasswordManager.cpp index ab98de7..66ba705 100644 --- a/source/PasswordManager.cpp +++ b/source/PasswordManager.cpp @@ -1,8 +1,8 @@ #include "PasswordManager.h" -#include +#include "Common/Logger.hpp" -PasswordManager::PasswordManager(const std::string &password) +PasswordManager::PasswordManager(const std::string& password) : password_(password) { std::cout << "Password is: " << password_ << '\n'; }