Download

Macro to prefix UE_LOG string

I want:



DEBUG("Debug message here: %s %i etc", *myfstring, 3)


to expand to:



UE_LOG(LogTemp, Warning, "[MyClassName] Debug message here: %s %i etc", *myfstring, 3);


So I’m essentially trying to inject a prefix of [MyClassName] before the string. I’ve tried so many different combinations of things, but it’s just quoting the wrong things instead of evaluating. I really don’t want to do this, but I even tried putting a %s before the string to try to inject the prefix that way with the way UE_LOG works, but I couldn’t figure that out either.

One of the many things I’ve tried:



#define DEBUG_NAME MyClassName
#define DEBUG(x, y) x, TEXT(""##DEBUG_NAME##"]"), y)


Usage:



DEBUG("%sDebug message here: %s %i etc", *myfstring, 3) // %s at start is ugly; should be handled behind the scenes


There’s a few options to try here…

https://stackoverflow.com/questions/…ngth-arguments

Maybe try something like…


#define DEBUG(...) UE_LOG( LogTemp, Warning, __VA_ARGS__ )

Solved:



#define DEBUG_NAME MyClassName
#define DEBUG_NAME_PREFIX_1(x) #x
#define DEBUG_NAME_PREFIX_2(x) DEBUG_NAME_PREFIX_1(x)
#define DEBUG(str, ...) UE_LOG(LogTemp, Warning, TEXT(DEBUG_NAME_PREFIX_2([DEBUG_NAME] )" "str), __VA_ARGS__)


Usage:



DEBUG("Debugging %s and %i", *myfstring, myint);


Expands to:



UE_LOG(LogTemp, Warning, TEXT("[MyClassName] Debugging %s and %i"), *myfstring, myint);


Example result:



[MyClassName] Debugging abc and 3


Keep in mind that sooner or later, you’ll want to use different logging categories, and verbosity levels. There’s a reason why we have it this way. Every post you make is basically “how to work outside of the Unreal Engine environment using Unreal Engine”. Don’t fight the engine, it’s not worth it.

Doing this has saved me a lot of space and makes my code neater. The engine is too rigid - that’s why the learning curve is so brutal - and I need to figure out ways to make it more flexible because it seems flashy new rendering is all the developers seem to care about, not the basics.