上边的代码,我的printf("aaa");没有报错,可见UE4对C语言和C++所有的东西都是原生支持的.


先上官网的使用说明:


​https://wiki.unrealengine.com/Logs,_Printing_Messages_To_Yourself_During_Runtime​


下边是转来的UE_LOG()用法的使用说明:


转自: ​​http://code.roastew.com/?p=451​​


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

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


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


DEFINE_LOG_CATEGORY_STATIC          (          CategoryName          ,                     Log          ,                     All          )          ;


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


// 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,可以使用:



// 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,例如:



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的宏:


/**




* 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中的枚举:

   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




}
;




}