-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from coders-school/openSSL
Logger integration
- Loading branch information
Showing
5 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "Logger.hpp" | ||
|
||
#include <chrono> | ||
#include <format> | ||
#include <memory> | ||
#include <sstream> | ||
#include <vector> | ||
|
||
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<std::string> severityVec = {"INFO", "WARNING", "DEBUG", "ERROR"}; | ||
|
||
std::string logMessage = std::format( | ||
"{} [{}][{}]: {}\n", timeStr, | ||
severityVec[static_cast<int>(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <fstream> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <string> | ||
|
||
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters