Skip to content

Commit

Permalink
Logger integration
Browse files Browse the repository at this point in the history
added Logger class
  • Loading branch information
Kacperfis committed Feb 20, 2024
1 parent 28f5304 commit 9c35f58
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 78 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ endif()

include_directories(headers)
add_subdirectory(source)

79 changes: 36 additions & 43 deletions source/Common/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,55 @@
#include "Logger.hpp"

#include <sstream>
#include <chrono>
#include <vector>
#include <format>
#include <memory>
#include <sstream>
#include <vector>

namespace common::logger
{
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;
}
: 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);
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<std::string> severityVec = {"INFO", "WARNING", "DEBUG", "ERROR"};
std::stringstream ss;
ss << std::put_time(&timeInfo, "%Y-%m-%d %H:%M:%S");
std::string timeStr = ss.str();
std::vector<std::string> severityVec = {"INFO", "WARNING", "DEBUG", "ERROR"};

std::string logMessage = std::format(
"{} [{}][{}]: {}\n", timeStr, severityVec[static_cast<int>(currentSeverity_)],
prefix_, message);
std::string logMessage = std::format(
"{} [{}][{}]: {}\n", timeStr,
severityVec[static_cast<int>(currentSeverity_)], prefix_, message);

file_ << logMessage;
file_ << logMessage;
}

Logger& Logger::operator<<(Severity severity) noexcept
{
currentSeverity_ = severity;
return *this;
Logger& Logger::operator<<(Severity severity) noexcept {
currentSeverity_ = severity;
return *this;
}

Logger& Logger::operator<<(const std::string& message) noexcept
{
log(message);
return *this;
Logger& Logger::operator<<(const std::string& message) noexcept {
log(message);
return *this;
}

} // namespace common::logger
} // namespace common::logger
55 changes: 25 additions & 30 deletions source/Common/Logger.hpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
#pragma once

#include <iostream>
#include <iomanip>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <filesystem>

#include "time.h"

namespace common::logger
{
namespace common::logger {

enum class Severity
{
info,
warning,
debug,
error
};
enum class Severity { info, warning, debug, error };

class Logger final {
public:
explicit Logger(const std::string& prefix,
const std::string& testName = "defaultLogs");
~Logger() = default;

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;

Logger& operator<<(Severity severity) noexcept;
Logger& operator<<(const std::string& message) noexcept;
private:
void log(const std::string& message) noexcept;
private:
void log(const std::string& message) noexcept;

std::ofstream file_;
std::string prefix_;
std::string testName_;
Severity currentSeverity_;
std::ofstream file_;
std::string prefix_;
std::string testName_;
Severity currentSeverity_;

static inline const std::filesystem::path logDirectory_ = "ut_logs/";
static inline const std::filesystem::path logDirectory_ = "ut_logs/";
};

} // namespace common::logger
} // namespace common::logger

constexpr common::logger::Severity info = common::logger::Severity::info;
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;
constexpr common::logger::Severity debug = common::logger::Severity::debug;
constexpr common::logger::Severity error = common::logger::Severity::error;
6 changes: 3 additions & 3 deletions source/PasswordManager.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "PasswordManager.h"

#include "Common/Logger.hpp"

PasswordManager::PasswordManager(const std::string& password)
: password_(password)
{
std::cout << "Password is: " << password_ << '\n';
: password_(password) {
std::cout << "Password is: " << password_ << '\n';
}

PasswordManager::~PasswordManager() {
Expand Down
2 changes: 1 addition & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ int main() {
std::cerr << "Error: " << e.what() << std::endl;
}

return 0;
return 0;
}

0 comments on commit 9c35f58

Please sign in to comment.