From fdae53e9ead4c354eb8049161ba387fd347c0dc7 Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Sun, 15 Dec 2019 01:08:33 +0100 Subject: [PATCH] Add log formatting support --- inferno/src/inferno/core.h | 4 +-- inferno/src/inferno/log.cpp | 27 --------------- inferno/src/inferno/log.h | 68 +++++++++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 46 deletions(-) diff --git a/inferno/src/inferno/core.h b/inferno/src/inferno/core.h index aa14cf9..a7ff7ed 100644 --- a/inferno/src/inferno/core.h +++ b/inferno/src/inferno/core.h @@ -17,8 +17,8 @@ #define ABORT_SIGNAL SIGABRT #endif - #define NF_ASSERT(x, y) if(!(x)) { NF_DANGER(y); raise(ABORT_SIGNAL); } - #define NF_CORE_ASSERT(x, y) if(!(x)) { NF_CORE_DANGER(y); raise(ABORT_SIGNAL); } + #define NF_ASSERT(x, y) if(!(x)) { NF_DANGER("Assert: {%s}, %s", #x, y); raise(ABORT_SIGNAL); } + #define NF_CORE_ASSERT(x, y) if(!(x)) { NF_CORE_DANGER("Assert: {%s}, %s", #x, y); raise(ABORT_SIGNAL); } #else #define NF_ASSERT(x, y) #define NF_CORE_ASSERT(x, y) diff --git a/inferno/src/inferno/log.cpp b/inferno/src/inferno/log.cpp index 0dd8593..1934727 100644 --- a/inferno/src/inferno/log.cpp +++ b/inferno/src/inferno/log.cpp @@ -1,5 +1,3 @@ -#include // printf - #include "inferno/log.h" namespace Inferno { @@ -25,29 +23,4 @@ namespace Inferno { { } - void Logger::log(const char* message, const char* level) - { - printf("%s%s: %s\033[0m\n", level, m_name, message); - } - - void Logger::info(const char* message) - { - this->log(message, "\x1B[34m"); - } - - void Logger::warn(const char* message) - { - this->log(message, "\x1B[33m"); - } - - void Logger::danger(const char* message) - { - this->log(message, "\x1B[31m"); - } - - void Logger::success(const char* message) - { - this->log(message, "\x1B[32m"); - } - } diff --git a/inferno/src/inferno/log.h b/inferno/src/inferno/log.h index 5d47009..335756e 100644 --- a/inferno/src/inferno/log.h +++ b/inferno/src/inferno/log.h @@ -1,7 +1,9 @@ #ifndef LOG_H #define LOG_H -#include +#include // sprintf, printf +#include // strlen +#include // shared_ptr namespace Inferno { @@ -28,11 +30,43 @@ namespace Inferno { Logger(const char* name); ~Logger(); - void log(const char* message, const char* level = ""); - void info(const char* message); - void warn(const char* message); - void danger(const char* message); - void success(const char* message); + template + void print(const char* color, const char* format, A... arguments) + { + char buffer[10 + strlen(color) + strlen(m_name) + strlen(format)]; + sprintf(buffer, "%s%s: %s\033[0m\n", color, m_name, format); + printf(buffer, arguments...); + } + + template + void log(const char* format, A... arguments) + { + this->print("", format, arguments...); + } + + template + void info(const char* format, A... arguments) + { + this->print("\x1B[34m", format, arguments...); + } + + template + void warn(const char* format, A... arguments) + { + this->print("\x1B[33m", format, arguments...); + } + + template + void danger(const char* format, A... arguments) + { + this->print("\x1B[31m", format, arguments...); + } + + template + void success(const char* format, A... arguments) + { + this->print("\x1B[32m", format, arguments...); + } private: const char* m_name; @@ -40,16 +74,16 @@ namespace Inferno { } -#define NF_CORE_LOG(x) Inferno::Log::getCoreLogger()->log(x) -#define NF_CORE_INFO(x) Inferno::Log::getCoreLogger()->info(x) -#define NF_CORE_WARN(x) Inferno::Log::getCoreLogger()->warn(x) -#define NF_CORE_DANGER(x) Inferno::Log::getCoreLogger()->danger(x) -#define NF_CORE_SUCCESS(x) Inferno::Log::getCoreLogger()->success(x) - -#define NF_LOG(x) Inferno::Log::getGameLogger()->log(x) -#define NF_INFO(x) Inferno::Log::getGameLogger()->info(x) -#define NF_WARN(x) Inferno::Log::getGameLogger()->warn(x) -#define NF_DANGER(x) Inferno::Log::getGameLogger()->danger(x) -#define NF_SUCCESS(x) Inferno::Log::getGameLogger()->success(x) +#define NF_CORE_LOG(...) Inferno::Log::getCoreLogger()->log(__VA_ARGS__) +#define NF_CORE_INFO(...) Inferno::Log::getCoreLogger()->info(__VA_ARGS__) +#define NF_CORE_WARN(...) Inferno::Log::getCoreLogger()->warn(__VA_ARGS__) +#define NF_CORE_DANGER(...) Inferno::Log::getCoreLogger()->danger(__VA_ARGS__) +#define NF_CORE_SUCCESS(...) Inferno::Log::getCoreLogger()->success(__VA_ARGS__) + +#define NF_LOG(...) Inferno::Log::getGameLogger()->log(__VA_ARGS__) +#define NF_INFO(...) Inferno::Log::getGameLogger()->info(__VA_ARGS__) +#define NF_WARN(...) Inferno::Log::getGameLogger()->warn(__VA_ARGS__) +#define NF_DANGER(...) Inferno::Log::getGameLogger()->danger(__VA_ARGS__) +#define NF_SUCCESS(...) Inferno::Log::getGameLogger()->success(__VA_ARGS__) #endif // LOG_H