Using UE_LOG properly without fatal crashes

So while using logs (UE_LOG) throughout my code to debug my game development, I’ve been running into this issue constantly that just does not seem to really make much sense where UE_LOG will completely crash the game if the variable it is trying to log is not there (NULL). I understand that C++ is a little more unforgiving than that of uScript from UDK, however whenever I would use uScript I could log a variable, and if that variable was null it would print out a “NONE” instead, and the game would go on. This was extremely helpfully if perhaps I ran into race conditions due to a bad system, and especially during Replication to figure out if a client received the right information.

In example, if I was logging a weapon instance, I could easily log the variable and determine if it was there or not.

In uScript I could do


`log("Character Weapon = "@Weapon); 

this would produce a log saying “Character Weapon = None” if it failed.

In UE4, I would have to go with


UE_LOG(LogName,All,TEXT("Character Weapon = %s"),*Weapon->GetName());

Which produces an instant/fatal crash of the entire game if there is no Weapon. I don’t feel like I’m doing this right if this is the case, because what’s the point of logging if it can’t log the lack of something?

My current solution is to convert it into a hunky if statement


if(Weapon->GetName() == NULL)
{
 UELOG(LogName,All,TEXT("NO WEAPON!"));
}
else
{
UE_LOG(LogName,All,TEXT("Character Weapon = %s"),*Weapon->GetName());
}


This results in extremely painful setups and conditioning just to log a variable to see if, or if not it is there. Can anyone explain to me what i’m doing wrong here?



UE_LOG(LogName,All,TEXT("Character Weapon = %s"),(Weapon != NULL) ?  *Weapon->GetName() : TEXT("nullptr"));


The above is a way to write out your if statement into one single line if you prefer that. The “?” is a if/else check with the first result happening if true and the second (after the colon) resulting if false.

In terms of checking against NULL I believe in UE4 there are better methods such as IsValid() that should likely be used instead.

Using this or a similar method you could also write your own logging function/macro that calls UE_LOG with a check built in to see if the result is null before printing it. There may be a pre-existing log function that already does similar to this but I have not come across it yet if there is.