-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
#ifndef CONSOLE_LOGGER_H_ | ||
#define CONSOLE_LOGGER_H_ | ||
|
||
#include "logger.hpp" | ||
#include <iostream> | ||
#include <string> | ||
#include <chrono> | ||
#include <ctime> | ||
#include <iomanip> | ||
|
||
namespace tpt | ||
{ | ||
class ConsoleLogger : public ILogger | ||
{ | ||
public: | ||
virtual void log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) override; | ||
virtual void debug(const std::string &message) override; | ||
virtual void info(const std::string &message) override; | ||
virtual void warning(const std::string &message) override; | ||
virtual void error(const std::string &message) override; | ||
virtual void critical(const std::string &message) override; | ||
}; | ||
} | ||
|
||
#endif |
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,38 @@ | ||
#ifndef LOGGER_H_ | ||
#define LOGGER_H_ | ||
|
||
#include <string> | ||
|
||
namespace tpt | ||
{ | ||
|
||
// Define the LogLevel enum to specify the severity of the log messages. | ||
enum class LogLevel | ||
{ | ||
DEBUG, | ||
INFO, | ||
WARNING, | ||
ERROR, | ||
CRITICAL | ||
}; | ||
|
||
// ILogger interface with virtual methods for logging messages at different severity levels. | ||
class ILogger | ||
{ | ||
public: | ||
virtual ~ILogger() = default; // Virtual destructor for proper cleanup of derived classes | ||
|
||
// Pure virtual functions for logging messages at different severity levels | ||
virtual void log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) = 0; | ||
virtual void debug(const std::string &message) = 0; | ||
virtual void info(const std::string &message) = 0; | ||
virtual void warning(const std::string &message) = 0; | ||
virtual void error(const std::string &message) = 0; | ||
virtual void critical(const std::string &message) = 0; | ||
|
||
// Additional virtual methods can be added here as needed | ||
}; | ||
|
||
} // namespace tpt | ||
|
||
#endif // LOGGER_H_ |
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,54 @@ | ||
#include "../include/console_logger.hpp" | ||
|
||
using namespace tpt; | ||
|
||
void ConsoleLogger::log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) | ||
{ | ||
auto now = std::chrono::system_clock::now(); | ||
std::time_t now_time = std::chrono::system_clock::to_time_t(now); | ||
|
||
// You might want to adjust the time format according to your needs | ||
std::tm now_tm = *std::localtime(&now_time); | ||
|
||
std::cout << "[" << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "] "; | ||
std::cout << "[" << static_cast<int>(level) << "] "; | ||
|
||
if (file) | ||
{ | ||
std::cout << "[" << file << ":" << line << "] "; | ||
} | ||
|
||
std::cout << message << std::endl; | ||
} | ||
void ConsoleLogger::debug(const std::string &message) | ||
{ | ||
log(message, LogLevel::DEBUG); | ||
} | ||
|
||
void ConsoleLogger::info(const std::string &message) | ||
{ | ||
log(message, LogLevel::INFO); | ||
} | ||
|
||
void ConsoleLogger::warning(const std::string &message) | ||
{ | ||
log(message, LogLevel::WARNING); | ||
} | ||
|
||
void ConsoleLogger::error(const std::string &message) | ||
{ | ||
log(message, LogLevel::ERROR); | ||
} | ||
|
||
void ConsoleLogger::critical(const std::string &message) | ||
{ | ||
log(message, LogLevel::CRITICAL); | ||
} | ||
|
||
// Helper Macros | ||
#define LOG(logger, level, message) (logger).log(message, level, __FILE__, __LINE__) | ||
#define LOG_DEBUG(logger, message) LOG(logger, tpt::LogLevel::DEBUG, message) | ||
#define LOG_INFO(logger, message) LOG(logger, tpt::LogLevel::INFO, message) | ||
#define LOG_WARNING(logger, message) LOG(logger, tpt::LogLevel::WARNING, message) | ||
#define LOG_ERROR(logger, message) LOG(logger, tpt::LogLevel::ERROR, message) | ||
#define LOG_CRITICAL(logger, message) LOG(logger, tpt::LogLevel::CRITICAL, message) |