Log Macro with Net Mode and Color
@alert info This is a legacy article imported from the old wiki. An updated version can be found here: [Not Created Yet] @endalert Overview A macro that adds the current net mode and color to loggi...
This is a legacy article imported from the old wiki.
An updated version can be found here: [Not Created Yet]
Overview
A macro that adds the current net mode and color to logging, as well as making it easier to use.
Not recommended as a replacement for UE_LOG, but as a quick'n dirty tool for rapid iteration.
YourProject.h
#define NETMODE_WORLD (((GEngine == nullptr)
(GetWorld() == nullptr)) ? TEXT("") \
: (GEngine->GetNetMode(GetWorld()) == NM_Client) ? TEXT("[Client] ") \
: (GEngine->GetNetMode(GetWorld()) == NM_ListenServer) ? TEXT("[ListenServer] ") \
: (GEngine->GetNetMode(GetWorld()) == NM_DedicatedServer) ? TEXT("[DedicatedServer] ") \
: TEXT("[Standalone] "))
#if _MSC_VER
#define FUNC_NAME TEXT(__FUNCTION__)
#else // FIXME - GCC?
#define FUNC_NAME TEXT(__func__)
#endif
#define TRACE(Format, ...) \
{ \
SET_WARN_COLOR(COLOR_CYAN);\
const FString Msg = FString::Printf(TEXT(Format), ##__VA_ARGS__); \
if (Msg == "") \
{ \
UE_LOG(LogYourCategory, Log, TEXT("%s%s() : %s"), NETMODE_WORLD, FUNC_NAME, *GetNameSafe(this));\
} \
else \
{ \
UE_LOG(LogYourCategory, Log, TEXT("%s%s() : %s"), NETMODE_WORLD, FUNC_NAME, *Msg);\
} \
CLEAR_WARN_COLOR();\
}
#define TRACESTATIC(Format, ...) \
{ \
SET_WARN_COLOR(COLOR_CYAN);\
const FString Msg = FString::Printf(TEXT(Format), ##__VA_ARGS__); \
UE_LOG(LogYourCategory, Log, TEXT("%s() : %s"), FUNC_NAME, *Msg);\
CLEAR_WARN_COLOR();\
}
#define TRACEWARN(Format, ...) \
{ \
SET_WARN_COLOR( COLOR_YELLOW );\
const FString Msg = FString::Printf(TEXT(Format), ##__VA_ARGS__); \
UE_LOG(LogYourCategory, Log, TEXT("**WARNING** %s%s() : %s"), NETMODE_WORLD, FUNC_NAME, *Msg);\
CLEAR_WARN_COLOR();\
}
#define TRACEERROR(Format, ...) \
{ \
SET_WARN_COLOR( COLOR_RED );\
const FString Msg = FString::Printf(TEXT(Format), ##__VA_ARGS__); \
UE_LOG(LogYourCategory, Log, TEXT("**ERROR** %s%s() : %s"), NETMODE_WORLD, FUNC_NAME, *Msg);\
CLEAR_WARN_COLOR();\
}
#define SCREENDEBUG(Format, ...) \
{ \
const FString Msg = FString::Printf(TEXT(Format), ##__VA_ARGS__); \
if (Msg == "") \
{ \
TCHAR StdMsg[MAX_SPRINTF] = TEXT(""); \
FCString::Sprintf(StdMsg, TEXT("%s%s() : %s"), NETMODE_WORLD, FUNC_NAME, *GetNameSafe(this)); \
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, StdMsg); \
} \
else \
{ \
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, Msg); \
} \
}
YourProject.cpp
DEFINE_LOG_CATEGORY( LogYourCategory );
Examples
Function logging
Usage:
void USomeClass::WithSomeFunction()
{
TRACE("");
}
Output:
LogYourCategory: [Standalone] USomeClass::WithSomeFunction() :
Variable logging
Usage:
void USomeClass::WithSomeFunction()
{
TRACE("Name: %s, Health %d, DistanceToEnemy: %f", *PlayerName.ToString(), Health, GetDistance(EnemyLoc));
}
Output:
LogYourCategory: [Client] USomeClass::WithSomeFunction() : Name: DudeGuy, Health 76, DistanceToEnemy: 128.0512