feat: add logger class
This commit is contained in:
		
							
								
								
									
										57
									
								
								src/logger.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/logger.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| #include "logger.h" | ||||
|  | ||||
| #include <chrono> | ||||
| #include <iostream> | ||||
|  | ||||
| namespace { | ||||
|  | ||||
| std::string current_datetime_string() | ||||
| { | ||||
| 	auto	  now	     = std::chrono::system_clock::now(); | ||||
| 	auto	  now_time_t = std::chrono::system_clock::to_time_t(now); | ||||
| 	struct tm now_tm {}; | ||||
| #ifdef _WIN32 | ||||
| 	localtime_s(&now_tm, &now_time_t); | ||||
| #else | ||||
| 	localtime_r(&now_time_t, &now_tm); | ||||
| #endif | ||||
|  | ||||
| 	char buffer[30]; | ||||
| 	std::strftime(buffer, sizeof(buffer) / sizeof(char), "%Y-%m-%d %H:%M:%S", | ||||
| 		      &now_tm); | ||||
| 	return std::string{buffer}; | ||||
| } | ||||
|  | ||||
| } // namespace | ||||
|  | ||||
| void Logger::info(const std::string& message) | ||||
| { | ||||
| 	LogEntry entry; | ||||
| 	entry.type = LogType::info; | ||||
| 	entry.message = | ||||
| 	    "[" + current_datetime_string() + "] [INFO]: " + message; | ||||
|  | ||||
| 	std::cout << "\x1B[32m" << entry.message << "\033[0m" << std::endl; | ||||
| 	messages_.push_back(entry); | ||||
| } | ||||
|  | ||||
| void Logger::warn(const std::string& message) | ||||
| { | ||||
| 	LogEntry entry; | ||||
| 	entry.type = LogType::warn; | ||||
| 	entry.message = | ||||
| 	    "[" + current_datetime_string() + "] [WARN]: " + message; | ||||
|  | ||||
| 	std::cout << "\x1B[93m" << entry.message << "\033[0m" << std::endl; | ||||
| 	messages_.push_back(entry); | ||||
| } | ||||
|  | ||||
| void Logger::error(const std::string& message) | ||||
| { | ||||
| 	LogEntry entry; | ||||
| 	entry.type	= LogType::error; | ||||
| 	entry.message = "[" + current_datetime_string() + "] [ERROR]: " + message; | ||||
|  | ||||
| 	std::cerr << "\x1B[91m" << entry.message << "\033[0m" << std::endl; | ||||
| 	messages_.push_back(entry); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user