Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Prettier log output #221

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions engine/PilotEditor.ini

This file was deleted.

11 changes: 11 additions & 0 deletions engine/PilotEditor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"asset_folder": "asset",
"schema_folder": "schema",
"big_icon": "resource/PilotEditorBigIcon.png",
"small_icon": "resource/PilotEditorSmallIcon.png",
"font_file": "resource/PilotEditorFont.TTF",
"default_world_url": "asset/world/hello.world.json",
"global_rendering_res": "asset/global/rendering.global.json",
"jolt_asset_folder": "jolt-asset",
"log_pattern": "[%^%l%$] %!@%s+%# %v"
}
4 changes: 2 additions & 2 deletions engine/source/editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ target_link_libraries(${TARGET_NAME} PilotRuntime)
if(NOT ENABLE_PHYSICS_DEBUG_RENDERER)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy "${ENGINE_ROOT_DIR}/${TARGET_NAME}.ini" "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy "${ENGINE_ROOT_DIR}/${TARGET_NAME}.json" "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/resource" "${BINARY_ROOT_DIR}/resource"
COMMAND ${CMAKE_COMMAND} -E copy_directory "$<TARGET_FILE_DIR:${TARGET_NAME}>/" "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E remove_directory "${BINARY_ROOT_DIR}/${ENGINE_ASSET_DIR}"
Expand All @@ -34,7 +34,7 @@ if(NOT ENABLE_PHYSICS_DEBUG_RENDERER)
else()
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy "${ENGINE_ROOT_DIR}/${TARGET_NAME}.ini" "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy "${ENGINE_ROOT_DIR}/${TARGET_NAME}.json" "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/resource" "${BINARY_ROOT_DIR}/resource"
COMMAND ${CMAKE_COMMAND} -E copy_directory "$<TARGET_FILE_DIR:${TARGET_NAME}>/" "${BINARY_ROOT_DIR}"
COMMAND ${CMAKE_COMMAND} -E remove_directory "${BINARY_ROOT_DIR}/${ENGINE_ASSET_DIR}"
Expand Down
2 changes: 1 addition & 1 deletion engine/source/editor/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char** argv)

Pilot::EngineInitParams params;
params.m_root_folder = pilot_root_folder;
params.m_config_file_path = pilot_root_folder / "PilotEditor.ini";
params.m_config_file_path = pilot_root_folder / "PilotEditor.json";

Pilot::PilotEngine* engine = new Pilot::PilotEngine();

Expand Down
5 changes: 3 additions & 2 deletions engine/source/runtime/core/base/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include <chrono>
#include <thread>

#define LOG_HELPER(LOG_LEVEL, ...) \
g_runtime_global_context.m_logger_system->log(LOG_LEVEL, "[" + std::string(__FUNCTION__) + "] " + __VA_ARGS__);
#define LOG_SRC spdlog::source_loc(Pilot::LogSystem::FileName(__FILE__), __LINE__, __FUNCTION__)

#define LOG_HELPER(LOG_LEVEL, ...) g_runtime_global_context.m_logger_system->log(LOG_LEVEL, LOG_SRC, __VA_ARGS__);

#define LOG_DEBUG(...) LOG_HELPER(LogSystem::LogLevel::debug, __VA_ARGS__);

Expand Down
4 changes: 2 additions & 2 deletions engine/source/runtime/core/log/log_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace Pilot
{
LogSystem::LogSystem()
LogSystem::LogSystem(const std::string& log_pattern)
{
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::trace);
console_sink->set_pattern("[%^%l%$] %v");
console_sink->set_pattern(log_pattern);

const spdlog::sinks_init_list sink_list = {console_sink};

Expand Down
36 changes: 27 additions & 9 deletions engine/source/runtime/core/log/log_system.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <spdlog/spdlog.h>

#include <cstdint>
#include <filesystem>
#include <spdlog/spdlog.h>
#include <stdexcept>

namespace Pilot
Expand All @@ -20,29 +20,47 @@ namespace Pilot
fatal
};

static constexpr const char* FileName(const char* path)
Copy link
Contributor

@ShenMian ShenMian Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 该方法相比于 C++ 更适合 C, C 使用 strrchr(), C++ 可以考虑使用 std::string_view.
  2. 这种判断方法不适用于处理 Windows 的路径, 比如在 MSVC 中宏 __FILE__ 的值.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 这里其实用到了c++14 起的一个特性,constexpr 函数的编译期计算,可以看这里 https://godbolt.org/z/jjYqfjz7s , O1级别以上的优化都会在编译期算出表达式,也就是执行期不会call function
    image
  2. 没有理解
  3. 确实,只做了简单的测试,用 std::filesystem::path::preferred_separator替代即可

Copy link
Contributor

@ShenMian ShenMian Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没注意到 constexpr, 抱歉. strrchr() 确实不支持, 不过 std::string_view 需要使用到的成员函数应该支持.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static constexpr std::string_view FileName(std::string_view path)
{
    return path.substr(path.find_last_of(std::filesystem::path::preferred_separator) + 1);
}

仅供参考.

{
const char* file = path;
while (*path)
{
if (*path++ == std::filesystem::path::preferred_separator)
{
file = path;
}
}
return file;
}

public:
LogSystem();
LogSystem(const std::string& log_pattern);
~LogSystem();

template<typename... TARGS>
void log(LogLevel level, TARGS&&... args)
void log(LogLevel level, spdlog::source_loc&& loc, TARGS&&... args)
{
switch (level)
{
case LogLevel::debug:
m_logger->debug(std::forward<TARGS>(args)...);
m_logger->log(
std::forward<spdlog::source_loc>(loc), spdlog::level::debug, std::forward<TARGS>(args)...);
break;
case LogLevel::info:
m_logger->info(std::forward<TARGS>(args)...);
m_logger->log(
std::forward<spdlog::source_loc>(loc), spdlog::level::info, std::forward<TARGS>(args)...);
break;
case LogLevel::warn:
m_logger->warn(std::forward<TARGS>(args)...);
m_logger->log(
std::forward<spdlog::source_loc>(loc), spdlog::level::warn, std::forward<TARGS>(args)...);
break;
case LogLevel::error:
m_logger->error(std::forward<TARGS>(args)...);
m_logger->log(
std::forward<spdlog::source_loc>(loc), spdlog::level::err, std::forward<TARGS>(args)...);
break;
case LogLevel::fatal:
m_logger->critical(std::forward<TARGS>(args)...);
m_logger->log(
std::forward<spdlog::source_loc>(loc), spdlog::level::critical, std::forward<TARGS>(args)...);
fatalCallback(std::forward<TARGS>(args)...);
break;
default:
Expand Down
5 changes: 2 additions & 3 deletions engine/source/runtime/function/global/global_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "runtime/engine.h"
#include "runtime/function/framework/world/world_manager.h"
#include "runtime/function/input/input_system.h"
#include "runtime/function/physics/physics_system.h"
#include "runtime/function/physics/physics_manager.h"
#include "runtime/function/physics/physics_system.h"
#include "runtime/function/render/render_system.h"
#include "runtime/function/render/window_system.h"

Expand All @@ -28,7 +28,7 @@ namespace Pilot

m_file_system = std::make_shared<FileSystem>();

m_logger_system = std::make_shared<LogSystem>();
m_logger_system = std::make_shared<LogSystem>(m_config_manager->getLogPattern());

m_asset_manager = std::make_shared<AssetManager>();

Expand Down Expand Up @@ -72,7 +72,6 @@ namespace Pilot

m_asset_manager.reset();


m_logger_system.reset();

m_file_system.reset();
Expand Down
101 changes: 54 additions & 47 deletions engine/source/runtime/resource/config_manager/config_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
#include "runtime/resource/config_manager/config_manager.h"

#include "runtime/engine.h"

#include <fstream>
#include <sstream>
#include <string>

#include "_generated/serializer/all_serializer.h"
#include "runtime/core/base/macro.h"
#include "runtime/engine.h"

namespace Pilot
{
void ConfigManager::initialize(const EngineInitParams& init_param)
{
m_root_folder = init_param.m_root_folder;
// read configs
std::ifstream config_file(init_param.m_config_file_path);
std::string config_line;
while (std::getline(config_file, config_line))
std::ifstream config_file(init_param.m_config_file_path);
std::stringstream buffer;
buffer << config_file.rdbuf();
std::string config_file_text(buffer.str());

std::string error;
auto config_json = PJson::parse(config_file_text, error);
ASSERT(error.empty());

PSerializer::read(config_json, m_config);

if (!m_config.asset_folder.empty())
{
m_asset_folder = m_root_folder / m_config.asset_folder;
}
if (!m_config.schema_folder.empty())
{
m_schema_folder = m_root_folder / m_config.schema_folder;
}
if (!m_config.big_icon.empty())
{
m_editor_big_icon_path = m_root_folder / m_config.big_icon;
}
if (!m_config.small_icon.empty())
{
size_t seperate_pos = config_line.find_first_of('=');
if (seperate_pos > 0 && seperate_pos < (config_line.length() - 1))
{
std::string name = config_line.substr(0, seperate_pos);
std::string value = config_line.substr(seperate_pos + 1, config_line.length() - seperate_pos - 1);
if (name == "AssetFolder")
{
m_asset_folder = m_root_folder / value;
}
else if (name == "SchemaFolder")
{
m_schema_folder = m_root_folder / value;
}
else if (name == "DefaultWorld")
{
m_default_world_url = value;
}
else if (name == "BigIconFile")
{
m_editor_big_icon_path = m_root_folder / value;
}
else if (name == "SmallIconFile")
{
m_editor_small_icon_path = m_root_folder / value;
}
else if (name == "FontFile")
{
m_editor_font_path = m_root_folder / value;
}
else if (name == "GlobalRenderingRes")
{
m_global_rendering_res_url = value;
}
m_editor_small_icon_path = m_root_folder / m_config.small_icon;
}
if (!m_config.font_file.empty())
{
m_editor_font_path = m_root_folder / m_config.font_file;
}

// set default value
if (m_config.log_pattern.empty())
{
m_config.log_pattern = "[%^%l%$] %v";
}

#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
else if (name == "JoltAssetFolder")
{
m_jolt_physics_asset_folder = m_root_folder / value;
}
#endif
}
if (!m_config.jolt_asset_folder.empty())
{
m_jolt_physics_asset_folder = m_root_folder / jolt_asset_folder;
}
#endif
}

const std::filesystem::path& ConfigManager::getRootFolder() const { return m_root_folder; }
Expand All @@ -70,12 +72,17 @@ namespace Pilot

const std::filesystem::path& ConfigManager::getEditorFontPath() const { return m_editor_font_path; }

const std::string& ConfigManager::getDefaultWorldUrl() const { return m_default_world_url; }
const std::string& ConfigManager::getDefaultWorldUrl() const { return m_config.default_world_url; }

const std::string& ConfigManager::getGlobalRenderingResUrl() const { return m_global_rendering_res_url; }
const std::string& ConfigManager::getGlobalRenderingResUrl() const { return m_config.global_rendering_res; }

const std::string& ConfigManager::getLogPattern() const { return m_config.log_pattern; }

#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
const std::filesystem::path& ConfigManager::getJoltPhysicsAssetFolder() const { return m_jolt_physics_asset_folder; }
const std::filesystem::path& ConfigManager::getJoltPhysicsAssetFolder() const
{
return m_jolt_physics_asset_folder;
}
#endif

} // namespace Pilot
26 changes: 24 additions & 2 deletions engine/source/runtime/resource/config_manager/config_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,32 @@

#include <filesystem>

#include "runtime/core/meta/reflection/reflection.h"

namespace Pilot
{
struct EngineInitParams;

REFLECTION_TYPE(Config)
CLASS(Config, Fields)
{
REFLECTION_BODY(Config);

public:
std::string asset_folder;
std::string schema_folder;
std::string default_world_url;
std::string big_icon;
std::string small_icon;
std::string font_file;
std::string global_rendering_res;
std::string log_pattern;

#ifdef ENABLE_PHYSICS_DEBUG_RENDERER
std::string jolt_asset_folder;
#endif
};

class ConfigManager
{
public:
Expand All @@ -24,6 +46,7 @@ namespace Pilot

const std::string& getDefaultWorldUrl() const;
const std::string& getGlobalRenderingResUrl() const;
const std::string& getLogPattern() const;

private:
std::filesystem::path m_root_folder;
Expand All @@ -37,7 +60,6 @@ namespace Pilot
std::filesystem::path m_jolt_physics_asset_folder;
#endif

std::string m_default_world_url;
std::string m_global_rendering_res_url;
Config m_config;
};
} // namespace Pilot