# Passing formatted string to a macro

**URL:** <https://forums.unrealengine.com/t/passing-formatted-string-to-a-macro/436695>\
**Category:** UI\
**Tags:** logging, text, macros, unreal-engine, CPP, question\
**Created:** [October 30, 2018, 10:43am UTC](https://forums.unrealengine.com/t/passing-formatted-string-to-a-macro/436695 "2018-10-30T10:43:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![OmerK](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/omerk/32/467632_2.png) [@OmerK](https://forums.unrealengine.com/u/OmerK)\
**Post date:** [October 30, 2018, 10:43am UTC](https://forums.unrealengine.com/t/passing-formatted-string-to-a-macro/436695/1 "2018-10-30T10:43:20Z")

</div>

hello,  
I want to be able to write my own logs like this :

```
MY_LOG(LogDebug, Verbose, (TEXT("Item named %s has spawned"), *Name.ToString()))

```

and here is my macro:

```
#define MY_LOG(CategoryName,Verbosity,Message, ...)	
UE_LOG(CategoryName,Verbosity,TEXT("%s [%s]: %s"),*GET_CLASSNAME_WITH_FUNCTION,*GET_LINE_NUMBER,*FString(Message),## __VA_ARGS__ )    

```

i added the “…” and “## **VA\_ARGS** ” because i received error that my macro has too many arguments.  
the text that gets logged is “Item named %s has spawned” (not formatted with the value)  
is there a way to pass a formatted string to the macro? i.e.

```
auto formattedText= FORMAT(TEXT("Item named %s has spawned"), *Name.ToString())    
 MY_LOG(LogDebug, Verbose,formattedText)

```

or even make it work like in the first line of code above?

---

<div class="post-metadata">

**Author:** ![Brian\_Johnstone](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Brian\_Johnstone](https://forums.unrealengine.com/u/Brian_Johnstone)\
**Post date:** [October 30, 2018, 7:11pm UTC](https://forums.unrealengine.com/t/passing-formatted-string-to-a-macro/436695/2 "2018-10-30T19:11:55Z")

</div>

You can actually see exactly what unreal’s log macro is doing in LogMacros.h. You could take that code and do some string manipulation of Format inside your macro to preprend the extra info you want before moving ahead with the rest of the normal logging functionality.

---

<div class="post-metadata">

**Author:** ![OmerK](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/omerk/32/467632_2.png) [@OmerK](https://forums.unrealengine.com/u/OmerK)\
**Post date:** [October 30, 2018, 8:13pm UTC](https://forums.unrealengine.com/t/passing-formatted-string-to-a-macro/436695/3 "2018-10-30T20:13:58Z")

</div>

Thank you for replying,  
yeah i already tried to dive into the UE\_LOG macro but i saw it calls for other methods which were way too complex for me to understand

---

<div class="post-metadata">

**Author:** ![OmerK](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/omerk/32/467632_2.png) [@OmerK](https://forums.unrealengine.com/u/OmerK)\
**Post date:** [November 1, 2018, 5:39pm UTC](https://forums.unrealengine.com/t/passing-formatted-string-to-a-macro/436695/4 "2018-11-01T17:39:05Z")

</div>

Well… i didnt find the solution i was hoping for, but for anyone trying to figure out how to save formatted string into variables, i ended up using this:

```
MY_LOG(LogDebug, Verbose,FString::Printf( TEXT("Actor named %s has spawned"), *Name.ToString()))
```

---

<div class="post-metadata">

**Author:** ![brandav](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@brandav](https://forums.unrealengine.com/u/brandav)\
**Post date:** [July 19, 2019, 8:48pm UTC](https://forums.unrealengine.com/t/passing-formatted-string-to-a-macro/436695/5 "2019-07-19T20:48:51Z")

</div>

You can just do:

```
UE_LOG(LogTemp, Warning, L"Actor named %s has spawned", *Name.ToString());

```

Or in your case:

```
#define MY_LOG(CategoryName,Verbosity,Message, ...) UE_LOG(CategoryName, Verbosity, Message, ## __VA_ARGS__ )

MY_LOG(LogDebug, Verbose, L"Actor named %s has spawned", *Name.ToString());
```
