-
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.
added Logger class
- Loading branch information
Showing
5 changed files
with
65 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,3 @@ endif() | |
|
||
include_directories(headers) | ||
add_subdirectory(source) | ||
|
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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; |
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 |
---|---|---|
|
@@ -20,5 +20,5 @@ int main() { | |
std::cerr << "Error: " << e.what() << std::endl; | ||
} | ||
|
||
return 0; | ||
return 0; | ||
} |