Download

New Wiki, How to Get a UE4 FString of Calling Class, Function, and Line Number -Rama

Dear Community,

I’ve just posted a new wiki!

I am giving you easy-to-use pre-processor commands to get a UE4 String that tells you the Class Name, Function Name, and Line Number of the calling code!

Logs, Printing the Class Name, Function Name, and Line Number of your Calling Code!


**Pics**

![a51b4eb12a773cd47ef504c3015401ed5a1222f7.jpeg|1280x960](upload://nyBhSL7AeJYRaiuAA8hU6eu26hN.jpeg)

After you get my .h file below, the code for the above picture is this!



```


//~~~ Tick ~~~
void AEVCoreDefense::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//~~~~~~~~~~~~~

	VSCREENMSG("Got Here!");  //Class and line number get printed for you! ♥ Rama
}


```




![76592038c1a622532653d3516d343bb88787467e.jpeg|1280x960](upload://gSXi9wvKFnU8eJKryUDfNDEi8eG.jpeg)

Code

Here is the entire file you can #include in your code base!

I made this a file called JoyCurrentClassFuncLine.h

So you would then do this somewhere at the top of one of your core classes:



// Joy Class Func Line
**#include "JoyCurrentClassFuncLine.h"**




**/*
	Joy String 
		Current Class, File, and Line Number!
			by Rama
			
	PreProcessor commands to get 
		a. Class name
		b. Function Name
		c. Line number 
		d. Function Signature (including parameters)
		
	Gives you a UE4 FString anywhere in your code that these macros are used!
	
	Ex: 
		You can use JOYSTR_CUR_CLASS anywhere to get a UE4 FString back telling you 
		what the current class is where you called this macro!
	
	Ex:
		This macro prints the class and line along with the message of your choosing!
		VSCREENMSG("Have fun today!");
	<3  Rama
*/**
#pragma once

**//Current Class Name + Function Name where this is called!**
#define JOYSTR_CUR_CLASS_FUNC (FString(__FUNCTION__))

**//Current Class where this is called!**
#define JOYSTR_CUR_CLASS (FString(__FUNCTION__).Left(FString(__FUNCTION__).Find(TEXT(":"))) )

**//Current Function Name where this is called!**
#define JOYSTR_CUR_FUNC (FString(__FUNCTION__).Right(FString(__FUNCTION__).Len() - FString(__FUNCTION__).Find(TEXT("::")) - 2 ))
  
**//Current Line Number in the code where this is called!**
#define JOYSTR_CUR_LINE  (FString::FromInt(__LINE__))

**//Current Class and Line Number where this is called!**
#define JOYSTR_CUR_CLASS_LINE (JOYSTR_CUR_CLASS + "(" + JOYSTR_CUR_LINE + ")")
  
**//Current Function Signature where this is called!**
#define JOYSTR_CUR_FUNCSIG (FString(__FUNCSIG__))


**//Victory Screen Message
// 	Gives you the Class name and exact line number where you print a message to yourself!**
#define VSCREENMSG(Param1) (GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1)) )

#define VSCREENMSG2(Param1,Param2) (GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1 + " " + Param2)) )

#define VSCREENMSGF(Param1,Param2) (GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1 + " " + FString::SanitizeFloat(Param2))) )

//UE_LOG Messages!
#define V_LOG(LogCat, Param1) 		        UE_LOG(LogCat,Warning,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))

#define V_LOG2(LogCat, Param1,Param2) 	UE_LOG(LogCat,Warning,TEXT("%s: %s %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1),*FString(Param2))

#define V_LOGF(LogCat, Param1,Param2) 	UE_LOG(LogCat,Warning,TEXT("%s: %s %f"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1),Param2)

#define V_LOGM(LogCat, FormatString , ...) UE_LOG(LogCat,Warning,TEXT("%s: %s"), 	*JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ) )



Now you have additional tools to print exact class/function/line number debugging information for yourself during runtime!

Rama

This is realy cool! :slight_smile:
Thanks for sharing Rama.

I can see this being hella useful! Nice one Rama :slight_smile:

Very handy :slight_smile:

Nice work Rama, Saves a time!

Yay! I am glad you are all enjoying the code / new debugging tools!

:slight_smile:

Rama

Two More Pre Processor Commands!

I’ve added two more functions to this header file for you!

VSCREEMSG2 takes in two strings, one of which might be a vector for example, using SomeVector.ToString()

VSCREEMSGF lets you print a labeled flow, ex: VSCREEMSGF(“Player Velocity Z”,GetCharacterMovement()->Velocity.Z);



#define VSCREENMSG2(Param1,Param2) (GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1 + " " + Param2)) )

#define VSCREENMSGF(Param1,Param2) (GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(JOYSTR_CUR_CLASS_LINE + ": " + Param1 + " " + FString::SanitizeFloat(Param2))) )


_


Enjoy!

Rama

Three More PreProcessor Commands, UE LOGs!

I added three more commands to my header file in original post!

These UE_LOG versions print out the class and line number in addition to your string message.

2 variants offered:

a. Print a second string, which could be a FVector::ToString for example

b. Send a float value to log along with a string label.

Enjoy!



//UE_LOG Messages!
#define V_LOG(Param1) 	      UE_LOG(Joy,Warning,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define V_LOG2(Param1,Param2) UE_LOG(Joy,Warning,TEXT("%s: %s %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1),*FString(Param2))
#define V_LOGF(Param1,Param2) UE_LOG(Joy,Warning,TEXT("%s: %s %f"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1),Param2)


C++ Debugging Power For You

I am giving you the whole .h file to be able to automatically print out the actual line number, function name, and class name wherever you put my macros in your code!

Great for debugging!

Great for printing screen messages without having to track them down later and figure out where they are coming from!

Also great for testing execution order, and whether certain parts of code are executing at all, without having to write complicated messages.

You could even just use this:



VSCREENMSG("Got here!");


many places within a section of code you are debugging, and my macro will give print the line number and class name automatically, so you know how far the execution chain is getting!


**UE_LOG Support**

I also recently added UE_LOG macro versions so you can easily send class name and line number and your message to the log file!

Make sure to replace "Joy" with the name of your own log :)

More info on logs here:

**UE4 Wiki Link**
https://wiki.unrealengine.com/Logs,_Printing_Messages_To_Yourself_During_Runtime



```


//UE_LOG Messages!
#define V_LOG(Param1) 	      UE_LOG(Joy,Warning,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1))
#define V_LOG2(Param1,Param2) UE_LOG(Joy,Warning,TEXT("%s: %s %s"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1),*FString(Param2))
#define V_LOGF(Param1,Param2) UE_LOG(Joy,Warning,TEXT("%s: %s %f"), *JOYSTR_CUR_CLASS_LINE, *FString(Param1),Param2)


```



Enjoy!

Rama

Powerful Debugging Tools For You

I’ve been using these macros I share in the original post since I originally made them, and I am finding them very useful!

Using the .h file I share with you in the original post of this thread, you can easily output messages to yourself that tell you

  1. the class where you printed the message

  2. the line number on which you printed the message

  3. the** name of the function** in which you printed the message!

  4. the function signature which contains the printed message

And I exposed this to the screen debug messages as well as to the UE_LOG system!

Enjoy!

Rama

Out of interest, why do it as a separate header file? I can’t see the benefit of your approach over the wiki version, other than it provides the line number

If you follow the wiki, you don’t need to include a .h, and it deals with parameter passing so you dont need multiple versions. Once I figure out which part of your script is doing the line numbers I’ll update the wiki later :slight_smile:

Personal preference really, I like to keep code organized via header files and you could certainly #include my .h file in YourProject.h

It makes my code very easy to include/remove any time since it is in its own file.

I also find it easier to update/modify my code when it is organized into multiple files, everything is properly contained :slight_smile:

It is my personal preference to provide the multiple versions that receive different parameters, I just find it faster to type within my code base.

All of this is personal preference, I hope you find what setup you like best!


**V_LOGM For You**

I added a new macro for those who prefer arbitrary number of inputs!



```


#define V_LOGM(LogCat, FormatString , ...) UE_LOG(LogCat,Warning,TEXT("%s: %s"), *JOYSTR_CUR_CLASS_LINE, *FString::Printf(TEXT(FormatString), ##__VA_ARGS__ ) )

_


```



**Example usage:**



```


int32 Health = 100;
float ArmorPct = 52.33;
FVector Location(33,12,1);
	
V_LOGM(Joy, "Health: %d, ArmorPct: %f, Loc: %s",  Health, ArmorPct, *Location.ToString());


```


LogCat

I also updated all the V_LOG macros to include a LogCat rather than using an arbitrary one.


**Class and Line Number**

Note that all V_LOG macros automatically include the **class and line number** where V_LOG was called, along with your message!


See original post for the whole .h file!

:)

Rama

Powerful Debugging Tools For You

Update: I can honestly say this header file I am giving you really helps with debugging! It’s just so much faster to type debug messages / UE_LOG messages because I dont have to include the function name / class manually to ensure that I can find it and remove it later!

Check out this complimentary offering if you want to speed up your game logic debugging times!

I’ve been using these macros I share in the original post since I originally made them, and I am finding them very useful!

Using the .h file I share with you in the original post of this thread, you can easily output messages to yourself that tell you

  1. the class where you printed the message

  2. the line number on which you printed the message

  3. the** name of the function** in which you printed the message!

  4. the function signature which contains the printed message

And I exposed this to the screen debug messages as well as to the UE_LOG system!

Enjoy!

Rama

Powerful Debugging Tools For You

I continue to find printing screen messages with the line number built exceptionally useful, so I can freely print stuff to the screen all over my code base and always know where it is to get rid of it later :slight_smile:

Using the .h file I share with you in the original post of this thread, you can easily output messages to yourself that tell you

  1. the class where you printed the message

  2. the line number on which you printed the message

  3. the name of the function in which you printed the message!

  4. the function signature which contains the printed message

And I exposed this to the screen debug messages as well as to the UE_LOG system!

Enjoy!

Rama

Heres mine.

Theres only one way to call it. It will always go to the screen and log. The reason being there are already perfectly good functions for going straight to log, and writing to screen is a bit of a faff, and if you write to screen with this function you also get a logged copy to review later.

Also I always write code intending for multiplayer, so this version of the log lets me know where it came from… Its all basically a mash up of the wiki and Rama’s suggestion.



#define NETMODE_WORLD (((GEngine == NULL) || (GetWorld() == NULL)) ? TEXT("") \
        : (GEngine->GetNetMode(GetWorld()) == NM_Client) ? TEXT("[Client] ") \
        : (GEngine->GetNetMode(GetWorld()) == NM_ListenServer) ? TEXT("[ListenServer] ") \
        : (GEngine->GetNetMode(GetWorld()) == NM_DedicatedServer) ? TEXT("[DedicatedServer] ") \
        : TEXT("[Standalone] "))


#if _MSC_VER
#define CLASS_FUNC_NAME TEXT(__FUNCTION__)
#define CURR_LINE		TEXT(__LINE__)

#else // FIXME - GCC?
#define CLASS_FUNC_NAME TEXT(__func__)
#define CURR_LINE		TEXT(__line__)
#endif

#define FFLOG(Format, ...) \
{ \
    SET_WARN_COLOR( COLOR_CYAN );\
    FString Msg = FString::Printf(TEXT(Format), ##__VA_ARGS__ ); \
    UE_LOG( LogFF, Log, TEXT("%s%s() : %s"), NETMODE_WORLD, CLASS_FUNC_NAME, *Msg);\
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("%s %s: %s"), NETMODE_WORLD, CLASS_FUNC_NAME,  *Msg));\
    CLEAR_WARN_COLOR();\
}

#endif


I tried to add the line number from Rama’s code but it kept giving me an error message so I gave up and got on with writing a game :slight_smile:

Print Screen Messages from C++, Automatically Including Class / Line Number!

a51b4eb12a773cd47ef504c3015401ed5a1222f7.jpeg

Pic Explanation

In this pic, AEVCoreDefense is my C++ Class, and the line number of the screen message is currently line 93 (automatically updates as your class grows in size!)

Using the .h file I share with you in the original post of this thread, you can easily output messages to yourself that tell you

  1. the class where you printed the message

  2. the line number on which you printed the message

  3. the name of the function in which you printed the message!

  4. the function signature which contains the printed message

And I exposed this to the screen debug messages as well as to the UE_LOG system!

Enjoy!

Rama

Thanks this is awesome!

You’re welcome Sahkan!

:slight_smile:

Rama