跟着cherno手搓游戏引擎【2】:日志系统spdlog和premake的使用

配置:

日志库文件github:

GitHub - gabime/spdlog: Fast C++ logging library.

新建vendor文件夹

将下载好的spdlog放入

配置YOTOEngine的附加包含目录:

配置Sandbox的附加包含目录:

包装spdlog:

在YOTO文件夹下创建Log.cpp和log.h

log.h:

cpp 复制代码
#pragma once
#include"Core.h"
#include<spdlog/spdlog.h>
#include "spdlog/sinks/stdout_color_sinks.h" 
namespace YOTO {
	class YOTO_API Log
	{
	public:
		static void Init();
		//inline是为了提高性能,相当于直接把函数里的代码段放在那里
		//返回的是Logger,分为服务器logger和核心logger
		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;
		static std::shared_ptr<spdlog::logger>  s_ClientLogger;
	};
}
//Core 的log 的简化
#define YT_CORE_ERROR(...)		::YOTO::Log::GetCoreLogger()->error(__VA_ARGS__)
#define YT_CORE_WARN(...)		::YOTO::Log::GetCoreLogger()->warn(__VA_ARGS__)
#define YT_CORE_INFO(...)		::YOTO::Log::GetCoreLogger()->info(__VA_ARGS__)
#define YT_CORE_TRACE(...)		::YOTO::Log::GetCoreLogger()->trace(__VA_ARGS__)
#define YT_CORE_FATAL(...)		::YOTO::Log::GetCoreLogger()->fatal(__VA_ARGS__)
//client 的log的简化
#define YT_CLIENT_ERROR(...)	::YOTO::Log::GetClientLogger()->error(__VA_ARGS__)
#define YT_CLIENT_WARN(...)		::YOTO::Log::GetClientLogger()->warn(__VA_ARGS__)
#define YT_CLIENT_INFO(...)		::YOTO::Log::GetClientLogger()->info(__VA_ARGS__)
#define YT_CLIENT_TRACE(...)	::YOTO::Log::GetClientLogger()->trace(__VA_ARGS__)
#define YT_CLIENT_FATAL(...)	::YOTO::Log::GetClientLogger()->fatal(__VA_ARGS__)

log.cpp:

cpp 复制代码
#include "Log.h"

namespace YOTO {
	std::shared_ptr<spdlog::logger> Log::s_CoreLogger;
	std::shared_ptr<spdlog::logger>  Log::s_ClientLogger;
	void  Log::Init() {
		//设置日志格式
		spdlog::set_pattern("%^[%T] %n: %v%$");
		//创建多线程logger,核心logger为YOTO
		s_CoreLogger = spdlog::stdout_color_mt("YOTO");
		//设置打印消息的级别,trace是打印所有东西(筛选器)
		s_CoreLogger->set_level(spdlog::level::level_enum::trace);
		//客户端的为APP
		s_ClientLogger= spdlog::stdout_color_mt("APP");
		s_ClientLogger->set_level(spdlog::level::level_enum::trace);
	}
}

测试:

在YOTO.h中加入#include "YOTO/Log.h",记得重新生成,把dll加入到Sandbox(后续会使用premake简化该流程,这里暂时手动生成)

我们在入口点修改代码测试:

cpp 复制代码
#pragma once

#ifdef YT_PLATFORM_WINDOWS
#include "../YOTO.h"

extern YOTO::Application* YOTO::CreateApplication();
void main(int argc,char** argv) {
	YOTO::Log::Init();
	YT_CORE_ERROR("测试警告信息");
	int test = 1;
	YT_CLIENT_INFO("测试info:test={0}",test);
	auto app = YOTO::CreateApplication();
	app->Run();
	delete app;
}
#endif

输出:

Premake配置安装:

点击下载:https://github.com/premake/premake-core/releases/download/v5.0.0-beta2/premake-5.0.0-beta2-windows.zip

创建vendor文件夹:文件结构如下

放入声明文件LICENSE.txt:

cpp 复制代码
Copyright (c) 2003-2016 Jason Perkins and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.

  3. Neither the name of Premake nor the names of its contributors may be
     used to endorse or promote products derived from this software without
     specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

编写premake5.lua文件:

Lua 复制代码
workspace "YOTOEngine"		-- sln文件名
	architecture "x64"	
	configurations{
		"Debug",
		"Release",
		"Dist"
	}

-- 组成输出目录:Debug-windows-x86_64
outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}"

project "YOTOEngine"		
	location "YOTOEngine"--在sln所属文件夹下的Hazel文件夹
	kind "SharedLib"--dll动态库
	language "C++"
	targetdir ("bin/" .. outputdir .. "/%{prj.name}") -- 输出目录
	objdir ("bin-int/" .. outputdir .. "/%{prj.name}")-- 中间目录

	-- 包含的所有h和cpp文件
	files{
		"%{prj.name}/src/**.h",
		"%{prj.name}/src/**.cpp"
	}
	-- 包含目录
	includedirs{
		"%{prj.name}/vendor/spdlog-1.x/include"
	}
	-- 如果是window系统
	filter "system:windows"
		cppdialect "C++17"
		-- On:代码生成的运行库选项是MTD,静态链接MSVCRT.lib库;
		-- Off:代码生成的运行库选项是MDD,动态链接MSVCRT.dll库;打包后的exe放到另一台电脑上若无这个dll会报错
		staticruntime "On"	
		systemversion "latest"	-- windowSDK版本
		-- 预处理器定义
		defines{
			"YT_PLATFORM_WINDOWS",
			"YT_BUILD_DLL"
		}
		-- 编译好后移动Hazel.dll文件到Sandbox文件夹下
		postbuildcommands{
			("{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/Sandbox")
		}
	-- 不同配置下的预定义不同
	filter "configurations:Debug"
		defines "YT_DEBUG"
		symbols "On"

	filter "configurations:Release"
		defines "YT_RELEASE"
		optimize "On"

	filter "configurations:Dist"
		defines "YT_DIST"
		optimize "On"

project "Sandbox"
	location "Sandbox"
	kind "ConsoleApp"
	language "C++"

	targetdir ("bin/" .. outputdir .. "/%{prj.name}")
	objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

	files{
		"%{prj.name}/src/**.h",
		"%{prj.name}/src/**.cpp"
	}
	-- 同样包含spdlog头文件
	includedirs{
		"YOTOEngine/vendor/spdlog-1.x/include",
		"YOTOEngine/src"
	}
	-- 引用YOTOEngine
	links{
		"YOTOEngine"
	}

	filter "system:windows"
		cppdialect "C++17"
		staticruntime "On"
		systemversion "latest"

		defines{
			"YT_PLATFORM_WINDOWS"
		}

	filter "configurations:Debug"
		defines "YT_DEBUG"
		symbols "On"

	filter "configurations:Release"
		defines "YT_RELEASE"
		optimize "On"

	filter "configurations:Dist"
		defines "YT_DIST"
		optimize "On"

创建Generate.bat文件,每次将bin和bin-int删除然后点这个文件就可以自动生成。

Lua 复制代码
call vendor\bin\premake\premake5.exe vs2019
PAUSE

遇到问题不要慌:

这个问题是还没创建完就启动了,只需要等一会儿就好了。

等待10秒钟之后:启动!

今天就看到这里,下一集:事件系统【持续更新中】

相关推荐
向宇it10 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
芋芋qwq12 小时前
Unity UI射线检测 道具拖拽
ui·unity·游戏引擎
tealcwu13 小时前
【Unity服务】关于Unity LevelPlay的基本情况
unity·游戏引擎
鹿野素材屋19 小时前
Unity Dots下的动画合批工具:GPU ECS Animation Baker
unity·游戏引擎
小春熙子1 天前
Unity图形学之着色器之间传递参数
unity·游戏引擎·技术美术·着色器
虾球xz1 天前
游戏引擎学习第15天
学习·游戏引擎
Java Fans1 天前
在Unity中实现电梯升降功能的完整指南
unity·游戏引擎
GrimRaider2 天前
[Unity]TileMap开发,TileMap地图缝隙问题
unity·游戏引擎·tilemap
无敌最俊朗@2 天前
unity3d——基础篇小项目(开始界面)
unity·游戏引擎
suzh1qian2 天前
Unity类银河战士恶魔城学习总结(P133 Merge skill tree with Parry skill把技能树和格挡技能相组合)
学习·unity·c#·游戏引擎