feat: add logger class
This commit is contained in:
		
							
								
								
									
										48
									
								
								src/logger.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/logger.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | ||||
| #ifndef LOGGER_H_ | ||||
| #define LOGGER_H_ | ||||
|  | ||||
| #include <sstream> | ||||
| #include <string> | ||||
| #include <vector> | ||||
|  | ||||
| // Utility: Concatenate any types into a string | ||||
| template <typename... Args> | ||||
| std::string sstr(Args&&... args) | ||||
| { | ||||
| 	std::ostringstream stream; | ||||
| 	stream << std::dec; | ||||
| 	((stream << args), ...); | ||||
| 	return stream.str(); | ||||
| } | ||||
|  | ||||
| // Log types | ||||
| enum class LogType { | ||||
| 	info = 0, | ||||
| 	warn, | ||||
| 	error | ||||
| }; | ||||
|  | ||||
| // Log entry structure | ||||
| struct LogEntry { | ||||
| 	LogType	    type{LogType::info}; | ||||
| 	std::string message{}; | ||||
| }; | ||||
|  | ||||
| // Logger class | ||||
| class Logger { | ||||
| public: | ||||
| 	static void info(const std::string& message); | ||||
| 	static void warn(const std::string& message); | ||||
| 	static void error(const std::string& message); | ||||
|  | ||||
| 	[[nodiscard]] static const std::vector<LogEntry>& messages() | ||||
| 	{ | ||||
| 		return messages_; | ||||
| 	} | ||||
| 	static void clear() { messages_.clear(); } | ||||
|  | ||||
| private: | ||||
| 	inline static std::vector<LogEntry> messages_{}; | ||||
| }; | ||||
|  | ||||
| #endif // LOGGER_H_ | ||||
		Reference in New Issue
	
	Block a user