大家好,我是阿赵。
在之前介绍HelloWorld的时候,使用了一个打印的命令,把HelloWorld输出到输出日志里面。
对于我们编写程序代码来说,有2个手段是对程序差错非常重要的,一个是断点,另外一个是输出日志。通过输出日志,我们可以知道程序运行的情况,并且检查程序的问题。这里来看看UE里面是怎样输出日志的。
一、 蓝图打印
如果使用蓝图来输出打印,常用的有2种节点,分别是Print String另外一种是Log String
1、 Print String
这个节点的功能会比较多,可以指定输出的打印内容,可以选择输出到屏幕和输出到日志输出(Log),可以指定输出到屏幕时的字体颜色和持续时间。
2、 Log String
这个节点的功能就比较少了,只有输出字符串到日志输出(Log)
屏幕输出指的是在运行时在屏幕的左上角出现打印信息:
输出日志指的是下面输出日志框里面输出文字:
那么接下来就看看,在不用蓝图,而改成C++之后,要怎样实现这些打印输出。
二、 控制台日志输出
如果只是输出到日志,那么使用的方法是:
UE_LOG(CategoryName, Verbosity, Format, ...)
简单举例就是:
UE_LOG(LogTemp, Display, TEXT("Start level1 from CPP"));
这个方法实际上类似于蓝图的Log String功能,比较简单,也是传入字符串然后输出到日志。
不过这里多了一些参数:
第一个参数是打印的对象类型,对应的选项是这一些:
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogPath, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogController, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogPhysics, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogBlueprint, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogBlueprintUserMessages, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogAnimation, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogRootMotion, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogLevel, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogSkeletalMesh, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogStaticMesh, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogNet, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogNetSubObject, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogRep, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogNetPlayerMovement, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogNetTraffic, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogRepTraffic, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogNetDormancy, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogSkeletalControl, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogSubtitle, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogTexture, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogTextureUpload, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogPlayerManagement, Error, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogSecurity, Warning, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogEngineSessionManager, Log, All);
ENGINE_API DECLARE_LOG_CATEGORY_EXTERN(LogViewport, Log, All);
第二个参数Verbosity。这个参数是指定打印级别的,对应的枚举是:
/**
* Enum that defines the verbosity levels of the logging system.
* Also defines some non-verbosity levels that are hacks that allow
* breaking on a given log line or setting the color.
**/
namespace ELogVerbosity
{
enum Type : uint8
{
/** Not used */
NoLogging = 0,
/** Always prints a fatal error to console (and log file) and crashes (even if logging is disabled) */
Fatal,
/**
* Prints an error to console (and log file).
* Commandlets and the editor collect and report errors. Error messages result in commandlet failure.
*/
Error,
/**
* Prints a warning to console (and log file).
* Commandlets and the editor collect and report warnings. Warnings can be treated as an error.
*/
Warning,
/** Prints a message to console (and log file) */
Display,
/** Prints a message to a log file (does not print to console) */
Log,
/**
* Prints a verbose message to a log file (if Verbose logging is enabled for the given category,
* usually used for detailed logging)
*/
Verbose,
/**
* Prints a verbose message to a log file (if VeryVerbose logging is enabled,
* usually used for detailed logging that would otherwise spam output)
*/
VeryVerbose,
// Log masks and special Enum values
All = VeryVerbose,
NumVerbosity,
VerbosityMask = 0xf,
SetColor = 0x40, // not actually a verbosity, used to set the color of an output device
BreakOnLog = 0x80
};
}
稍微改一下打印的参数,看看效果:
UE_LOG(LogAnimation, Warning, TEXT("Start level1 from CPP"));
UE_LOG(LogActor, Error, TEXT("Start level1 from CPP"));
可以看到,第一个参数会出现在输出的前面,可以方便我们用过滤器来过滤打印,然后不同的打印级别,还会出现不同的颜色。
三、 屏幕输出
如果需要在屏幕,调用的是
UEngine::AddOnScreenDebugMessage(int32 Key, float TimeToDisplay, FColor DisplayColor, const FString& DebugMessage, bool bNewerOnTop, const FVector2D& TextScale)
比如这样:
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Start level1 from CPP"));
这里有多个参数
第一个参数是这个log的key
比如我这样打印:
GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::White, TEXT("Start level1 from CPP"));
GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::White, TEXT("Start level1 from CPP 2"));
GEngine->AddOnScreenDebugMessage(2, 5.0f, FColor::White, TEXT("Start level1 from CPP 3"));
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::White, TEXT("Start level1 from CPP 4"));
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::White, TEXT("Start level1 from CPP 5"));
相同的key的打印,会互相顶掉,所以同样是1作为key的两条打印,第二条就把第一条顶掉了。但如果key是-1,那么就不会互相顶掉:
第二个参数是打印的持续时间:
因为需要显示在屏幕上,如果一直出现,会导致屏幕很花,所以在屏幕打印的时候,需要指定一个持续时间,过了时间之后,打印就会消失
第三个参数是打印的颜色:
GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::White, TEXT("Start level1 from CPP 2"));
GEngine->AddOnScreenDebugMessage(2, 5.0f, FColor::Yellow, TEXT("Start level1 from CPP 3"));
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Start level1 from CPP 4"));
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("Start level1 from CPP 5"));
第三个参数就是字符串的内容
第四个参数是控制新的内容是不是在最上面
第五个参数是控制打印内容的缩放:
GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::White, TEXT("Start level1 from CPP"));
GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::White, TEXT("Start level1 from CPP 2"));
GEngine->AddOnScreenDebugMessage(2, 5.0f, FColor::Yellow, TEXT("Start level1 from CPP 3"), true, {3,2});
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Start level1 from CPP 4"));
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("Start level1 from CPP 5"));