diff --git a/engine/3rdparty/zlog/include/source_location.h b/engine/3rdparty/zlog/include/source_location.h new file mode 100644 index 0000000..6f8bee1 --- /dev/null +++ b/engine/3rdparty/zlog/include/source_location.h @@ -0,0 +1,88 @@ +#ifndef NOSTD_SOURCE_LOCATION_HPP +#define NOSTD_SOURCE_LOCATION_HPP + +#pragma once + +#include + +namespace zlog { + struct source_location { + public: + #if not defined(__apple_build_version__) and defined(__clang__) and (__clang_major__ >= 9) + static constexpr source_location current(const char* fileName = __builtin_FILE(), + const char* functionName = __builtin_FUNCTION(), + const uint_least32_t lineNumber = __builtin_LINE(), + const uint_least32_t columnOffset = __builtin_COLUMN()) noexcept + #elif defined(__GNUC__) and (__GNUC__ > 4 or (__GNUC__ == 4 and __GNUC_MINOR__ >= 8)) + static constexpr source_location current(const char* fileName = __builtin_FILE(), + const char* functionName = __builtin_FUNCTION(), + const uint_least32_t lineNumber = __builtin_LINE(), + const uint_least32_t columnOffset = 0) noexcept + #else + static constexpr source_location current(const char* fileName = "unsupported", + const char* functionName = "unsupported", + const uint_least32_t lineNumber = 0, + const uint_least32_t columnOffset = 0) noexcept + #endif + { + return source_location(fileName, functionName, lineNumber, columnOffset); + } + + source_location(const source_location&) = default; + source_location(source_location&&) = default; + + constexpr const char* file_name() const noexcept + { + return fileName; + } + + constexpr const char* function_name() const noexcept + { + return functionName; + } + + constexpr uint_least32_t line() const noexcept + { + return lineNumber; + } + + constexpr std::uint_least32_t column() const noexcept + { + return columnOffset; + } + + private: + constexpr source_location(const char* fileName, const char* functionName, const uint_least32_t lineNumber, + const uint_least32_t columnOffset) noexcept + : fileName(fileName) + , functionName(functionName) + , lineNumber(lineNumber) + , columnOffset(columnOffset) + { + } + + const char* fileName; + const char* functionName; + const std::uint_least32_t lineNumber; + const std::uint_least32_t columnOffset; + }; + + [[nodiscard]] constexpr auto get_log_source_location( + const source_location& location) { + return spdlog::source_loc{location.file_name(), + static_cast(location.line()), + location.function_name()}; + } + + struct format_with_location { + std::string_view value; + spdlog::source_loc loc; + + template + format_with_location(const String& s, const source_location& location = + source_location::current()) + : value{ s }, loc{ get_log_source_location(location) } {} + }; +} // namespace nostd + +#endif \ No newline at end of file diff --git a/engine/3rdparty/zlog/include/zlog.h b/engine/3rdparty/zlog/include/zlog.h index 6e926e4..c5283d3 100644 --- a/engine/3rdparty/zlog/include/zlog.h +++ b/engine/3rdparty/zlog/include/zlog.h @@ -1,72 +1,43 @@ #pragma once #include "spdlog/spdlog.h" - +#include "source_location.h" #include #include - -class zlog final -{ -public: - enum class LogLevel : uint8_t - { - debug, - info, - warn, - error, - fatal - }; - -public: - zlog(); - ~zlog(); - - template - void log(LogLevel level, TARGS&&... args) - { - switch (level) - { - case LogLevel::debug: - m_logger->debug(std::forward(args)...); - break; - case LogLevel::info: - m_logger->info(std::forward(args)...); - break; - case LogLevel::warn: - m_logger->warn(std::forward(args)...); - break; - case LogLevel::error: - m_logger->error(std::forward(args)...); - break; - case LogLevel::fatal: - m_logger->critical(std::forward(args)...); - fatalCallback(std::forward(args)...); - break; - default: - break; +namespace zlog { + using level_enum = spdlog::level::level_enum; + class zloger { + private: + std::shared_ptr m_logger; + public: + zloger(); + ~zloger(); + template + void log(level_enum level, format_with_location& fmt, Args &&...args) { + m_logger->log(fmt.loc, level, fmt::runtime(fmt.value), std::forward(args)...); } - } - - template - void fatalCallback(TARGS&&... args) - { - const std::string format_str = fmt::format(std::forward(args)...); + }; + extern zloger zlog; + template + void info(format_with_location fmt, Args &&...args) { + zlog.log(level_enum::info, fmt, std::forward(args)...); + }; + template + void debug(format_with_location fmt, Args &&...args) { + zlog.log(level_enum::debug, fmt, std::forward(args)...); + }; + template + void warn(format_with_location fmt, Args &&...args) { + zlog.log(level_enum::warn, fmt, std::forward(args)...); + }; + template + void error(format_with_location fmt, Args &&...args) { + zlog.log(level_enum::err, fmt, std::forward(args)...); + }; + template + void fatal(format_with_location fmt, Args &&...args) { + zlog.log(level_enum::critical, fmt, std::forward(args)...); + const std::string format_str = fmt::format(std::forward(args)...); throw std::runtime_error(format_str); - } - -private: - std::shared_ptr m_logger; -}; -extern zlog gLog; -#define LOG_HELPER(LOG_LEVEL, ...) \ - gLog.log(LOG_LEVEL, "[" + std::string(__FUNCTION__) + "] " + __VA_ARGS__); - -#define LOG_DEBUG(...) LOG_HELPER(zlog::LogLevel::debug, __VA_ARGS__); - -#define LOG_INFO(...) LOG_HELPER(zlog::LogLevel::info, __VA_ARGS__); - -#define LOG_WARN(...) LOG_HELPER(zlog::LogLevel::warn, __VA_ARGS__); - -#define LOG_ERROR(...) LOG_HELPER(zlog::LogLevel::error, __VA_ARGS__); - -#define LOG_FATAL(...) LOG_HELPER(zlog::LogLevel::fatal, __VA_ARGS__); \ No newline at end of file + }; +}; \ No newline at end of file diff --git a/engine/3rdparty/zlog/src/zlog.cpp b/engine/3rdparty/zlog/src/zlog.cpp index cabf788..600a728 100644 --- a/engine/3rdparty/zlog/src/zlog.cpp +++ b/engine/3rdparty/zlog/src/zlog.cpp @@ -3,29 +3,34 @@ #include #include #include -zlog gLog; -zlog::zlog() -{ - auto console_sink = std::make_shared(); - console_sink->set_level(spdlog::level::trace); - console_sink->set_pattern("[%Y-%m-%d %H:%M:%S] %l %^%v%$"); +namespace zlog { + zloger zlog; + zloger::zloger() + { + auto console_sink = std::make_shared(); + console_sink->set_level(spdlog::level::trace); + console_sink->set_pattern("[%Y-%m-%d %H:%M:%S][%s:%#] %-8l %^%v%$"); - const spdlog::sinks_init_list sink_list = {console_sink}; + auto file_sink = std::make_shared("test.log", true); + file_sink->set_level(spdlog::level::trace); + //file_sink->set_pattern("[%Y-%m-%d %H:%M:%S][%s:%#] %-8l %^%v%$"); - spdlog::init_thread_pool(8192, 1); + const spdlog::sinks_init_list sink_list = { console_sink, file_sink }; - m_logger = std::make_shared("muggle_logger", - sink_list.begin(), - sink_list.end(), - spdlog::thread_pool(), - spdlog::async_overflow_policy::block); - m_logger->set_level(spdlog::level::trace); + spdlog::init_thread_pool(8192, 1); - spdlog::register_logger(m_logger); + m_logger = std::make_shared("zlogger", + sink_list.begin(), + sink_list.end(), + spdlog::thread_pool(), + spdlog::async_overflow_policy::block); + m_logger->set_level(spdlog::level::trace); + m_logger->set_pattern("[%Y-%m-%d %H:%M:%S][%s:%#] %-8l %^%v%$"); + spdlog::register_logger(m_logger); + } + zloger::~zloger() + { + m_logger->flush(); + spdlog::drop_all(); + } } - -zlog::~zlog() -{ - m_logger->flush(); - spdlog::drop_all(); -} \ No newline at end of file diff --git a/engine/3rdparty/zlog/test/01file.cpp b/engine/3rdparty/zlog/test/01file.cpp new file mode 100644 index 0000000..2bc7556 --- /dev/null +++ b/engine/3rdparty/zlog/test/01file.cpp @@ -0,0 +1,8 @@ +#include +#include "zlog.h" +int main() { + zlog::info("hello info"); + zlog::warn("hello {}", "warn"); + zlog::error("hello {}", "error"); + return 0; +} diff --git a/engine/3rdparty/zlog/xmake.lua b/engine/3rdparty/zlog/xmake.lua index d116715..c00c0fc 100644 --- a/engine/3rdparty/zlog/xmake.lua +++ b/engine/3rdparty/zlog/xmake.lua @@ -2,6 +2,12 @@ set_languages("cxx20") target("zlog") set_kind("static") add_includedirs("include", {public = true}) - add_packages("spdlog") + add_packages("spdlog", {public = true}) add_files("src/*.cpp") - add_headerfiles("include/*.h") \ No newline at end of file + add_headerfiles("include/*.h") + +target("zlog_test01_file") + set_languages("cxx20") + set_kind("binary") + add_deps("zlog") + add_files("test/01file.cpp") \ No newline at end of file diff --git a/engine/log/asmble b/engine/logs/asmble similarity index 100% rename from engine/log/asmble rename to engine/logs/asmble diff --git a/engine/src/engine/vulkanapi/backend.cpp b/engine/src/engine/vulkanapi/backend.cpp index 6d1474d..ef22c38 100644 --- a/engine/src/engine/vulkanapi/backend.cpp +++ b/engine/src/engine/vulkanapi/backend.cpp @@ -3,7 +3,8 @@ namespace vulkanapi { Backend::Backend(const char* appName, int deviceIndex) { - mInstance = new Instance(appName); + auto instanceCreator = Instance::InstanceCreator(); + mInstance = new Instance(instanceCreator); std::vector available_devices; mInstance->EnumerateAvailablePhysicalDevices(available_devices); int device_count = available_devices.size(); diff --git a/engine/src/engine/vulkanapi/vulkan.h b/engine/src/engine/vulkanapi/vulkan.h index 86d16ad..1b1aa62 100644 --- a/engine/src/engine/vulkanapi/vulkan.h +++ b/engine/src/engine/vulkanapi/vulkan.h @@ -12,9 +12,9 @@ namespace vulkanapi { #define _USE_GRAPHIC_DEBUG -#define LOG_VULKAN(VkResult, info) \ +#define LOG_VULKAN(VkResult, info, ...) \ if (VkResult != VK_SUCCESS) { \ - LOG_ERROR(info)\ + LOG_ERROR(info,##__VA_ARGS__)\ } using voidFn = std::function; diff --git a/engine/src/engine/vulkanapi/wrapper/instance.cpp b/engine/src/engine/vulkanapi/wrapper/instance.cpp index fe5ca44..9730caf 100644 --- a/engine/src/engine/vulkanapi/wrapper/instance.cpp +++ b/engine/src/engine/vulkanapi/wrapper/instance.cpp @@ -10,7 +10,7 @@ namespace vulkanapi { #ifdef _USE_GRAPHIC_DEBUG , messageSeverity(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) , messageType(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) - , debugCallback(Instance::DebugCallback) + // , debugCallback(Instance::DebugCallback) #endif { } @@ -56,7 +56,7 @@ namespace vulkanapi { static_cast(extensions.size()), // uint32_t enabledExtensionCount extensions.data() // const char * const * ppEnabledExtensionNames }; - LOG_VULKAN(vkCreateInstance(&instance_create_info, nullptr, &mPtr), "Failed to create instance."); + //LOG_VULKAN(vkCreateInstance(&instance_create_info, nullptr, &mPtr), "Failed to create instance."); //调用vulkan接口,加载实例函数指针 LoadInstanceLevelFunctions(mPtr, extensions);