如何使用UE_LOG(转)

在学习UE4的时候发现,想要在C++代码中输出一个log不知道要怎么办,搜了一下,发现有一个UE_LOG()的相关宏,不过好像用法很复杂.在网上找到了下边的说明,不过我代码可以直接使用C语言的printf()函数来输出log.
如何使用UE_LOG(转) - ♂苹果 - 眼睛想旅行
 
上边的代码,我的printf("aaa");没有报错,可见UE4对C语言和C++所有的东西都是原生支持的.
先上官网的使用说明:
下边是转来的UE_LOG()用法的使用说明:

在Unreal Engine 4中,打Log很方便,可以使用宏:

1
2
UE_LOG ( LogTemp , Warning , TEXT ( "Your message" ) ) ;
 

但是使用之前需要先定义Log Category,Category会在Log中体现出来,以便在茫茫Log中更容易区分其作用或所属模块。
如果想定义只在一个CPP文件中使用的Category,不希望被其他类使用,可以定义一个Static的Category:

1
2
DEFINE_LOG_CATEGORY_STATIC ( CategoryName , Log , All ) ;
 

如果想定义一个‘Public’的Category,并且在全局生效,不管是static函数还是其他类都可以使用,就需要在头文件中声明一个Category,并在CPP中定义,每个用到的CPP文件都需要include该头文件:

1
2
3
4
5
6
7
8
9
10
// in A.h
DECLARE_LOG_CATEGORY_EXTERN ( CategoryName , Log , All ) // 声明一个Category为extern,避免多个文件使用此头文件时重复声明
 
// in A.cpp
#include "A.h"
DEFINE_LOG_CATEGORY ( CategoryName ) // 定义该Category,全局仅需一份
 
// in B.cpp
#include "A.h" // 由于之前声明为extern,使用者引入头文件即可使用在A.cpp中的定义
 

如果想给某个定义一个专属的Category,可以使用:

1
2
3
4
5
6
7
8
9
// in A.h
class A
{
DECLARE_LOG_CATEGORY_CLASS ( CategoryName , Log , All ) ;
} ;
 
// in A.cpp
DEFINE_LOG_CATEGORY_CLASS ( A , CategoryName ) ;
 

定义好Log Category之后就可以使用 de style="border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 13px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;" >UE_LOG de>了,除了最前面例子中的用法, de style="border: 0px; font-family: Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace; font-size: 13px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; line-height: normal;" >UE_LOG de>的用法和C中的printf类似,支持字符串Format,例如:

1
2
3
4
UE_LOG ( CategoryName , Warning , TEXT ( "MyCharacter's Health is %d" ) , MyCharacter - & gt ; Health ) ;
UE_LOG ( CategoryName , Warning , TEXT ( "MyCharacter's Health is %f" ) , MyCharacter - & gt ; Health ) ;
UE_LOG ( CategoryName , Warning , TEXT ( "MyCharacter's Name is %s" ) , * MyCharacter - & gt ; GetName ( ) ) ;
 

‘UE_LOG’和声明或定义Log Category的宏中提到的’Log’,’Warning’,’All’等,是ELogVerbosity中定义的枚举,具体含义可参考下面的源码。

相关源码:

声明和定义Log Category的宏:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* A macro to declare a logging category as a C++ "extern"
* @param CategoryName, category to declare
* @param DefaultVerbosity, default run time verbosity
* @param CompileTimeVerbosity, maximum verbosity to compile into the code
**/
#define DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
extern struct FLogCategory ##CategoryName : public FLogCategory<ELogVerbosity::DefaultVerbosity, ELogVerbosity::CompileTimeVerbosity>
{
FORCEINLINE FLogCategory ##CategoryName() : FLogCategory(TEXT(#CategoryName)) {}
} CategoryName ;
 
/**
* A macro to define a logging category, usually paired with DECLARE_LOG_CATEGORY_EXTERN from the header.
* @param CategoryName, category to define
**/
#define DEFINE_LOG_CATEGORY(CategoryName) FLogCategory##CategoryName CategoryName;
 
/**
* A macro to define a logging category as a C++ "static"
* @param CategoryName, category to declare
* @param DefaultVerbosity, default run time verbosity
* @param CompileTimeVerbosity, maximum verbosity to compile into the code
**/
#define DEFINE_LOG_CATEGORY_STATIC(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
static struct FLogCategory ##CategoryName : public FLogCategory<ELogVerbosity::DefaultVerbosity, ELogVerbosity::CompileTimeVerbosity>
{
FORCEINLINE FLogCategory ##CategoryName() : FLogCategory(TEXT(#CategoryName)) {}
} CategoryName ;
 
/**
* A macro to declare a logging category as a C++ "class static"
* @param CategoryName, category to declare
* @param DefaultVerbosity, default run time verbosity
* @param CompileTimeVerbosity, maximum verbosity to compile into the code
**/
#define DECLARE_LOG_CATEGORY_CLASS(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
DEFINE_LOG_CATEGORY_STATIC ( CategoryName , DefaultVerbosity , CompileTimeVerbosity )
 
/**
* A macro to define a logging category, usually paired with DECLARE_LOG_CATEGORY_CLASS from the header.
* @param CategoryName, category to definee
**/
#define DEFINE_LOG_CATEGORY_CLASS(Class, CategoryName) Class::FLogCategory##CategoryName Class::CategoryName;
 

ELogVerbosity中的枚举:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace ELogVerbosity
{
enum Type
{
/** Not used */
NoLogging = 0 ,
 
/** Always prints s 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
} ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值