update log

This commit is contained in:
ouczbs 2024-02-26 08:48:10 +08:00
parent f7a8d64272
commit 68148a475e
6 changed files with 118 additions and 80 deletions

View File

@ -1,12 +1,72 @@
#pragma once
#include <string>
#include <mutex>
namespace zlog
{
static std::mutex _mutex;
static void Message(std::string info, bool logCondition);
static void Message(std::string info);
static void Exception(std::string info, bool logCondition);
static void Exception(std::string info);
}
#include "spdlog/spdlog.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;
}
}
template<typename... TARGS>
void fatalCallback(TARGS&&... args)
{
const std::string format_str = fmt::format(std::forward<TARGS>(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

@ -1,38 +1,31 @@
#include "zlog.
#include <iostream>
std::mutex zlog::_mutex = std::mutex();
void zlog::Message(std::string info, bool logCondition)
#include "zlog.h"
#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
zlog gLog;
zlog::zlog()
{
if (logCondition)
{
std::unique_lock<std::mutex> lock(_mutex);
std::cout << "Message: " + info << std::endl;
}
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%$");
const spdlog::sinks_init_list sink_list = {console_sink};
spdlog::init_thread_pool(8192, 1);
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::register_logger(m_logger);
}
void zlog::Message(std::string info)
zlog::~zlog()
{
std::unique_lock<std::mutex> lock(_mutex);
std::cout << "Message: " + info << std::endl;
}
void zlog::Exception(std::string info, bool logCondition)
{
if (logCondition)
{
std::unique_lock<std::mutex> lock(_mutex);
std::cout << "Exception: " + info << std::endl;
}
}
void zlog::Exception(std::string info)
{
std::unique_lock<std::mutex> lock(_mutex);
std::cout << "Exception: " + info << std::endl;
m_logger->flush();
spdlog::drop_all();
}

View File

@ -2,5 +2,6 @@ set_languages("cxx20")
target("zlog")
set_kind("static")
add_includedirs("include", {public = true})
add_packages("spdlog")
add_files("src/*.cpp")
add_headerfiles("include/*.h")

View File

@ -12,23 +12,9 @@ namespace vulkanapi {
#define _USE_GRAPHIC_DEBUG
#define assert_return(cond, info, result)\
if (cond) {\
zlog::Exception(info);\
return result;\
}
#define assert(cond,info)\
if (cond) {\
zlog::Exception(info);\
}
#define assert_vulkan_return(VkResult, info, result) \
#define LOG_VULKAN(VkResult, info) \
if (VkResult != VK_SUCCESS) { \
zlog::Exception(info);\
return result;\
}
#define assert_vulkan(VkResult, info) \
if (VkResult != VK_SUCCESS) { \
zlog::Exception(info);\
LOG_ERROR(info)\
}
using voidFn = std::function<void()>;

View File

@ -56,7 +56,7 @@ namespace vulkanapi {
static_cast<uint32_t>(extensions.size()), // uint32_t enabledExtensionCount
extensions.data() // const char * const * ppEnabledExtensionNames
};
zlog::Exception("Failed to create instance.", vkCreateInstance(&instance_create_info, nullptr, &mPtr));
LOG_VULKAN(vkCreateInstance(&instance_create_info, nullptr, &mPtr), "Failed to create instance.");
//调用vulkan接口加载实例函数指针
LoadInstanceLevelFunctions(mPtr, extensions);

View File

@ -12,7 +12,7 @@ namespace vulkanapi {
vulkan_library = dlopen("libvulkan.so.1", RTLD_NOW);
#endif
if (vulkan_library == nullptr) {
zlog::Exception("Could not connect with a Vulkan Runtime library.");
LOG_INFO("Could not connect with a Vulkan Runtime library.");
return false;
}
return true;
@ -27,7 +27,7 @@ namespace vulkanapi {
#define EXPORTED_VULKAN_FUNCTION( name ) \
name = (PFN_##name)LoadFunction( vulkan_library, #name ); \
if( name == nullptr ) { \
zlog::Exception( "Could not load exported Vulkan function named:"#name);\
LOG_ERROR( "Could not load exported Vulkan function named:"#name);\
return false; \
}
#include "engine/vulkanapi/vulkan_function_list.inl"
@ -38,7 +38,7 @@ namespace vulkanapi {
#define GLOBAL_LEVEL_VULKAN_FUNCTION( name ) \
name = (PFN_##name)vkGetInstanceProcAddr( nullptr, #name ); \
if( name == nullptr ) { \
zlog::Exception( "Could not load global level Vulkan function named:"#name);\
LOG_ERROR( "Could not load global level Vulkan function named:"#name);\
return false; \
}
@ -52,7 +52,7 @@ namespace vulkanapi {
#define INSTANCE_LEVEL_VULKAN_FUNCTION( name ) \
name = (PFN_##name)vkGetInstanceProcAddr( instance, #name ); \
if( name == nullptr ) { \
zlog::Exception( "Could not load instance-level Vulkan function named:"#name);\
LOG_ERROR( "Could not load instance-level Vulkan function named:"#name);\
return false; \
}
@ -62,7 +62,7 @@ namespace vulkanapi {
if( std::string( enabled_extension ) == std::string( extension ) ) { \
name = (PFN_##name)vkGetInstanceProcAddr( instance, #name ); \
if( name == nullptr ) { \
zlog::Exception( "Could not load instance-level Vulkan function named:"#name);\
LOG_ERROR( "Could not load instance-level Vulkan function named:"#name);\
return false; \
} \
} \
@ -97,7 +97,7 @@ namespace vulkanapi {
result = vkEnumerateInstanceLayerProperties(&extensions_count, nullptr);
if ((result != VK_SUCCESS) ||
(extensions_count == 0)) {
zlog::Exception("Could not get the number of instance layers.");
LOG_ERROR("Could not get the number of instance layers.");
return false;
}
@ -105,7 +105,7 @@ namespace vulkanapi {
result = vkEnumerateInstanceLayerProperties(&extensions_count, available_layers.data());
if ((result != VK_SUCCESS) ||
(extensions_count == 0)) {
zlog::Exception("Could not enumerate instance layers.");
LOG_ERROR("Could not enumerate instance layers.");
return false;
}
@ -118,7 +118,7 @@ namespace vulkanapi {
result = vkEnumerateInstanceExtensionProperties(nullptr, &extensions_count, nullptr);
if ((result != VK_SUCCESS) ||
(extensions_count == 0)) {
zlog::Exception("Could not get the number of instance extensions.");
LOG_ERROR("Could not get the number of instance extensions.");
return false;
}
@ -126,42 +126,40 @@ namespace vulkanapi {
result = vkEnumerateInstanceExtensionProperties(nullptr, &extensions_count, available_extensions.data());
if ((result != VK_SUCCESS) ||
(extensions_count == 0)) {
zlog::Exception("Could not enumerate instance extensions.");
LOG_ERROR("Could not enumerate instance extensions.");
return false;
}
return true;
}
std::vector<char const*> _EnabledExtensionNames(std::vector<char const*>& extensions) {
std::vector<char const*> _EnabledExtensionNames(std::vector<std::string>& extensions) {
std::vector<VkExtensionProperties> available_extensions;
std::vector<char const*> _extension;
if (!CheckAvailableInstanceExtensions(available_extensions)) {
return _extension;
}
for (int i = 0, l = extensions.size(); i < l; i++) {
if (IsExtensionSupported(available_extensions, extensions[i])) {
_extension.push_back(extensions[i]);
if (IsExtensionSupported(available_extensions, extensions[i].c_str())) {
_extension.push_back(extensions[i].c_str());
}
else {
zlog::Exception("cann't support extension: ");
std::cout << "cann't support extension: " << extensions[i] << std::endl;
LOG_ERROR("cann't support extension: {}", extensions[i].c_str());
}
}
return _extension;
}
std::vector<char const*> _EnabledLayerNames(std::vector<char const*>& layers) {
std::vector<char const*> _EnabledLayerNames(std::vector<std::string>& layers) {
std::vector<VkLayerProperties> available_layers;
std::vector<char const*> _layers;
if (!CheckAvailableInstanceLayers(available_layers)) {
return _layers;
}
for (int i = 0, l = layers.size(); i < l; i++) {
if (IsLayerSupported(available_layers, layers[i])) {
_layers.push_back(layers[i]);
if (IsLayerSupported(available_layers, layers[i].c_str())) {
_layers.push_back(layers[i].c_str());
}
else {
zlog::Exception("Could not load instance-level Vulkan function named:"#name); \
std::cout << "cann't support layer: " << layers[i] << std::endl;
LOG_ERROR("Could not load instance-level Vulkan function named: {}", layers[i].c_str());
}
}
return _layers;