Inlining the UnrealEngine UE_LOG macro with C++

2.7k views Asked by At

I'm new to Unreal Engine, and I'm trying to declare an inline function as following:

void inline Print(const char* s) {
    UE_LOG(LogTemp, Warning, TEXT("%s"), s);
}

In order to avoid the call to UE_LOG with LogTemp and Warning every time.

When calling for example Print("Hello"), the output is LogTemp:Warning: 效汬o.

My guess is something related to the ASCII encoding, but I'm really not sure.

I also tried to use reinterpret_cast as following:

void inline Print(const char* s) {
UE_LOG(LogTemp, Warning, TEXT("%s"), reinterpret_cast<const TCHAR *>(s));
}

But ended up with same results.

I'd like to know the right way of doing it (I didn't want to use MACRO over inline func), and if there's a simple explanation to what's the reason for the gibberish output, it will also be very useful.

1

There are 1 answers

0
Marson Mao On BEST ANSWER

You can't give const char* to UE_LOG, it requires const TCHAR*, also it can't be reinterpret_casted that way, it needs a conversion, but just let FString handles the dirty work for you. I think you can choose one of the following:

1.

inline void Print(const FString& s)
{
    UE_LOG(LogTemp, Warning, TEXT("%s"), *s);
}

2.

inline void Print(const char* s)
{
    FString str(s);
    UE_LOG(LogTemp, Warning, TEXT("%s"), *str);
}