Logging
一个完整的引擎自然需要一个日志系统来进行各种状态的显示,便于我们查看程序的运行状态和 debug,在这里主要使用的是 github 上面的开源项目 spdlog
-
添加 spdlog 根目录终端输入
git submodule add https://github.com/gabime/spdlog Hazel/vendor/spdlog
- 包含 spdlog 目录-在 Hazel 和 SandBox 属性页,所有配置,常规,附加包含目录 $(Solution)Hazel\vendor\spdlog\include;
-
创建 Log 类
//Log.h
#pragma once
#include <memory>
#include "Core.h"
#include "spdlog/spdlog.h"
namespace Hazel {
class HAZEL_API Log
{
public:
static void Init();
inline static std::shared_ptr<spdlog::logger>& GetCoreLogger() { return s_CoreLogger; }
inline static std::shared_ptr<spdlog::logger>& GetClientLogger() { return s_ClientLogger; }
private:
static std::shared_ptr<spdlog::logger> s_CoreLogger; //log for engine
static std::shared_ptr<spdlog::logger> s_ClientLogger; //log for client
};
}
// Log.cpp
#include "Log.h"
namespace Hazel {
std::shared_ptr<spdlog::logger> Log::s_CoreLogger; //这里没加Log:: 类声明导致LNK2001 报错!!
std::shared_ptr<spdlog::logger> Log::s_ClientLogger;
void Log::Init() {
spdlog::set_pattern("%^[%T] %n: %v%$"); //time
s_CoreLogger = spdlog::stdout_color_mt("HAZEL");
s_CoreLogger->set_level(spdlog::level::trace);
s_ClientLogger = spdlog::stdout_color_mt("APP");
s_ClientLogger->set_level(spdlog::level::trace);
}
}
//Core log macros
#define HZ_CORE_TRACE(...) ::Hazel::Log::GetCoreLogger()->trace(__VA_ARGS__)
#define HZ_CORE_WARN(...) ::Hazel::Log::GetCoreLogger()->warn(__VA_ARGS__)
#define HZ_CORE_INFO(...) ::Hazel::Log::GetCoreLogger()->info(__VA_ARGS__)
#define HZ_CORE_ERROR(...) ::Hazel::Log::GetCoreLogger()->error(__VA_ARGS__)
#define HZ_CORE_FATAL(...) ::Hazel::Log::GetCoreLogger()->fatal(__VA_ARGS__)
//Client log macros
#define HZ_TRACE(...) ::Hazel::Log::GetClientLogger()->trace(__VA_ARGS__)
#define HZ_WARN(...) ::Hazel::Log::GetClientLogger()->warn(__VA_ARGS__)
#define HZ_INFO(...) ::Hazel::Log::GetClientLogger()->info(__VA_ARGS__)
#define HZ_ERROR(...) ::Hazel::Log::GetClientLogger()->error(__VA_ARGS__)
#define HZ_FATAL(...) ::Hazel::Log::GetClientLogger()->fatal(__VA_ARGS__)
- main 函数中调用
int main(int argc, char** argv)
{
Hazel::Log::Init();
HZ_CORE_WARN("Initialized Log");
int a = 5;
HZ_INFO("Hello! Var={0}", a);
auto app = Hazel::CreateApplication();
app->Run();
delete app;
}
- 运行效果: