From a72cd5cab60f297926e18fa6d3d1fc125551bb9d Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Sun, 21 Feb 2021 18:43:42 -0800 Subject: [PATCH] simplifying logging, changing App events to std::function --- include/blah/core/app.h | 20 +++++++++++--------- include/blah/core/log.h | 10 ++++++++++ src/core/app.cpp | 4 +--- src/core/log.cpp | 16 ++++++++-------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/include/blah/core/app.h b/include/blah/core/app.h index 7256c31..e8dd74b 100644 --- a/include/blah/core/app.h +++ b/include/blah/core/app.h @@ -1,8 +1,13 @@ #pragma once #include +#include +#include namespace Blah { + using AppEventFn = std::function; + using AppLogFn = std::function; + struct Config { const char* name; @@ -11,15 +16,12 @@ namespace Blah int max_updates; int target_framerate; - void (*on_startup)(); - void (*on_shutdown)(); - void (*on_update)(); - void (*on_render)(); - void (*on_exit_request)(); - - void (*on_info)(const char* text); - void (*on_warn)(const char* text); - void (*on_error)(const char* text); + AppEventFn on_startup; + AppEventFn on_shutdown; + AppEventFn on_update; + AppEventFn on_render; + AppEventFn on_exit_request; + AppLogFn on_log; Config(); }; diff --git a/include/blah/core/log.h b/include/blah/core/log.h index 626fdae..36266dd 100644 --- a/include/blah/core/log.h +++ b/include/blah/core/log.h @@ -23,6 +23,9 @@ #define BLAH_ASSERT(condition, message) \ do { if (!(condition)) { BLAH_ERROR(message); } } while(0) +#define BLAH_ASSERT_FMT(condition, message, ...) \ + do { if (!(condition)) { BLAH_ERROR_FMT(message, __VA_ARGS__); } } while(0) + // maximum length of a print/warn/error message #ifndef BLAH_MESSAGE #define BLAH_MESSAGE 1024 @@ -32,6 +35,13 @@ namespace Blah { namespace Log { + enum class Category + { + Info, + Warning, + Error + }; + void print(const char* info, ...); void warn(const char* info, ...); void error(const char* info, ...); diff --git a/src/core/app.cpp b/src/core/app.cpp index 384d3c5..aeb755d 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -27,9 +27,7 @@ Config::Config() on_update = nullptr; on_render = nullptr; on_exit_request = App::exit; - on_info = nullptr; - on_warn = nullptr; - on_error = nullptr; + on_log = nullptr; } namespace diff --git a/src/core/log.cpp b/src/core/log.cpp index 75622d3..6913ec2 100644 --- a/src/core/log.cpp +++ b/src/core/log.cpp @@ -13,9 +13,9 @@ void Log::print(const char* format, ...) vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap); va_end(ap); - if (App::is_running() && App::config()->on_info != nullptr) + if (App::is_running() && App::config()->on_log) { - App::config()->on_info(msg); + App::config()->on_log(msg, Category::Info); } else { @@ -31,13 +31,13 @@ void Log::warn(const char* format, ...) vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap); va_end(ap); - if (App::is_running() && App::config()->on_warn != nullptr) + if (App::is_running() && App::config()->on_log) { - App::config()->on_warn(msg); + App::config()->on_log(msg, Category::Warning); } else { - printf("\033[01;33mWARN:\033[0m\t%s\n", msg); + printf("WARN: %s\n", msg); } } @@ -49,12 +49,12 @@ void Log::error(const char* format, ...) vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap); va_end(ap); - if (App::is_running() && App::config()->on_error != nullptr) + if (App::is_running() && App::config()->on_log) { - App::config()->on_error(msg); + App::config()->on_log(msg, Category::Error); } else { - printf("\033[1;31mERROR:\033[0m\t%s\n", msg); + printf("ERROR: %s\n", msg); } } \ No newline at end of file