|
| 1 | +#include "../include/console_logger.hpp" |
| 2 | + |
| 3 | +using namespace tpt; |
| 4 | + |
| 5 | +void ConsoleLogger::log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) |
| 6 | +{ |
| 7 | + auto now = std::chrono::system_clock::now(); |
| 8 | + std::time_t now_time = std::chrono::system_clock::to_time_t(now); |
| 9 | + |
| 10 | + // You might want to adjust the time format according to your needs |
| 11 | + std::tm now_tm = *std::localtime(&now_time); |
| 12 | + |
| 13 | + std::cout << "[" << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "] "; |
| 14 | + std::cout << "[" << static_cast<int>(level) << "] "; |
| 15 | + |
| 16 | + if (file) |
| 17 | + { |
| 18 | + std::cout << "[" << file << ":" << line << "] "; |
| 19 | + } |
| 20 | + |
| 21 | + std::cout << message << std::endl; |
| 22 | +} |
| 23 | +void ConsoleLogger::debug(const std::string &message) |
| 24 | +{ |
| 25 | + log(message, LogLevel::DEBUG); |
| 26 | +} |
| 27 | + |
| 28 | +void ConsoleLogger::info(const std::string &message) |
| 29 | +{ |
| 30 | + log(message, LogLevel::INFO); |
| 31 | +} |
| 32 | + |
| 33 | +void ConsoleLogger::warning(const std::string &message) |
| 34 | +{ |
| 35 | + log(message, LogLevel::WARNING); |
| 36 | +} |
| 37 | + |
| 38 | +void ConsoleLogger::error(const std::string &message) |
| 39 | +{ |
| 40 | + log(message, LogLevel::ERROR); |
| 41 | +} |
| 42 | + |
| 43 | +void ConsoleLogger::critical(const std::string &message) |
| 44 | +{ |
| 45 | + log(message, LogLevel::CRITICAL); |
| 46 | +} |
| 47 | + |
| 48 | +// Helper Macros |
| 49 | +#define LOG(logger, level, message) (logger).log(message, level, __FILE__, __LINE__) |
| 50 | +#define LOG_DEBUG(logger, message) LOG(logger, tpt::LogLevel::DEBUG, message) |
| 51 | +#define LOG_INFO(logger, message) LOG(logger, tpt::LogLevel::INFO, message) |
| 52 | +#define LOG_WARNING(logger, message) LOG(logger, tpt::LogLevel::WARNING, message) |
| 53 | +#define LOG_ERROR(logger, message) LOG(logger, tpt::LogLevel::ERROR, message) |
| 54 | +#define LOG_CRITICAL(logger, message) LOG(logger, tpt::LogLevel::CRITICAL, message) |
0 commit comments