How to pass variable LogCategory to UE_LOG?

Hi There,

I’m trying to write some helper functions which require passing around FLogCategory variables which in turn should be used somehow in the call to UE_LOG. I’m banging my head against this and can’t figure it out. Any ideas on how to achieve this?

void UPWNLogger::LogCat(const FLogCategoryBase& Kategory)
{
	// Get log category name
	FName logCatName = Kategory.GetCategoryName();


	UE_LOG(LogTemp, Warning, TEXT("%s"), *(n.ToString()));	// Prints the expected log category as a string value using LogTemp category
	UE_LOG(logCatName.ToString(), Warning, TEXT("%s"), *(n.ToString()));	// Results in error 'FLogCategorylogCatName': undeclared identifier
}

You need to declare log category first, you can read about it here:

If you want to do easy quick debug you can also use GLog->Log(FString) which is easiest way to log but bypass advance log features.

Thanks Shadowriver. The issue is not how to declare a custom log category, I have already done that and it’s working fine. My issue is that I want to write some helper methods (not macros) that in turn calls the UE_LOG macro with specifically prepared data. In order to do this I need to pass my log category to a function (as in my first post) which seems to be working, however the issue is I can’t figure out how to use that variable when calling UE_LOG.

Essentially it seems that when passing my variable “logCatName” to UE_LOG it is not evaluated, and instead the macro takes the variable name (“logCatName”) and prepends “FLogCategory” to result in the string “FLogCategorylogCatName” which cannot be resolved (as it doesn’t exist).

I have very little experience with C++ macros so I’m probably trying something stupid here, but any suggestions will be appreciated.

Ah ok :> Then it can’t be helped as those macros are made that way, you might try to look up on how UE_LOG is made and try to reproduce it, code from macro is pasted at point there are used so if you can declare your own fixed macro it should work the same way

https://github.com/EpicGames/UnrealEngine/blob/8e4560b8c22b309e73ff0ce90377742c3dfe13cc/Engine/Source/Runtime/Core/Public/Logging/LogMacros.h

just remember to make NO_LOGGING conditioning same as code above do, if you don’t do that log will embeded in to binary even if logging system is no build which might lead in to some complications. UE4 use macros for that execly because of that to control whatever log messages and or not will be build in to binary.

You might try to experiment with GLog more, there bits of category system there too: