-
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
8 changed files
with
140 additions
and
93 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
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 |
---|---|---|
@@ -1,54 +1,47 @@ | ||
#include "../include/console_logger.hpp" | ||
|
||
using namespace tpt; | ||
|
||
void ConsoleLogger::log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) | ||
namespace tpt | ||
{ | ||
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) | ||
void ConsoleLogger::log(const std::string &message, tpt::LogLevel level, const char *file, int line) | ||
{ | ||
std::cout << "[" << file << ":" << line << "] "; | ||
auto now = std::chrono::system_clock::now(); | ||
std::time_t now_time = std::chrono::system_clock::to_time_t(now); | ||
std::tm now_tm = *std::localtime(&now_time); | ||
|
||
std::string colorCode; | ||
switch (level) | ||
{ | ||
case LogLevel::DEBUG: | ||
colorCode = "\033[36m"; // Cyan | ||
break; | ||
case LogLevel::INFO: | ||
colorCode = "\033[0m"; | ||
break; | ||
case LogLevel::WARNING: | ||
colorCode = "\033[33m"; // Yellow | ||
break; | ||
case LogLevel::ERROR: | ||
colorCode = "\033[31m"; // Red | ||
break; | ||
case LogLevel::CRITICAL: | ||
colorCode = "\033[35m"; // Magenta | ||
break; | ||
default: | ||
colorCode = "\033[0m"; // Reset | ||
break; | ||
} | ||
|
||
// Use logLevelToString to convert the LogLevel to a string | ||
std::string levelStr = logLevelToString(level); | ||
|
||
std::cout << colorCode << "[" << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "] " | ||
<< "[" << levelStr << "]"; | ||
|
||
if (file) | ||
{ | ||
std::cout << " [" << file << ":" << line << "]"; | ||
} | ||
|
||
std::cout << " " << message << "\033[0m" << std::endl; | ||
} | ||
|
||
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) |
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,23 @@ | ||
#include "../include/logger.hpp" | ||
|
||
namespace tpt | ||
{ | ||
std::string Logger::logLevelToString(LogLevel level) | ||
{ | ||
switch (level) | ||
{ | ||
case LogLevel::DEBUG: | ||
return "DEBUG"; | ||
case LogLevel::INFO: | ||
return "INFO"; | ||
case LogLevel::WARNING: | ||
return "WARNING"; | ||
case LogLevel::ERROR: | ||
return "ERROR"; | ||
case LogLevel::CRITICAL: | ||
return "CRITICAL"; | ||
default: | ||
return "UNKNOWN"; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.