如何使用UE_LOG

在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
classA
{
    DECLARE_LOG_CATEGORY_CLASS(CategoryName,Log,All);
};
 
// in A.cpp
DEFINE_LOG_CATEGORY_CLASS(A,CategoryName);

定义好Log Category之后就可以使用 UE_LOG了,除了最前面例子中的用法, UE_LOG的用法和C中的printf类似,支持字符串Format,例如:

UE_LOG(CategoryName,Warning,TEXT("MyCharacter's Health is %d"), MyCharacter->Health );
UE_LOG(CategoryName,Warning,TEXT("MyCharacter's Health is %f"), MyCharacter->Health );
UE_LOG(CategoryName,Warning,TEXT("MyCharacter's Name is %s"), *MyCharacter->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)
externstructFLogCategory##CategoryName : public FLogCategory<ELogVerbosity::DefaultVerbosity, ELogVerbosity::CompileTimeVerbosity>
{
    FORCEINLINEFLogCategory##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)
staticstructFLogCategory##CategoryName : public FLogCategory<ELogVerbosity::DefaultVerbosity, ELogVerbosity::CompileTimeVerbosity>
{
    FORCEINLINEFLogCategory##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中的枚举:

namespaceELogVerbosity
{
enumType
{
/** 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
};
}

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值