【资料图】
1. 概述
相比各种打断点调试的办法,还是更习惯使用打印输出来进行调试。
2. 详论
2.1. 代码
这里写了三个函数:分别是输出到屏幕,输出到警告日志,输出错误日志。
Output.h:
#pragma oncenamespace Output{void PrintScreen(const char* lpszFormat, ...);void PrintLogWarning(const char* lpszFormat, ...);void PrintLogError(const char* lpszFormat, ...);};
Output.cpp:
#include "Output.h"#include #include #include namespace Output{void PrintScreen(const char* lpszFormat, ...){char szInfo[512];va_list argList;va_start(argList, lpszFormat);vsnprintf(szInfo, 512, lpszFormat, argList);va_end(argList);//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("Print Message this is %f"), 2.3f));GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString(UTF8_TO_TCHAR(szInfo)));}void PrintLogWarning(const char* lpszFormat, ...){char szInfo[512];va_list argList;va_start(argList, lpszFormat);vsnprintf(szInfo, 512, lpszFormat, argList);va_end(argList);UE_LOG(LogTemp, Warning, TEXT("%s"), UTF8_TO_TCHAR(szInfo));}void PrintLogError(const char* lpszFormat, ...){char szInfo[512];va_list argList;va_start(argList, lpszFormat);vsnprintf(szInfo, 512, lpszFormat, argList);va_end(argList);GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString(UTF8_TO_TCHAR(szInfo)));UE_LOG(LogTemp, Error, TEXT("%s"), UTF8_TO_TCHAR(szInfo));}};
2.2. 解析
打印输出信息我认为还是C语言的格式化输出比较方便(类似
printf("个数:%d", num);
),使用格式化占位符来拼接字符串。UE的字符串类FString管理的是TCHAR字符串,TCHAR是char与wchar_t的封装,在ANSI本地编码中是char,在Unicode国际化编码中是wchar_t。
UTF8_TO_TCHAR宏会将UTF8字符串转换成TCHAR字符串。这段输出打印代码如果要输出中文,需要把代码文件的编码更改为UTF8编码。
输出日志可以显示在“输出日志”面板:窗口->开发者工具->输出日志。
代码地址