UE中日志定义和使用

记录下在UE中定义一个日志和使用类别的方式.

分类

  • 可以定义静态日志(只有当前CPP使用)
  • 共享日志类别(多个CPP模块都可用)

日志的定义

头文件中添加

DECLARE_LOG_CATEGORY_EXTERN(YOUR_LOG_CATOGORY_NAME, Log, All);

在实现CPP中添加

DEFINE_LOG_CATEGORY(YOUR_LOG_CATOGORY_NAME);

在其他文件中引用

可以直接包含声明的日志的头文件, 也可以添加一行声明。

//直接包含声明
#include "YourDeclareHeader.h"

//或者使用
DECLARE_LOG_CATEGORY_EXTERN(YOUR_LOG_CATOGORY_NAME, Log, All);

使用

format 字段类比C中的printf的格式化描述字符串 。

//"this is Grey Text"
UE_LOG(YOUR_LOG_CATOGORY_NAME, Log, TEXT("This is grey text! %s"), *Actor->GetString());
//"this is Yellow Text"
UE_LOG(YOUR_LOG_CATOGORY_NAME,  Warning,    TEXT("This is yellow text!"));
//"This is Red Text"
UE_LOG(YOUR_LOG_CATOGORY_NAME,  Error,  TEXT("This is red text!"));

日志开关

以下代码中, 其中[cat]表示的是日志的类别标签, 也就是以上 YOUR_LOG_CATOGORY_NAME
[level] 表示日志的等级过滤

[cat] = a category for the command to operate on, or 'global' for all categories. 标签,没有设置就显示所有的Log
[level] = verbosity level, one of: none, error, warning, display, log, verbose, all, default

环境变量中设置

set UE-CmdLineArgs=\"-LogCmds=foo verbose breakon, bar off\"

可以在命令行中设置

-LogCmds=\"[arguments],[arguments]...\"           - applies a list of console commands at boot time
-LogCmds=\"foo verbose, bar off\"         - turns on the foo category and turns off the bar category

在Engine.ini配置中设置

[Core.Log]
global=[default verbosity for things not listed later]
[cat]=[level]
foo=verbose break

在控制台动态设置

可以使用以下命令

Log list - list all log categories
Log list [string] - list all log categories containing a substring
Log reset - reset all log categories to their boot-time default
Log [cat] - toggle the display of the category [cat]
Log [cat] off - disable display of the category [cat]
Log [cat] on - resume display of the category [cat]
Log [cat] [level] - set the verbosity level of the category [cat]
Log [cat] break - toggle the debug break on display of the category [cat]