Hey!đź‘‹ ubyte.dev just recently launched. It's dedicated towards Unreal Engine 5.

Log function names automatically with __FUNCTIONW__

November 27, 2022
Lucas Tchikhinachvili
Using certain macros (or compiler-generated fields) such as __FUNCTION__ and __FUNCTIONW__

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

__func__

TestFunction

__FUNCTION__

AMyActor::TestFunction

__FUNCTIONW__

AMyActor::TestFunction

__FUNCSIG__

void AMyActor::TestFunction(AActor* UselessArgument)

__PRETTY_FUNCTION__

void AMyActor::TestFunction(AActor* UselessArgument)

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:

  1. 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 the ANSI_TO_TCHAR macro.

  2. __PRETTY_FUNCTION__ may not compile. Use __FUNCSIG__ instead.

  3. 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);
    }
cross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram