Improved logging based of fmtlib.
This commit is contained in:
parent
e9da60c056
commit
a0a84f30f8
|
|
@ -1,13 +1,13 @@
|
|||
# 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_REQUIRED ON )
|
||||
set( CMAKE_CXX_EXTENSIONS OFF )
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
set( CMAKE_CXX_FLAGS -Wall )
|
||||
set(CMAKE_CXX_FLAGS -Wall)
|
||||
|
||||
add_subdirectory( "aster_core" )
|
||||
add_subdirectory("aster_core")
|
||||
|
|
|
|||
|
|
@ -1,25 +1,27 @@
|
|||
# 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( glfw3 CONFIG REQUIRED )
|
||||
find_path( SCOTTT_DEBUGBREAK_INCLUDE_DIRS "debugbreak.h" )
|
||||
find_package( Vulkan REQUIRED )
|
||||
find_package(glm CONFIG REQUIRED)
|
||||
find_package(glfw3 CONFIG REQUIRED)
|
||||
find_path(SCOTTT_DEBUGBREAK_INCLUDE_DIRS "debugbreak.h")
|
||||
find_package(Vulkan 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( SOURCE_FILES "logger.cpp" "global.cpp" "context.cpp" "window.cpp" )
|
||||
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")
|
||||
|
||||
add_library( aster_core ${SOURCE_FILES} ${HEADER_FILES} )
|
||||
set_property( TARGET aster_core PROPERTY CXX_STANDARD 23 )
|
||||
add_library(aster_core ${SOURCE_FILES} ${HEADER_FILES})
|
||||
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 glfw )
|
||||
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 glm::glm-header-only)
|
||||
target_link_libraries(aster_core PRIVATE glfw)
|
||||
target_include_directories(aster_core PRIVATE ${SCOTTT_DEBUGBREAK_INCLUDE_DIRS})
|
||||
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" )
|
||||
target_link_libraries( aster_exe PRIVATE aster_core )
|
||||
target_link_libraries( aster_exe PRIVATE glm::glm-header-only )
|
||||
add_executable(aster_exe "aster.cpp")
|
||||
target_link_libraries(aster_exe PRIVATE aster_core)
|
||||
target_link_libraries(aster_exe PRIVATE glm::glm-header-only)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
#include <iostream>
|
||||
#include "constants.h"
|
||||
#include "glfw_context.h"
|
||||
#include "window.h"
|
||||
|
||||
int main(int, char**) {
|
||||
|
||||
GlfwContext* glfw = new GlfwContext();
|
||||
Context* context = new Context("Aster", VERSION);
|
||||
Window* window = new Window("Aster1", context, { 640, 480 });
|
||||
int main(int, char **) {
|
||||
GlfwContext *glfw = new GlfwContext();
|
||||
Context *context = new Context("Aster", VERSION);
|
||||
Window *window = new Window("Aster1", context, { 640, 480 });
|
||||
|
||||
delete window;
|
||||
delete context;
|
||||
|
|
@ -15,4 +13,3 @@ int main(int, char**) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ VKAPI_ATTR b32 VKAPI_CALL Context::debug_callback(VkDebugUtilsMessageSeverityFla
|
|||
|
||||
if (message_type & MessageTypeBits::eValidation) {
|
||||
if (severity & SeverityBits::eError)
|
||||
ERROR(_callback_data->pMessage);
|
||||
ERROR("{}", _callback_data->pMessage);
|
||||
if (severity & SeverityBits::eWarning)
|
||||
WARN(_callback_data->pMessage);
|
||||
WARN("{}", _callback_data->pMessage);
|
||||
if (severity & SeverityBits::eInfo)
|
||||
INFO(_callback_data->pMessage);
|
||||
INFO("{}", _callback_data->pMessage);
|
||||
if (severity & SeverityBits::eVerbose)
|
||||
VERBOSE(_callback_data->pMessage);
|
||||
VERBOSE("{}", _callback_data->pMessage);
|
||||
}
|
||||
|
||||
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()),
|
||||
.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)
|
||||
ELSE_INFO("Instance Created.");
|
||||
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
|
||||
if (enable_validation_layers) {
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ struct GlfwContext {
|
|||
static i32 post_error() noexcept {
|
||||
static const char* error_ = nullptr;
|
||||
const auto code = glfwGetError(&error_);
|
||||
ERROR("GLFW "s + error_);
|
||||
ERROR("GLFW {}", error_);
|
||||
return code;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
#include "constants.h"
|
||||
#include <debugbreak.h>
|
||||
#include <fmt/core.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Logger {
|
||||
|
|
@ -55,7 +57,7 @@ struct Logger {
|
|||
template <LogType LogLevel>
|
||||
void log(const std::string_view &_message, const char *_loc, u32 _line) const {
|
||||
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 constexpr (LogLevel == LogType::eError) {
|
||||
|
|
@ -67,7 +69,7 @@ struct Logger {
|
|||
template <LogType LogLevel>
|
||||
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) {
|
||||
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 constexpr (LogLevel == LogType::eError) {
|
||||
|
|
@ -79,52 +81,52 @@ struct Logger {
|
|||
|
||||
extern Logger g_logger;
|
||||
|
||||
#define ERROR(msg) g_logger.log<Logger::LogType::eError>(msg, __FILE__, __LINE__)
|
||||
#define WARN(msg) g_logger.log<Logger::LogType::eWarning>(msg, __FILE__, __LINE__)
|
||||
#define INFO(msg) g_logger.log<Logger::LogType::eInfo>(msg, __FILE__, __LINE__)
|
||||
#define ERROR(...) g_logger.log<Logger::LogType::eError>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define WARN(...) g_logger.log<Logger::LogType::eWarning>(fmt::format(__VA_ARGS__), __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]] \
|
||||
g_logger.log_cond<Logger::LogType::eError>(#expr, msg, __FILE__, __LINE__)
|
||||
#define WARN_IF(expr, msg) \
|
||||
g_logger.log_cond<Logger::LogType::eError>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define WARN_IF(expr, ...) \
|
||||
if (cast<bool>(expr)) [[unlikely]] \
|
||||
g_logger.log_cond<Logger::LogType::eWarning>(#expr, msg, __FILE__, __LINE__)
|
||||
#define INFO_IF(expr, msg) \
|
||||
g_logger.log_cond<Logger::LogType::eWarning>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define INFO_IF(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__)
|
||||
#define ELSE_IF_WARN(expr, msg) \
|
||||
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, ...) \
|
||||
; \
|
||||
else if (cast<bool>(expr)) [[unlikely]] g_logger.log_cond<Logger::LogType::eWarning>(#expr, msg, __FILE__, __LINE__)
|
||||
#define ELSE_IF_INFO(expr, msg) \
|
||||
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, ...) \
|
||||
; \
|
||||
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__)
|
||||
#define ELSE_WARN(msg) \
|
||||
else [[unlikely]] g_logger.log<Logger::LogType::eError>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define ELSE_WARN(...) \
|
||||
; \
|
||||
else [[unlikely]] g_logger.log<Logger::LogType::eWarning>(msg, __FILE__, __LINE__)
|
||||
#define ELSE_INFO(msg) \
|
||||
else [[unlikely]] g_logger.log<Logger::LogType::eWarning>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#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)
|
||||
|
||||
#define DEBUG(msg) g_logger.log<Logger::LogType::eDebug>(msg, __FILE__, __LINE__)
|
||||
#define DEBUG_IF(expr, msg) \
|
||||
#define DEBUG(...) g_logger.log<Logger::LogType::eDebug>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define DEBUG_IF(expr, ...) \
|
||||
if (cast<bool>(expr)) \
|
||||
g_logger.log_cond<Logger::LogType::eDebug>(#expr, msg, __FILE__, __LINE__)
|
||||
#define ELSE_IF_DEBUG(expr, msg) \
|
||||
g_logger.log_cond<Logger::LogType::eDebug>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define ELSE_IF_DEBUG(expr, ...) \
|
||||
; \
|
||||
else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eDebug>(#expr, msg, __FILE__, __LINE__)
|
||||
#define ELSE_DEBUG(msg) \
|
||||
else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eDebug>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#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)
|
||||
|
||||
|
|
@ -145,16 +147,16 @@ extern Logger g_logger;
|
|||
|
||||
#if !defined(VERBOSE_LOG_DISABLED) && !defined(NDEBUG)
|
||||
|
||||
#define VERBOSE(msg) g_logger.log<Logger::LogType::eVerbose>(msg, __FILE__, __LINE__)
|
||||
#define VERBOSE_IF(expr, msg) \
|
||||
#define VERBOSE(...) g_logger.log<Logger::LogType::eVerbose>(fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define VERBOSE_IF(expr, ...) \
|
||||
if (cast<bool>(expr)) \
|
||||
g_logger.log_cond<Logger::LogType::eVerbose>(#expr, msg, __FILE__, __LINE__)
|
||||
#define ELSE_IF_VERBOSE(expr, msg) \
|
||||
g_logger.log_cond<Logger::LogType::eVerbose>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#define ELSE_IF_VERBOSE(expr, ...) \
|
||||
; \
|
||||
else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eVerbose>(#expr, msg, __FILE__, __LINE__)
|
||||
#define ELSE_VERBOSE(msg) \
|
||||
else if (cast<bool>(expr)) g_logger.log_cond<Logger::LogType::eVerbose>(#expr, fmt::format(__VA_ARGS__), __FILE__, __LINE__)
|
||||
#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)
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ Window::Window(const std::string_view& _title, Context* _context, vk::Extent2D _
|
|||
glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE);
|
||||
|
||||
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) {
|
||||
auto code = GlfwContext::post_error();
|
||||
glfwTerminate();
|
||||
|
|
@ -38,7 +38,7 @@ Window::Window(const std::string_view& _title, Context* _context, vk::Extent2D _
|
|||
|
||||
VkSurfaceKHR 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_);
|
||||
}
|
||||
|
||||
|
|
@ -74,5 +74,5 @@ Window::~Window() {
|
|||
}
|
||||
monitor = nullptr;
|
||||
|
||||
INFO("Window '" + name + "' Destroyed");
|
||||
INFO("Window '{}' Destroyed", name);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
"glfw3",
|
||||
"glm",
|
||||
"scottt-debugbreak",
|
||||
"vulkan-memory-allocator"
|
||||
"vulkan-memory-allocator",
|
||||
"fmt"
|
||||
]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue