simplifying logging, changing App events to std::function

This commit is contained in:
Noel Berry 2021-02-21 18:43:42 -08:00
parent f8741e27e2
commit a72cd5cab6
4 changed files with 30 additions and 20 deletions

View File

@ -1,8 +1,13 @@
#pragma once
#include <memory>
#include <functional>
#include <blah/core/log.h>
namespace Blah
{
using AppEventFn = std::function<void()>;
using AppLogFn = std::function<void(const char* message, Log::Category category)>;
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();
};

View File

@ -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, ...);

View File

@ -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

View File

@ -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);
}
}