Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
2 changes: 1 addition & 1 deletion engine/source/runtime/core/log/log_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Pilot
{
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("[%^%l%$] %!@%s+%# %v");

const spdlog::sinks_init_list sink_list = {console_sink};

Expand Down
30 changes: 24 additions & 6 deletions engine/source/runtime/core/log/log_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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++ == '/')
{
file = path;
}
}
return file;
}

public:
LogSystem();
~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