Using std::cout and UE_LOG, get ostream into Output console

When I debug my usual console C++ programs, I use cout function a lot - of course there are gdb and other debuggers, but clumsy and raw practice taught me using this simple method. Long story short - natural equivalent of cout in UE4 is UE_LOG, but gods now it is not very comfortable to use - it’s more like printf. Is it some simple way to tell Unreal Engine to take stream from cout to Output Console?

I posted similar question on StackOverflow and, after day of searching, I found the answer. It is possible - to do so I had to import library sstream. After this I made a class derived from the std::stringbuf and I overshadowed protected method sync() here. The body of my class:

class LStream : public std::stringbuf{
protected:
	int sync() {
		UE_LOG(LogTemp, Log, TEXT("%s"), *FString(str().c_str()));
		str("");
		return std::stringbuf::sync();
	}
};

Thanks to this, I could get any text wrote to cout into Console Output in Unreal Editor. Only thing I had to change was:

LStream Stream;
std::cout.rdbuf(&Stream); 

before any writting. Now we can use complete functional std::cout in Unreal Engine!

I have also made a short tutorial with this solution: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

UE_LOG is actually just template, there is a lower level output device management, FOutputDeviceRedirector (which is technicly FOutputDevice pointer)

https://docs.unrealengine.com/latest/INT/API/Runtime/Core/HAL/FOutputDeviceRedirector/index.html

And there global pointer for it called GLog (similat to GEngine and GEditor), in there you got very simple Log() function so all you need to do is:

GLog->Log("Something something");

To do simple log message

1 Like

Good to know, thanks. That will make my scripts simpler.