add zlog
This commit is contained in:
parent
1d96f31f52
commit
51f009c05e
1
engine/3rdparty/xmake.lua
vendored
Normal file
1
engine/3rdparty/xmake.lua
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
add_requires("spdlog")
|
||||||
88
engine/modules/engine/core/include/3rdparty/source_location.h
vendored
Normal file
88
engine/modules/engine/core/include/3rdparty/source_location.h
vendored
Normal 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
|
||||||
7924
engine/modules/engine/core/include/3rdparty/yyjson.h
vendored
Normal file
7924
engine/modules/engine/core/include/3rdparty/yyjson.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
47
engine/modules/engine/core/include/3rdparty/zlog.h
vendored
Normal file
47
engine/modules/engine/core/include/3rdparty/zlog.h
vendored
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
#include "source_location.h"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <stdexcept>
|
||||||
|
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)...);
|
||||||
|
}
|
||||||
|
void flush() {
|
||||||
|
m_logger->flush();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
void flush();
|
||||||
|
};
|
||||||
14
engine/modules/engine/core/include/archive/json/serde.h
Normal file
14
engine/modules/engine/core/include/archive/json/serde.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "serialize.h"
|
||||||
|
namespace api {
|
||||||
|
struct JsonSerialize {
|
||||||
|
|
||||||
|
};
|
||||||
|
template<typename T>
|
||||||
|
struct JsonSerde {
|
||||||
|
inline static bool read(yyjson_val* val, T& v)
|
||||||
|
{
|
||||||
|
return r.read(v);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
85
engine/modules/engine/core/include/archive/json/serialize.h
Normal file
85
engine/modules/engine/core/include/archive/json/serialize.h
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "zlog.h"
|
||||||
|
#include "yyjson.h"
|
||||||
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
namespace api {
|
||||||
|
using std::string_view;
|
||||||
|
struct JsonReader {
|
||||||
|
struct Level {
|
||||||
|
enum EType : uint32_t {
|
||||||
|
EObjectIndex = -1,
|
||||||
|
EArrayIndex = 0,
|
||||||
|
};
|
||||||
|
yyjson_val* val = nullptr;
|
||||||
|
uint32_t index = EObjectIndex;
|
||||||
|
constexpr Level(yyjson_val* val, EType type = EObjectIndex) noexcept
|
||||||
|
: val(val),index(type){}
|
||||||
|
constexpr bool is_object()const {
|
||||||
|
return index == EObjectIndex;
|
||||||
|
}
|
||||||
|
constexpr bool is_array()const {
|
||||||
|
return index != EObjectIndex;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
yyjson_doc* doc;
|
||||||
|
std::vector<Level> stack;
|
||||||
|
~JsonReader() {
|
||||||
|
if (doc) {
|
||||||
|
yyjson_doc_free(doc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JsonReader(string_view json) {
|
||||||
|
yyjson_read_err err = {};
|
||||||
|
doc = yyjson_read_opts((char*)json.data(), json.size(), 0, nullptr, &err);
|
||||||
|
if (doc == nullptr) {
|
||||||
|
zlog::error("Failed to parse JSON: {}, error: {}", json.data(), err.msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
yyjson_val* find(string_view name) {
|
||||||
|
auto back = stack.back();
|
||||||
|
if (back.is_object()) {
|
||||||
|
return yyjson_obj_get(back.val, name.data());
|
||||||
|
}
|
||||||
|
return yyjson_arr_get(back.val, back.index++);
|
||||||
|
}
|
||||||
|
bool start_object(string_view name = "") {
|
||||||
|
yyjson_val* root = stack.empty() ? yyjson_doc_get_root(doc) : find(name);
|
||||||
|
stack.emplace_back(root, Level::EObjectIndex);
|
||||||
|
}
|
||||||
|
void end_object() {
|
||||||
|
stack.pop_back();
|
||||||
|
}
|
||||||
|
bool start_array(string_view name = "") {
|
||||||
|
yyjson_val* root = stack.empty() ? yyjson_doc_get_root(doc) : find(name);
|
||||||
|
stack.emplace_back(root, Level::EArrayIndex);
|
||||||
|
}
|
||||||
|
void end_array() {
|
||||||
|
stack.pop_back();
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
bool read(T& v, const string_view& name = "") {
|
||||||
|
template<typename T>
|
||||||
|
concept is_string_v = requires(T t) {
|
||||||
|
{ static_cast<std::string>(t) } -> std::convertible_to<std::string>;
|
||||||
|
};
|
||||||
|
yyjson_val* parent = find(name);
|
||||||
|
if constexpr (std::is_same_v<T, bool>) {
|
||||||
|
v = (T)yyjson_get_bool(parent);
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_integral_v<T>) {
|
||||||
|
v = (T)yyjson_get_uint(parent);
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_floating_point_v<T>) {
|
||||||
|
v = (T)yyjson_get_real(parent);
|
||||||
|
}
|
||||||
|
else if constexpr (is_string_v<T>){
|
||||||
|
v = yyjson_get_str(parent);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
9473
engine/modules/engine/core/src/3rdparty/yyjson.cpp
vendored
Normal file
9473
engine/modules/engine/core/src/3rdparty/yyjson.cpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
39
engine/modules/engine/core/src/3rdparty/zlog.cpp
vendored
Normal file
39
engine/modules/engine/core/src/3rdparty/zlog.cpp
vendored
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#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>
|
||||||
|
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%$");
|
||||||
|
|
||||||
|
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/zengine.log", true);
|
||||||
|
file_sink->set_level(spdlog::level::trace);
|
||||||
|
//file_sink->set_pattern("[%Y-%m-%d %H:%M:%S][%s:%#] %-8l %^%v%$");
|
||||||
|
|
||||||
|
const spdlog::sinks_init_list sink_list = { console_sink, file_sink };
|
||||||
|
|
||||||
|
spdlog::init_thread_pool(8192, 1);
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
void flush(){
|
||||||
|
zlog.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
static_component("core","engine")
|
static_component("core","engine")
|
||||||
add_includedirs("include", {public = true})
|
add_includedirs("include", "include/3rdparty", {public = true})
|
||||||
add_headerfiles("include/**.h","include/**.inl")
|
add_headerfiles("include/**.h","include/**.inl")
|
||||||
add_files("src/**.cpp")
|
add_files("src/**.cpp")
|
||||||
add_deps("zlib", {public = true})
|
add_deps("zlib", {public = true})
|
||||||
|
add_packages("spdlog", {public = true})
|
||||||
--add_syslinks("Kernel32")
|
--add_syslinks("Kernel32")
|
||||||
@ -6,6 +6,7 @@
|
|||||||
#include <stack>
|
#include <stack>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include "yyjson.h"
|
||||||
namespace pmr {
|
namespace pmr {
|
||||||
using std::pmr::monotonic_buffer_resource;
|
using std::pmr::monotonic_buffer_resource;
|
||||||
using std::pmr::vector;
|
using std::pmr::vector;
|
||||||
@ -134,7 +135,12 @@ void genLua(const char* file_path, const pmr::vector<MacroData>& mdList) {
|
|||||||
writeFile(file_path, oss.str());
|
writeFile(file_path, oss.str());
|
||||||
}
|
}
|
||||||
void genPlugin(const char* file_path, const pmr::vector<MacroData>& mdList) {
|
void genPlugin(const char* file_path, const pmr::vector<MacroData>& mdList) {
|
||||||
|
yyjson_read_err err = {};
|
||||||
|
std::pmr::string json{"{a=1}"};
|
||||||
|
yyjson_doc* _document = yyjson_read_opts(
|
||||||
|
(char*)json.data(), json.size(),
|
||||||
|
0, nullptr, &err);
|
||||||
|
auto obj = yyjson_doc_get_root((yyjson_doc*)_document);
|
||||||
}
|
}
|
||||||
int main() {
|
int main() {
|
||||||
const char* file_path = R"(F:\engine\zengine\engine\modules\render\vulkan\include\vulkan\module.h)";
|
const char* file_path = R"(F:\engine\zengine\engine\modules\render\vulkan\include\vulkan\module.h)";
|
||||||
|
|||||||
@ -1,2 +1,3 @@
|
|||||||
tool_target("make_plugin")
|
tool_target("make_plugin")
|
||||||
add_files("src/main.cpp")
|
add_files("src/main.cpp")
|
||||||
|
add_deps("core")
|
||||||
@ -5,5 +5,6 @@ target("engine")
|
|||||||
set_kind("static")
|
set_kind("static")
|
||||||
set_group("Engine")
|
set_group("Engine")
|
||||||
includes("xmake/xmake.lua")
|
includes("xmake/xmake.lua")
|
||||||
|
includes("3rdparty/xmake.lua")
|
||||||
includes("tools/xmake.lua")
|
includes("tools/xmake.lua")
|
||||||
includes("modules/xmake.lua")
|
includes("modules/xmake.lua")
|
||||||
@ -6,7 +6,7 @@ function cmd_compile(target, genfile, file)
|
|||||||
{"asset", { public = true}},
|
{"asset", { public = true}},
|
||||||
}
|
}
|
||||||
]]
|
]]
|
||||||
--io.writefile(genfile, res)
|
io.writefile(genfile, res)
|
||||||
local dependency = io.load(genfile)
|
local dependency = io.load(genfile)
|
||||||
for k,v in ipairs(dependency) do
|
for k,v in ipairs(dependency) do
|
||||||
--target:add("deps", v[1], v[2])
|
--target:add("deps", v[1], v[2])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user