Logging in UE4
Macros that I use for logging/printing.
Use them via PRINT("MyStringToPrint"); or LOG("MyStringToLog"); or LOG("MyStringToLog %f", 3.0f); etc.
#if 1
#include "Engine/Engine.h"
#include "DrawDebugHelpers.h"
#define GETSTR(text, ...) *FString::Printf(TEXT(text), ##__VA_ARGS__)
#define PRINT(color, text, ...) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 10.f, color, CUR_CLASS_LINE + ": " + GETSTR(text, ##__VA_ARGS__))
#define PRINT_S(text, ...) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Yellow, CUR_CLASS_LINE + ": [SUCCESS] " + GETSTR(text, ##__VA_ARGS__))
#define PRINT_E(text, ...) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, CUR_CLASS_LINE + ": [ERROR] " + GETSTR(text, ##__VA_ARGS__))
#define PRINT_W(text, ...) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Orange, CUR_CLASS_LINE + ": [WARNING] " + GETSTR(text, ##__VA_ARGS__))
#define LOG(text, ...) UE_LOG(LogTemp, Log, TEXT("%s: %s"), *CUR_CLASS, GETSTR(text, ##__VA_ARGS__))
#define LOG_S(text, ...) UE_LOG(LogTemp, Log, TEXT("%s: [SUCCESS] %s"), *CUR_CLASS, GETSTR(text, ##__VA_ARGS__))
#define LOG_W(text, ...) UE_LOG(LogTemp, Warning, TEXT("%s: [WARNING] %s"), *CUR_CLASS, GETSTR(text, ##__VA_ARGS__))
#define LOG_E(text, ...) UE_LOG(LogTemp, Error, TEXT("%s: [ERROR] %s"), *CUR_CLASS, GETSTR(text, ##__VA_ARGS__))
#define LOG_D(text, ...) UE_LOG(LogTemp, Display, TEXT("%s: [WARNING] %s"), *CUR_CLASS, GETSTR(text, ##__VA_ARGS__))
#define PRINT_LOG(color, text, ...) PRINT(color, text, ##__VA_ARGS__); LOG(text, ##__VA_ARGS__);
#define PRINT_LOG_S(text, ...) PRINT_S(text, ##__VA_ARGS__); LOG_S(text, ##__VA_ARGS__);
#define PRINT_LOG_W(text, ...) PRINT_W(text, ##__VA_ARGS__); LOG_W(text, ##__VA_ARGS__);
#define PRINT_LOG_E(text, ...) PRINT_E(text, ##__VA_ARGS__); LOG_E(text, ##__VA_ARGS__);
/** Current Class Name + Function Name where this is called! */
#define CUR_CLASS_FUNC (FString(__FUNCTION__))
/** Current Class where this is called! */
#define CUR_CLASS (FString(__FUNCTION__).Left(FString(__FUNCTION__).Find(TEXT(":"))) )
/** Current Function Name where this is called! */
#define CUR_FUNC (FString(__FUNCTION__).Right(FString(__FUNCTION__).Len() - FString(__FUNCTION__).Find(TEXT("::")) - 2 ))
/** Current Line Number in the code where this is called! */
#define CUR_LINE (FString::FromInt(__LINE__))
/** Current Class and Line Number where this is called! */
#define CUR_CLASS_LINE (CUR_CLASS + "(" + CUR_LINE + ")")
/** Current Function Signature where this is called! */
#define CUR_FUNCSIG (FString(__FUNCSIG__))
#else
#define PRINT(color, text)
#define PRINT_S(text)
#define PRINT_E(text)
#define LOG(text, ...)
#define LOG_S(text, ...)
#define LOG_W(text, ...)
#define LOG_E(text, ...)
#define LOG_D(text, ...)
#define PRINT_LOG_S(text)
#define PRINT_LOG_W(text)
#define PRINT_LOG_E(text)
#endif