Hazel Engine learning 2-- Logging

youtube视频:https://www.youtube.com/watch?v=dZr-53LAlOw&list=PLlrATfBNZ98dC-V-N3m0Go4deliWHPFwT&index=6

完整代码:https://github.com/DXT00/Hazel_study/tree/master/Logging

使用第三方库 gabime/spdlog :https://github.com/gabime/spdlog

作为子模块clone到Hazel/vendor/spdlog

 

给sendBox和Hazel添加spdlog目录

编写Log类:

Log.h

#pragma once

#include "Core.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <memory>
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; //使用 shared_ptr 记得 include<memory>
		static std::shared_ptr<spdlog::logger> s_ClientLogger;

		

	};

}

Log.cpp

#include "Log.h"


namespace Hazel {

	std::shared_ptr<spdlog::logger> Log::s_CoreLogger; //static 对象需要在类外初始化
	std::shared_ptr<spdlog::logger> Log::s_ClientLogger;

	void Log::Init() {

		spdlog::set_pattern("%^[%T] %n: %v%$");
		s_CoreLogger = spdlog::stdout_color_mt("HAZEL");
		s_CoreLogger->set_level(spdlog::level::trace);
		s_ClientLogger = spdlog::stderr_color_mt("APP");
		s_ClientLogger->set_level(spdlog::level::trace);

	}	

}

EntryPoint.h

#pragma once


#ifdef HZ_PLATFORM_WINDOWS

extern Hazel::Application* Hazel::CreateApplication();//由client生成

int main(int argc,char **argv) {
	
	Hazel::Log::Init();
	Hazel::Log::GetCoreLogger()->warn("Initialized Log!");
	Hazel::Log::GetClientLogger()->info("Hello");

	auto app = Hazel::CreateApplication();
	app->Run();
	delete app;
	return 0;
}



#endif // HZ_PLATFORM_WINDOWS

不要忘了在Hazel.h里添加 Log.h,并把Hazel.dll复制给Sandbox 

#pragma once

//For use by HAZEL Application
#include "Hazel/Application.h"
#include"Hazel/Log.h"
//-----------------Entry Point---------------
#include "Hazel/EntryPoint.h"

F5运行:

把这些接口改成宏:

Hazel::Log::GetCoreLogger()->warn("Initialized Log!");
Hazel::Log::GetClientLogger()->info("Hello");

 Log.h

#pragma once

#include "Core.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <memory>
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; //使用 shared_ptr 记得 include<memory>
		static std::shared_ptr<spdlog::logger> s_ClientLogger;

		

	};

}
//Core log macros
#define HZ_CORE_TRACE(...) ::Hazel::Log::GetCoreLogger()->trace(__VA_ARGS__)
#define HZ_CORE_INFO(...)  ::Hazel::Log::GetCoreLogger()->info(__VA_ARGS__)
#define HZ_CORE_WARN(...)  ::Hazel::Log::GetCoreLogger()->warn(__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_INFO(...)	   ::Hazel::Log::GetClientLogger()->info(__VA_ARGS__)
#define HZ_WARN(...)	   ::Hazel::Log::GetClientLogger()->warn(__VA_ARGS__)
#define HZ_ERROR(...)	   ::Hazel::Log::GetClientLogger()->error(__VA_ARGS__)
#define HZ_FATAL(...)	   ::Hazel::Log::GetClientLogger()->fatal(__VA_ARGS__)

(__VA_ARGS__)可变参数

EntryPoint.h

#pragma once


#ifdef HZ_PLATFORM_WINDOWS

extern Hazel::Application* Hazel::CreateApplication();//由client生成

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;
	return 0;
}

#endif // HZ_PLATFORM_WINDOWS

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值