Logging a direct text is rather simple:
UE_LOG(LogTemp, Error, TEXT("My Error Message")));
Logging a direct FString for the output log can be kinda tricky and took me ages to figure out. This method works perfectly and avoids all TChar* erros:
FString String = "My Output String";
UE_LOG(LogTemp, Error, TEXT("%s"), *String);
The important thing here is to de-reference the String (*String).
This is how you print the infor using the formated log command:
FString String = "My Output String";
UE_LOGFMT(LogTemp, Warning, "{A}", Message);
This is a bit like formatting regular texts, where the letters, numbers or words in the brackets basically represent variables.
Variant 1 (untested):
Let's imagine we have an Enum (Unreal Engine) called "EMyEnum" and we wan't the currently selected value to be printed to the Log.
UE_LOG(LogTemp, Error, TEXT("%s"), *UEnum::GetValueAsString(EMyEnum.Entry1);
UE_LOG(LogTemp, Error, TEXT("%s"), *UEnum::GetDisplayValueAsString((EMyEnum::Entry1)).ToString());
Variant 2:
FString NewString = UEnum::GetValueAsString(EMyEnum);
UE_LOG(LogTemp, Error, TEXT("%s"), *NewString);
This formats the ENum as an FString and then logs it.
To change the way LOG messages are displayed, you can use various different verbosities. For example:
UE_LOG(LogTemp, Fatal...
UE_LOG(LogTemp, Error...
UE_LOG(LogTemp, Warning...
UE_LOG(LogTemp, Display...
For more information, check out the official Verbosity Documentation.
Although it may seem simple, printing an FString to the Screen is not as straight forward as it seems. Most of the example online use the FString::Printf(TEXT("Message")) method for a static message. But trying to print an FString can cause issues if you try to adopt the mentioned message. Therefore here is an example using a Vector:
FVector Location = {0.f, 0.f, 10.f};
FString MyString = "The Location is: " + Location.ToString();
GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, MyString);
To log information to a .txt file, the following code seems to be working great:
bool UCPPFL_EUSU::WriteToTextFile(
const bool DeletePreviousFile,
const FString Filename,
const FString Text)
{
const FString FilePath =
FPaths::ConvertRelativePathToFull(FPaths::ProjectDir())
+ Filename
+ ".txt";
// File already exists, add new entry to it.
if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*FilePath) && !DeletePreviousFile)
{
FString FoundString;
FFileHelper::LoadFileToString(FoundString, *FilePath);
const FString UpdatedString =
FoundString
+ "\n"
+ Text;
FFileHelper::SaveStringToFile(
UpdatedString,
*FilePath,
FFileHelper::EEncodingOptions::AutoDetect,
&IFileManager::Get());
return true;
}
// File does not already exist, create a new one.
const FString LogTitleBar = "========== EUSU " + Filename + " ==========\n\n";
FFileHelper::SaveStringToFile(
LogTitleBar + Text,
*FilePath,
FFileHelper::EEncodingOptions::AutoDetect,
&IFileManager::Get());
return false;
}
Info: this checks if a file has already been created. If so, it simply just updates it. If not, it creates one and then updates it continuously.
A helpful external resource can be found HERE and HERE.
Note: FStrings are always heap allocated, so using the above solution may be very slow. A good practice would be to reserve memory for the string beforehand to reduce heap allocations and therefore system-calls. You can use .Reserve(23) just like you would for TArray.