How to Log into Console and on Display
Every time I encounter strange behavior in my code that I don't understand, the first thing I do is pepper it with logs to see if the code is even being reached and what values are being passed to the function calls.
While I could set breakpoints and stop the program's execution to step through the code and see what's happening, I must admit that I'm often just too lazy to run the editor via Visual Studio. A simple log here and there, and I get the information I need.
For critical sections where errors occur or odd arguments are passed to a function, I often keep logs permanently to be alerted when a serious error happens.
And every time I want to output logs to the console in the Unreal Editor, I google anew how to do it because I forget each time. So, I'm documenting the logging features not just for myself, but for you too.
UE_LOG
Here's an example of a simple log output:
UE_LOG(LogTemp, Display, TEXT("This is a log to the console and log file"));
The Unreal Engine has implemented the logging function via a macro. This macro expects 3 parameters:
- The category name.
- The log verbosity level.
- The text to be output.
Category
For the category, I currently only use LogTemp
, but you can create your own categories to be considered in the log. You can create your own categories with the macro:DECLARE_LOG_CATEGORY_EXTERN(<LOG_CATEGORY>, <VERBOSITY_LEVEL>, All);
For completeness, here is a complete list of all categories internally used by Unreal:
LogHAL
,LogSerialization
,LogUnrealMath
,LogUnrealMatrix
,LogContentComparisonCommandlet
,LogNetPackageMap
,LogNetSerialization
,LogMemory
,LogProfilingDebugging
,LogCore
,LogOutputDevice
,LogSHA
,LogStats
,LogStreaming
,LogInit
,LogExit
,LogExec
,LogScript
,LogLocalization
,LogLongPackageNames
,LogProcess
,LogLoad
,LogVirtualization
,LogTemp
Log Verbosity
The Log Verbosity is an enum that indicates the urgency of the log. This affects under which environments logging occurs, in what color the log is output, and which output channel is used.
Below is a table explaining the Log Verbosity Levels, taken from the Official Unreal Engine Documentation:
Enum | Description |
---|---|
Fatal | Always prints a fatal error to the console and log file, then crashes even if logging is disabled. |
Error | Prints an error to the console and log file. Commandlets and the editor collect and report errors. Error messages result in a commandlet failure. |
Warning | Prints a warning to the console and log file. Commandlets and the editor collect and report warnings. |
Display | Prints a message to the console and log file. |
Log | Prints a message to the log file, however, it does not print to the console. |
Verbose | Prints a verbose message to a log file if Verbose logging is enabled for the given category. This is usually used for detailed logging. |
VeryVerbose | Prints a verbose message to a log file. If VeryVerbose logging is enabled, then this is used for detailed logging that would otherwise spam output. |
Text to be Logged
For the parameter to be passed. The simplest option is a static text, like this one:
UE_LOG(LogTemp, Display, TEXT("Hello World"));
FString
If you want to represent dynamic text, you can pass an FString as a parameter. This works similarly to the fprintf function in C++:
FString listenerNameString = *(listenerName.getName());
UE_LOG(LogTemp, Error, TEXT("hash collision occured! %s"), listenerNameString);
Integer
int32 ExampleInteger = 123;
UE_LOG(LogTemp, Warning, TEXT("The integer value is: %d"), ExampleInteger);
Float
float ExampleFloat = 1.0f;
UE_LOG(LogTemp, Warning, TEXT("The float value is: %f"), ExampleFloat);
Multiple Specifiers
UE_LOG(LogTemp, Warning, TEXT("Current values are: vector %s, float %f, and integer %d"), *ExampleVector.ToString(), ExampleFloat, ExampleInteger);
Debugging Message on the Screen
If you want to display a log message on the screen, you can do so using the AddOnScreenDebugMessage
function. I use on-screen logging rather infrequently, but it's handy when you want to quickly see a reaction to your action in the game or display a periodically updating value without cluttering the log. For example, it can be used to display coordinates that update every 100 milliseconds.
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 0.1f, FColor::White, TEXT("This is an Example on-screen debug message."));
}
The function is passed the following parameters:
/**
* This function will add a debug message to the onscreen message list.
* It will be displayed for FrameCount frames.
*
* @param Key A unique key to prevent the same message from being added multiple times.
* @param TimeToDisplay How long to display the message, in seconds.
* @param DisplayColor The color to display the text in.
* @param DebugMessage The message to display.
*/
void UEngine::AddOnScreenDebugMessage(uint64 Key, float TimeToDisplay, FColor DisplayColor, const FString& DebugMessage, bool bNewerOnTop, const FVector2D& TextScale)