Improved logging based of fmtlib.

This commit is contained in:
Anish Bhobe 2024-06-15 17:45:22 +02:00
parent e9da60c056
commit a0a84f30f8
8 changed files with 85 additions and 83 deletions

View File

@ -1,13 +1,13 @@
# CMakeLists.txt ; Top-level CMake project file. # CMakeLists.txt ; Top-level CMake project file.
cmake_minimum_required( VERSION 3.13 ) cmake_minimum_required(VERSION 3.13)
project( Aster VERSION 0.1.0 ) project(Aster VERSION 0.1.0)
set( CMAKE_CXX_STANDARD 23 ) set(CMAKE_CXX_STANDARD 20)
set( CMAKE_CXX_STANDARD_REQUIRED ON ) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set( CMAKE_CXX_EXTENSIONS OFF ) set(CMAKE_CXX_EXTENSIONS OFF)
set( CMAKE_CXX_FLAGS -Wall ) set(CMAKE_CXX_FLAGS -Wall)
add_subdirectory( "aster_core" ) add_subdirectory("aster_core")

View File

@ -1,25 +1,27 @@
# CMakeList.txt ; CMake project for Aster Core # CMakeList.txt ; CMake project for Aster Core
cmake_minimum_required( VERSION 3.13 ) cmake_minimum_required(VERSION 3.13)
find_package( glm CONFIG REQUIRED ) find_package(glm CONFIG REQUIRED)
find_package( glfw3 CONFIG REQUIRED ) find_package(glfw3 CONFIG REQUIRED)
find_path( SCOTTT_DEBUGBREAK_INCLUDE_DIRS "debugbreak.h" ) find_path(SCOTTT_DEBUGBREAK_INCLUDE_DIRS "debugbreak.h")
find_package( Vulkan REQUIRED ) find_package(Vulkan REQUIRED)
# find_package( VulkanHeaders CONFIG REQUIRED ) # find_package( VulkanHeaders CONFIG REQUIRED )
find_package( VulkanMemoryAllocator CONFIG REQUIRED ) find_package(fmt CONFIG REQUIRED)
find_package(VulkanMemoryAllocator CONFIG REQUIRED)
set( HEADER_FILES "constants.h" "config.h" "logger.h" "global.h" "context.h" "window.h" ) set(HEADER_FILES "constants.h" "config.h" "logger.h" "global.h" "context.h" "window.h")
set( SOURCE_FILES "logger.cpp" "global.cpp" "context.cpp" "window.cpp" ) set(SOURCE_FILES "logger.cpp" "global.cpp" "context.cpp" "window.cpp")
add_library( aster_core ${SOURCE_FILES} ${HEADER_FILES} ) add_library(aster_core ${SOURCE_FILES} ${HEADER_FILES})
set_property( TARGET aster_core PROPERTY CXX_STANDARD 23 ) set_property(TARGET aster_core PROPERTY CXX_STANDARD 20)
target_link_libraries( aster_core PRIVATE glm::glm-header-only ) target_link_libraries(aster_core PRIVATE glm::glm-header-only)
target_link_libraries( aster_core PRIVATE glfw ) target_link_libraries(aster_core PRIVATE glfw)
target_include_directories( aster_core PRIVATE ${SCOTTT_DEBUGBREAK_INCLUDE_DIRS}) target_include_directories(aster_core PRIVATE ${SCOTTT_DEBUGBREAK_INCLUDE_DIRS})
target_link_libraries( aster_core PRIVATE Vulkan::Vulkan Vulkan::Headers GPUOpen::VulkanMemoryAllocator ) target_link_libraries(aster_core PRIVATE fmt::fmt)
target_link_libraries(aster_core PRIVATE Vulkan::Vulkan Vulkan::Headers GPUOpen::VulkanMemoryAllocator)
add_executable( aster_exe "aster.cpp" ) add_executable(aster_exe "aster.cpp")
target_link_libraries( aster_exe PRIVATE aster_core ) target_link_libraries(aster_exe PRIVATE aster_core)
target_link_libraries( aster_exe PRIVATE glm::glm-header-only ) target_link_libraries(aster_exe PRIVATE glm::glm-header-only)

View File

@ -1,13 +1,11 @@
#include <iostream>
#include "constants.h" #include "constants.h"
#include "glfw_context.h" #include "glfw_context.h"
#include "window.h" #include "window.h"
int main(int, char**) { int main(int, char **) {
GlfwContext *glfw = new GlfwContext();
GlfwContext* glfw = new GlfwContext(); Context *context = new Context("Aster", VERSION);
Context* context = new Context("Aster", VERSION); Window *window = new Window("Aster1", context, { 640, 480 });
Window* window = new Window("Aster1", context, { 640, 480 });
delete window; delete window;
delete context; delete context;
@ -15,4 +13,3 @@ int main(int, char**) {
return 0; return 0;
} }

View File

@ -16,13 +16,13 @@ VKAPI_ATTR b32 VKAPI_CALL Context::debug_callback(VkDebugUtilsMessageSeverityFla
if (message_type & MessageTypeBits::eValidation) { if (message_type & MessageTypeBits::eValidation) {
if (severity & SeverityBits::eError) if (severity & SeverityBits::eError)
ERROR(_callback_data->pMessage); ERROR("{}", _callback_data->pMessage);
if (severity & SeverityBits::eWarning) if (severity & SeverityBits::eWarning)
WARN(_callback_data->pMessage); WARN("{}", _callback_data->pMessage);
if (severity & SeverityBits::eInfo) if (severity & SeverityBits::eInfo)
INFO(_callback_data->pMessage); INFO("{}", _callback_data->pMessage);
if (severity & SeverityBits::eVerbose) if (severity & SeverityBits::eVerbose)
VERBOSE(_callback_data->pMessage); VERBOSE("{}", _callback_data->pMessage);
} }
return false; return false;
@ -75,7 +75,7 @@ void Context::init(const std::string_view &_app_name, const Version &_app_versio
.enabledExtensionCount = cast<u32>(vulkan_extensions.size()), .enabledExtensionCount = cast<u32>(vulkan_extensions.size()),
.ppEnabledExtensionNames = vulkan_extensions.data(), .ppEnabledExtensionNames = vulkan_extensions.data(),
}); });
ERROR_IF(failed(result), "Failed to create Vulkan instance with "s + to_string(result)) ERROR_IF(failed(result), "Failed to create Vulkan instance with {}", to_string(result))
THEN_CRASH(result) THEN_CRASH(result)
ELSE_INFO("Instance Created."); ELSE_INFO("Instance Created.");
VULKAN_HPP_DEFAULT_DISPATCHER.init(instance); VULKAN_HPP_DEFAULT_DISPATCHER.init(instance);
@ -83,7 +83,7 @@ void Context::init(const std::string_view &_app_name, const Version &_app_versio
// Debug Messenger // Debug Messenger
if (enable_validation_layers) { if (enable_validation_layers) {
tie(result, debug_messenger) = instance.createDebugUtilsMessengerEXT(debug_messenger_create_info); tie(result, debug_messenger) = instance.createDebugUtilsMessengerEXT(debug_messenger_create_info);
ERROR_IF(failed(result), "Debug Messenger creation failed with "s + to_string(result)) ERROR_IF(failed(result), "Debug Messenger creation failed with {}", to_string(result))
ELSE_INFO("Debug Messenger Created."); ELSE_INFO("Debug Messenger Created.");
} }
} }

View File

@ -11,7 +11,7 @@ struct GlfwContext {
static i32 post_error() noexcept { static i32 post_error() noexcept {
static const char* error_ = nullptr; static const char* error_ = nullptr;
const auto code = glfwGetError(&error_); const auto code = glfwGetError(&error_);
ERROR("GLFW "s + error_); ERROR("GLFW {}", error_);
return code; return code;
} }

View File

@ -7,6 +7,8 @@
#include "constants.h" #include "constants.h"
#include <debugbreak.h> #include <debugbreak.h>
#include <fmt/core.h>
#include <string> #include <string>
struct Logger { struct Logger {
@ -55,7 +57,7 @@ struct Logger {
template <LogType LogLevel> template <LogType LogLevel>
void log(const std::string_view &_message, const char *_loc, u32 _line) const { void log(const std::string_view &_message, const char *_loc, u32 _line) const {
if (cast<u32>(LogLevel) <= minimum_logging_level) { if (cast<u32>(LogLevel) <= minimum_logging_level) {
printf("%s%s %s%s| at %s:%u%s\n", to_color_cstr<LogLevel>(), to_cstr<LogLevel>(), _message.data(), ANSI_Black, _loc, _line, ANSI_Reset); fmt::println("{}{} {}{}| at {}:{}{}", to_color_cstr<LogLevel>(), to_cstr<LogLevel>(), _message.data(), ANSI_Black, _loc, _line, ANSI_Reset);
} }
#if !defined(NDEBUG) #if !defined(NDEBUG)
if constexpr (LogLevel == LogType::eError) { if constexpr (LogLevel == LogType::eError) {
@ -67,7 +69,7 @@ struct Logger {
template <LogType LogLevel> template <LogType LogLevel>
void log_cond(const char *_expr_str, const std::string_view &_message, const char *_loc, u32 _line) const { void log_cond(const char *_expr_str, const std::string_view &_message, const char *_loc, u32 _line) const {
if (cast<u32>(LogLevel) <= minimum_logging_level) { if (cast<u32>(LogLevel) <= minimum_logging_level) {
printf("%s%s (%s) %s%s| at %s:%u%s\n", to_color_cstr<LogLevel>(), to_cstr<LogLevel>(), _expr_str, _message.data(), ANSI_Black, _loc, _line, ANSI_Reset); fmt::println("{}{} ({}) {}{}| at {}:{}{}", to_color_cstr<LogLevel>(), to_cstr<LogLevel>(), _expr_str, _message.data(), ANSI_Black, _loc, _line, ANSI_Reset);
} }
#if !defined(NDEBUG) #if !defined(NDEBUG)
if constexpr (LogLevel == LogType::eError) { if constexpr (LogLevel == LogType::eError) {
@ -79,52 +81,52 @@ struct Logger {
extern Logger g_logger; extern Logger g_logger;
#define ERROR(msg) g_logger.log<Logger::LogType::eError>(msg, __FILE__, __LINE__) #define ERROR(...) g_logger.log<Logger::LogType::eError>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define WARN(msg) g_logger.log<Logger::LogType::eWarning>(msg, __FILE__, __LINE__) #define WARN(...) g_logger.log<Logger::LogType::eWarning>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define INFO(msg) g_logger.log<Logger::LogType::eInfo>(msg, __FILE__, __LINE__) #define INFO(...) g_logger.log<Logger::LogType::eInfo>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ERROR_IF(expr, msg) \ #define ERROR_IF(expr, ...) \
if (cast<bool>(expr)) [[unlikely]] \ if (cast<bool>(expr)) [[unlikely]] \
g_logger.log_cond<Logger::LogType::eError>(#expr, msg, __FILE__, __LINE__) g_logger.log_cond<Logger::LogType::eError>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define WARN_IF(expr, msg) \ #define WARN_IF(expr, ...) \
if (cast<bool>(expr)) [[unlikely]] \ if (cast<bool>(expr)) [[unlikely]] \
g_logger.log_cond<Logger::LogType::eWarning>(#expr, msg, __FILE__, __LINE__) g_logger.log_cond<Logger::LogType::eWarning>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define INFO_IF(expr, msg) \ #define INFO_IF(expr, ...) \
if (cast<bool>(expr)) \ if (cast<bool>(expr)) \
g_logger.log_cond<Logger::LogType::eInfo>(#expr, msg, __FILE__, __LINE__) g_logger.log_cond<Logger::LogType::eInfo>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_IF_ERROR(expr, msg) \ #define ELSE_IF_ERROR(expr, ...) \
; \ ; \
else if (cast<bool>(expr)) [[unlikely]] g_logger.log_cond<Logger::LogType::eError>(#expr, msg, __FILE__, __LINE__) else if (cast<bool>(expr)) [[unlikely]] g_logger.log_cond<Logger::LogType::eError>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_IF_WARN(expr, msg) \ #define ELSE_IF_WARN(expr, ...) \
; \ ; \
else if (cast<bool>(expr)) [[unlikely]] g_logger.log_cond<Logger::LogType::eWarning>(#expr, msg, __FILE__, __LINE__) else if (cast<bool>(expr)) [[unlikely]] g_logger.log_cond<Logger::LogType::eWarning>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_IF_INFO(expr, msg) \ #define ELSE_IF_INFO(expr, ...) \
; \ ; \
else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eInfo>(#expr, msg, __FILE__, __LINE__) else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eInfo>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_ERROR(msg) \ #define ELSE_ERROR(...) \
; \ ; \
else [[unlikely]] g_logger.log<Logger::LogType::eError>(msg, __FILE__, __LINE__) else [[unlikely]] g_logger.log<Logger::LogType::eError>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_WARN(msg) \ #define ELSE_WARN(...) \
; \ ; \
else [[unlikely]] g_logger.log<Logger::LogType::eWarning>(msg, __FILE__, __LINE__) else [[unlikely]] g_logger.log<Logger::LogType::eWarning>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_INFO(msg) \ #define ELSE_INFO(...) \
; \ ; \
else g_logger.log<Logger::LogType::eInfo>(msg, __FILE__, __LINE__) else g_logger.log<Logger::LogType::eInfo>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#if !defined(DEBUG_LOG_DISABLED) && !defined(NDEBUG) #if !defined(DEBUG_LOG_DISABLED) && !defined(NDEBUG)
#define DEBUG(msg) g_logger.log<Logger::LogType::eDebug>(msg, __FILE__, __LINE__) #define DEBUG(...) g_logger.log<Logger::LogType::eDebug>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define DEBUG_IF(expr, msg) \ #define DEBUG_IF(expr, ...) \
if (cast<bool>(expr)) \ if (cast<bool>(expr)) \
g_logger.log_cond<Logger::LogType::eDebug>(#expr, msg, __FILE__, __LINE__) g_logger.log_cond<Logger::LogType::eDebug>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_IF_DEBUG(expr, msg) \ #define ELSE_IF_DEBUG(expr, ...) \
; \ ; \
else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eDebug>(#expr, msg, __FILE__, __LINE__) else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eDebug>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_DEBUG(msg) \ #define ELSE_DEBUG(...) \
; \ ; \
else [[unlikely]] g_logger.log<Logger::LogType::eDebug>(msg, __FILE__, __LINE__) else [[unlikely]] g_logger.log<Logger::LogType::eDebug>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#else // !defined(DEBUG_LOG_DISABLED) #else // !defined(DEBUG_LOG_DISABLED)
@ -145,16 +147,16 @@ extern Logger g_logger;
#if !defined(VERBOSE_LOG_DISABLED) && !defined(NDEBUG) #if !defined(VERBOSE_LOG_DISABLED) && !defined(NDEBUG)
#define VERBOSE(msg) g_logger.log<Logger::LogType::eVerbose>(msg, __FILE__, __LINE__) #define VERBOSE(...) g_logger.log<Logger::LogType::eVerbose>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define VERBOSE_IF(expr, msg) \ #define VERBOSE_IF(expr, ...) \
if (cast<bool>(expr)) \ if (cast<bool>(expr)) \
g_logger.log_cond<Logger::LogType::eVerbose>(#expr, msg, __FILE__, __LINE__) g_logger.log_cond<Logger::LogType::eVerbose>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_IF_VERBOSE(expr, msg) \ #define ELSE_IF_VERBOSE(expr, ...) \
; \ ; \
else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eVerbose>(#expr, msg, __FILE__, __LINE__) else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eVerbose>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#define ELSE_VERBOSE(msg) \ #define ELSE_VERBOSE(...) \
; \ ; \
else g_logger.log<Logger::LogType::eVerbose>(msg, __FILE__, __LINE__) else g_logger.log<Logger::LogType::eVerbose>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
#else // !defined(DEBUG_LOG_DISABLED) #else // !defined(DEBUG_LOG_DISABLED)

View File

@ -24,7 +24,7 @@ Window::Window(const std::string_view& _title, Context* _context, vk::Extent2D _
glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE); glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE);
window = glfwCreateWindow(extent.width, extent.height, name.data(), full_screen ? monitor : nullptr, nullptr); window = glfwCreateWindow(extent.width, extent.height, name.data(), full_screen ? monitor : nullptr, nullptr);
ERROR_IF(window == nullptr, "Window creation failed") ELSE_INFO(std::fmt("Window '%s' created with resolution '%dx%d'", name.data(), extent.width, extent.height)); ERROR_IF(window == nullptr, "Window creation failed") ELSE_INFO("Window '{}' created with resolution '{}x{}'", name, extent.width, extent.height);
if (window == nullptr) { if (window == nullptr) {
auto code = GlfwContext::post_error(); auto code = GlfwContext::post_error();
glfwTerminate(); glfwTerminate();
@ -38,7 +38,7 @@ Window::Window(const std::string_view& _title, Context* _context, vk::Extent2D _
VkSurfaceKHR surface_; VkSurfaceKHR surface_;
auto result = cast<vk::Result>(glfwCreateWindowSurface(cast<VkInstance>(_context->instance), window, nullptr, &surface_)); auto result = cast<vk::Result>(glfwCreateWindowSurface(cast<VkInstance>(_context->instance), window, nullptr, &surface_));
ERROR_IF(failed(result), "Failed to create Surface with "s + to_string(result)) THEN_CRASH(result) ELSE_INFO("Surface Created"); ERROR_IF(failed(result), "Failed to create Surface with {}", to_string(result)) THEN_CRASH(result) ELSE_INFO("Surface Created");
surface = vk::SurfaceKHR(surface_); surface = vk::SurfaceKHR(surface_);
} }
@ -74,5 +74,5 @@ Window::~Window() {
} }
monitor = nullptr; monitor = nullptr;
INFO("Window '" + name + "' Destroyed"); INFO("Window '{}' Destroyed", name);
} }

View File

@ -3,6 +3,7 @@
"glfw3", "glfw3",
"glm", "glm",
"scottt-debugbreak", "scottt-debugbreak",
"vulkan-memory-allocator" "vulkan-memory-allocator",
"fmt"
] ]
} }