UE4_C++自定义log
@
欢迎
Time: 2021年5月4日15:04:01
Author: blackD
Desc: UE4自定义log(输出log格式: 类名/函数名 + 行号 + 自定义输出log)
1. 输出字符串到Output Log中
- DEFINE_LOG_CATEGORY(Log类别名称)
- 实例
FString filePath UE_LOG(LogTemp, Log, TEXT("%d, %s"), 100, *filePath); UE_LOG(LogTemp, Error, TEXT("Hello,World!"));
2. 输出字符串到屏幕上
- 实例
FString filePath;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green,FString::Printf(TEXT("DLL_Init")));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green,FString::Printf(TEXT("%s"),*filePath));
3. 自定义Log日志
- 头文件定义
.h文件
DECLARE_LOG_CATEGORY_EXTERN(MyProjectLog, Log, All);
- cpp文件定义
.cpp文件
DEFINE_LOG_CATEGORY(MyProjectLog);
- 调用自定义Log
需要打印日志的.cpp添加申明自定义日志的头文件
FString filePath
UE_LOG(MyProjectLog, Log, TEXT("%s"), *filePath);
UE_LOG(MyProjectLog, Error, TEXT("Hello,World!"));
3. 个人案例
- 头文件定义
#include "GameFramework/PlayerController.h"
DECLARE_LOG_CATEGORY_EXTERN(LogMyController, Log, All)
//#define LINE_LOG "[" + FDateTime::Now().ToString() + "," + FString(__FUNCTION__) + "," + FString::FromInt(__LINE__) + "], LogMsg: "//Log日志格式: 类名/函数名 + 行号 + msg
#define LINE_LOG ("CTime: %s; CFunc: %s; CLineNum: %s; CLog: ", *FDateTime::Now().ToString(), *FString(__FUNCTION__), *FString::FromInt(__LINE__))//Log日志格式: 类名/函数名 + 行号 + msg
#include "MyPlayerController.generated.h"
- cpp文件定义
#include "Runtime/Engine/Classes/Engine/StaticMeshActor.h"
DEFINE_LOG_CATEGORY(LogMyController)
#include "GenericPlatformFile.h"
- 调用自定义Log
void AMyPlayerController::BeginPlay()
{
UE_LOG(LogMyController, Warning, TEXT("%s, Start"), LINE_LOG);
Super::BeginPlay();
UE_LOG(LogMyController, Warning, TEXT("%s, End"), LINE_LOG);
}