Logging automatically generated function string literals
Underneath are what certain macros (or compiler-generated fields) automatically expand to inside the scope of void AMyActor::TestFunction(AActor* UselessArgument)
:
Macro / field | Expands to |
|
|
|
|
|
|
|
|
|
|
Usage example (with output commented)
void AMyActor::TestFunction(AActor* UselessArgument)
{
// TestFunction
UE_LOG(LogTemp, Warning, TEXT("%s"), ANSI_TO_TCHAR(__func__));
// AMyActor::TestFunction
UE_LOG(LogTemp, Warning, TEXT("%s"), TEXT(__FUNCTION__));
// AMyActor::TestFunction
UE_LOG(LogTemp, Warning, TEXT("%s"), __FUNCTIONW__);
// void AMyActor::TestFunction(AActor* UselessArgument)
UE_LOG(LogTemp, Warning, TEXT("%s"), TEXT(__FUNCSIG__));
// May not compile. Use __FUNCSIG__
// void AMyActor::TestFunction(AActor* UselessArgument)
UE_LOG(LogTemp, Warning, TEXT("%s"), TEXT(__PRETTY_FUNCTION__));
}
Notes:
Some platforms and compilers that Unreal Engine 5 supports may not provide support for the
__FUNCTIONW__
macro. For example, GCC and Clang, which are commonly used on Linux and macOS platforms, do not provide support for the__FUNCTIONW__
macro. Similarly, other platforms that Unreal Engine 5 supports, such as Android or iOS, may not provide support for this macro.Using the
ANSI_TO_TCHAR(__FUNCTION__)
macro instead of__FUNCTIONW__
is a more portable solution and should be safe to use in a plugin for Unreal Engine 5.The
ANSI_TO_TCHAR
macro is a helper macro in Unreal Engine that is used to convert ANSI (narrow-character) strings to TCHAR (wide-character) strings, which is a more portable way to handle character strings in Unreal Engine code. The__FUNCTION__
macro returns the name of the current function as a narrow-character string, which can be converted to a wide-character string using theANSI_TO_TCHAR
macro.__PRETTY_FUNCTION__
may not compile. Use__FUNCSIG__
instead.If you’re usingÂ
__FUNCSIG__
 and get this:void __cdecl AInteractableActor::BeginPlay(void)
There's a hack to remove the "__cdecl" part:
{ FString Funcsig = __FUNCSIG__; int32 Index = Funcsig.Find("__cdecl"); if (Index != -1) { Funcsig.RemoveAt(Index, 8); } UE_LOG(LogTemp, Warning, TEXT("%s"), *Funcsig); }