上边的代码,我的printf("aaa");没有报错,可见UE4对C语言和C++所有的东西都是原生支持的.
在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之后就可以使用
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 co
**/
#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 co
**/
#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 co
**/
#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
}
;
}
|