UE4日志输出需要这样:UE_LOG(LogTemp, Log, TEXT(“Log Here %s”), *FString(“Other Msg”));
或者在屏幕上输出需要这样:GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, “Print Msg Here”);
有的时候我们只想打个简单的日志,希望能这样:LogInfo(“LogHere”);或者LogInfoFormat(“LogHere”, *FString(Other Msg"));
那么可以这样做:
新建一个.h文件,写入一下代码:
#pragma once
//Current Class Name + Function Name where this is called!
#define JOYSTR_CUR_CLASS_FUNC (FString(__FUNCTION__))
//Current Class where this is called!
#define JOYSTR_CUR_CLASS (FString(__FUNCTION__).Left(FString(__FUNCTION__).Find(TEXT(":"))) )
//Current Function Name where this is called!
#define JOYSTR_CUR_FUNC (FString(__FUNCTION__).Right(FString(__FUNCTION__).Len() - FString(__FUNCTION__).Find(TEXT("::")) - 2 ))
//Current Line Number in the code where this is called!
#define JOYSTR_CUR_LINE (FString::FromInt(__LINE__))
//Current Function Signature where this is called!
#define JOYSTR_CUR_FUNCSIG (FString(__FUNCSIG__))
//Current Class and Line Number where this is called!
#define JOYSTR_CUR_CLASS_LINE (JOYSTR_CUR_CLASS_FUNC + "(" + JOYSTR_CUR_LINE + ")")
//Print Message On Screen
#define ScreenLog(Param1) (ScreenLogT(Param1, 5))
#define ScreenLogT(Param1, Param2) (GEngine->AddOnScreenDebugMessage(-1, Param2, FColor::White, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1)))
#define ScreenWarn(Param1) (ScreenWarnT(Param1, 5))
#define ScreenWarnT(Param1, Param2) (GEngine->AddOnScreenDebugMessage(-1, Param2, FColor::Yellow, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1)))
#define ScreenError(Param1) (ScreenErrorT(Param1, 5))
#define ScreenErrorT(Param1, Param2) (GEngine->AddOnScreenDebugMessage(-1, Param2, FColor::Red, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1)))
//UE LOG!
#define LogInfo(Param1) UE_LOG(LogTemp, Log, TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define LogInfoFormat(FormatString , ...) UE_LOG(LogTemp, Log,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ))
#define LogWarn(Param1) UE_LOG(LogTemp, Warning, TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define LogWarnFormat(FormatString , ...) UE_LOG(LogTemp, Warning,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ))
#define LogError(Param1) UE_LOG(LogTemp, Error, TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define LogErrorFormat(FormatString , ...) UE_LOG(LogTemp, Error,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ))
//Custom Category
#define CLogInfo(LogCat, Param1) UE_LOG(LogCat, Log, TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define CLogInfoFormat(LogCat, FormatString , ...) UE_LOG(LogCat,Log,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ))
#define CLogWarn(LogCat, Param1) UE_LOG(LogCat, Warning, TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define CLogWarnFormat(LogCat, FormatString , ...) UE_LOG(LogCat,Warning,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ))
#define CLogError(LogCat, Param1) UE_LOG(LogCat, Error, TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define CLogErrorFormat(LogCat, FormatString , ...) UE_LOG(LogCat,Error,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ))
就OK了,不紧附带屏幕日志功能,而且还附带函数调用和行号显示,是不是很方便呢