#pragma once

#include "CoreMinimal.h"
#include "Misc/OutputDevice.h"

class FLogCaptureOutputDevice : public FOutputDevice
{
public:
	TArray<FString> CapturedErrors;
	bool bEnabled = false;

	// If the device is marked 'CanBeUsedOnMultipleThreads,'
	// then UE_LOG will call Serialize from the current
	// thread, otherwise, it will call Serialize from the
	// logging thread.  Without this, we wouldn't be able to
	// tell whether an error is coming from the game thread.
	virtual bool CanBeUsedOnMultipleThreads() const override { return true; }

	virtual void Serialize(const TCHAR* V, ELogVerbosity::Type Verbosity, const FName& Category) override
	{
		// Only capture messages from the game thread.
		// Other threads generate noise we don't care about.
		if (!IsInGameThread() || !bEnabled) return;

		if (Verbosity == ELogVerbosity::Warning ||
			Verbosity == ELogVerbosity::Error ||
			Verbosity == ELogVerbosity::Fatal)
		{
			CapturedErrors.Add(FString(V));
		}
	}
};
