This commit is contained in:
ouczbs 2024-02-28 01:32:58 +08:00
parent 68148a475e
commit e27b1aeda9
9 changed files with 172 additions and 93 deletions

View File

@ -0,0 +1,88 @@
#ifndef NOSTD_SOURCE_LOCATION_HPP
#define NOSTD_SOURCE_LOCATION_HPP
#pragma once
#include <cstdint>
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<std::int32_t>(location.line()),
location.function_name()};
}
struct format_with_location {
std::string_view value;
spdlog::source_loc loc;
template <typename String>
format_with_location(const String& s, const source_location& location =
source_location::current())
: value{ s }, loc{ get_log_source_location(location) } {}
};
} // namespace nostd
#endif

View File

@ -1,72 +1,43 @@
#pragma once
#include "spdlog/spdlog.h"
#include "source_location.h"
#include <cstdint>
#include <stdexcept>
class zlog final
{
public:
enum class LogLevel : uint8_t
{
debug,
info,
warn,
error,
fatal
};
public:
zlog();
~zlog();
template<typename... TARGS>
void log(LogLevel level, TARGS&&... args)
{
switch (level)
{
case LogLevel::debug:
m_logger->debug(std::forward<TARGS>(args)...);
break;
case LogLevel::info:
m_logger->info(std::forward<TARGS>(args)...);
break;
case LogLevel::warn:
m_logger->warn(std::forward<TARGS>(args)...);
break;
case LogLevel::error:
m_logger->error(std::forward<TARGS>(args)...);
break;
case LogLevel::fatal:
m_logger->critical(std::forward<TARGS>(args)...);
fatalCallback(std::forward<TARGS>(args)...);
break;
default:
break;
namespace zlog {
using level_enum = spdlog::level::level_enum;
class zloger {
private:
std::shared_ptr<spdlog::logger> m_logger;
public:
zloger();
~zloger();
template <typename... Args>
void log(level_enum level, format_with_location& fmt, Args &&...args) {
m_logger->log(fmt.loc, level, fmt::runtime(fmt.value), std::forward<Args>(args)...);
}
}
template<typename... TARGS>
void fatalCallback(TARGS&&... args)
{
const std::string format_str = fmt::format(std::forward<TARGS>(args)...);
};
extern zloger zlog;
template <typename... Args>
void info(format_with_location fmt, Args &&...args) {
zlog.log(level_enum::info, fmt, std::forward<Args>(args)...);
};
template <typename... Args>
void debug(format_with_location fmt, Args &&...args) {
zlog.log(level_enum::debug, fmt, std::forward<Args>(args)...);
};
template <typename... Args>
void warn(format_with_location fmt, Args &&...args) {
zlog.log(level_enum::warn, fmt, std::forward<Args>(args)...);
};
template <typename... Args>
void error(format_with_location fmt, Args &&...args) {
zlog.log(level_enum::err, fmt, std::forward<Args>(args)...);
};
template <typename... Args>
void fatal(format_with_location fmt, Args &&...args) {
zlog.log(level_enum::critical, fmt, std::forward<Args>(args)...);
const std::string format_str = fmt::format(std::forward<Args>(args)...);
throw std::runtime_error(format_str);
}
private:
std::shared_ptr<spdlog::logger> 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__);
};
};

View File

@ -3,29 +3,34 @@
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
zlog gLog;
zlog::zlog()
{
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
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<spdlog::sinks::stdout_color_sink_mt>();
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<spdlog::sinks::basic_file_sink_mt>("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<spdlog::async_logger>("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<spdlog::async_logger>("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();
}

8
engine/3rdparty/zlog/test/01file.cpp vendored Normal file
View File

@ -0,0 +1,8 @@
#include <iostream>
#include "zlog.h"
int main() {
zlog::info("hello info");
zlog::warn("hello {}", "warn");
zlog::error("hello {}", "error");
return 0;
}

View File

@ -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")
add_headerfiles("include/*.h")
target("zlog_test01_file")
set_languages("cxx20")
set_kind("binary")
add_deps("zlog")
add_files("test/01file.cpp")

View File

@ -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<VkPhysicalDevice> available_devices;
mInstance->EnumerateAvailablePhysicalDevices(available_devices);
int device_count = available_devices.size();

View File

@ -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<void()>;

View File

@ -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<uint32_t>(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);