在.h 文件中,填写如下代码:
DECLARE_LOG_CATEGORY_EXTERN(YourLog, Log, All);
在.cpp文件中,填写如下代码:
DEFINE_LOG_CATEGORY(YourLog);
输出格式如下:
(1)UE_LOG(LogTemp, Warning, TEXT("Problem on load Province Message!"));
(2)UE_LOG(LogTemp, Warning, TEXT("Content:%s"), *(Response->GetContentAsString()));
-
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() );
https://www.cnblogs.com/blueroses/p/6037981.html
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TestHUDString);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Some variable values: x: %d, y: %d"), HitX, HitY) )
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString()));
游戏模式:
在游戏模式下,你需要在游戏的快捷方式后面加 -Log,才会在游戏中显示。
编辑器模式(Play In Editor):
你可以在Output窗口中看到log信息。
如果想在游戏中看到,需要到Engin.ini中修改参数添加"GameCommandLine=-log,如果没有,则需要按~,输入-Log命令开启。
快速使用:
UE_LOG(LogTemp, Warning, TEXT("Your message"));
不用设置标签,简单快速。
设置拥有自己标签的Log:
在你的游戏头文件中加入:
//General Log
DECLARE_LOG_CATEGORY_EXTERN(YourLog, Log, All);
//Logging during game startup
DECLARE_LOG_CATEGORY_EXTERN(YourInit, Log, All);
//Logging for your AI system
DECLARE_LOG_CATEGORY_EXTERN(YourAI, Log, All);
//Logging for Critical Errors that must always be addressed
DECLARE_LOG_CATEGORY_EXTERN(YourCriticalErrors, Log, All);
这样输出的Log你就可以知道是哪个部分的,这也是UE_Log很有用的原因。
在你的游戏Cpp文件中:
//General Log
DEFINE_LOG_CATEGORY(YourLog);
//Logging during game startup
DEFINE_LOG_CATEGORY(YourInit);
//Logging for your AI system
DEFINE_LOG_CATEGORY(YourAI);
//Logging for Critical Errors that must always be addressed
DEFINE_LOG_CATEGORY(YourCriticalErrors);
Log格式:
Log Message
//"This is a message to yourself during runtime!"
UE_LOG(YourLog,Warning,TEXT("This is a message to yourself during runtime!"));
Log an FString
%s strings are wanted as TCHAR* by Log, so use *FString()
//"MyCharacter's Name is %s"
UE_LOG(YourLog,Warning,TEXT("MyCharacter's Name is %s"), *MyCharacter->GetName() );
Log an Int
//"MyCharacter's Health is %d"
UE_LOG(YourLog,Warning,TEXT("MyCharacter's Health is %d"), MyCharacter->Health );
Log a Float
//"MyCharacter's Health is %f"
UE_LOG(YourLog,Warning,TEXT("MyCharacter's Health is %f"), MyCharacter->Health );
Log an FVector
//"MyCharacter's Location is %s"
UE_LOG(YourLog,Warning,TEXT("MyCharacter's Location is %s"),
*MyCharacter->GetActorLocation().ToString());
Log an FName
//"MyCharacter's FName is %s"
UE_LOG(YourLog,Warning,TEXT("MyCharacter's FName is %s"),
*MyCharacter->GetFName().ToString());
Log an FString,Int,Float
//"%s has health %d, which is %f percent of total health"
UE_LOG(YourLog,Warning,TEXT("%s has health %d, which is %f percent of total health"),
*MyCharacter->GetName(), MyCharacter->Health, MyCharacter->HealthPercent);
Log的颜色设置:
//"this is Grey Text"
UE_LOG(YourLog,Log,TEXT("This is grey text!"));
//"this is Yellow Text"
UE_LOG(YourLog,Warning,TEXT("This is yellow text!"));
//"This is Red Text"
UE_LOG(YourLog,Error,TEXT("This is red text!"));
可以看得出第二个参数是是用来控制颜色的。
传递客户端信息(网络模式):
PlayerController->ClientMessage("Your Message");
命令行命令以及Engine.ini配置:
Log conventions (in the console, ini files, or environment variables)
[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
关卡,显示某某关卡的Log
At boot time, compiled in default is overridden by ini files setting, which is overridden by command line
Log console command usage
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]
Log command line
-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
Environment variables
Any command line option can be set via the environment variable UE-CmdLineArgs
set UE-CmdLineArgs=\"-LogCmds=foo verbose breakon, bar off\"
Config file
In DefaultEngine.ini or Engine.ini:
[Core.Log]
global=[default verbosity for things not listed later]
[cat]=[level]
foo=verbose break
/**
* 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;
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
};
}