Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    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!
    https://wiki.unrealengine.com/Logs,_...B_Code_For_You

    ~~~

    Pics

    Click image for larger version

Name:	JoyStringCurrentClassFuncLineNumber2.jpg
Views:	3
Size:	323.0 KB
ID:	1143250

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

    Code:
    //~~~ Tick ~~~
    void AEVCoreDefense::Tick(float DeltaTime)
    {
    	Super::Tick(DeltaTime);
    	//~~~~~~~~~~~~~
    
    	VSCREENMSG("Got Here!");  //Class and line number get printed for you! ♥ Rama
    }

    Click image for larger version

Name:	JoyStringCurrentClassFuncLineNumber3.jpg
Views:	2
Size:	373.8 KB
ID:	1143251

    ~~~

    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:

    Code:
    // Joy Class Func Line
    #include "JoyCurrentClassFuncLine.h"
    Code:
    /*
    	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
    Last edited by Rama; 02-19-2015, 03:21 AM.
    100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

    UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

    Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

    #2
    This is realy cool!
    Thanks for sharing Rama.

    Comment


      #3
      I can see this being hella useful! Nice one Rama

      Comment


        #4
        Very handy

        Comment


          #5
          Nice work Rama, Saves a time!
          Abatron Development Thread
          www.abatrongame.com
          Steam Store

          Comment


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



            Rama
            100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

            UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

            Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

            Comment


              #7
              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);


              Code:
              #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
              100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

              UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

              Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

              Comment


                #8
                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!

                Code:
                //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)
                Last edited by Rama; 01-28-2015, 07:50 PM.
                100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

                UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

                Comment


                  #9
                  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:

                  Code:
                  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,_...During_Runtime

                  Code:
                  //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
                  Last edited by Rama; 02-01-2015, 09:24 AM.
                  100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

                  UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                  Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

                  Comment


                    #10
                    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
                    100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

                    UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                    Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

                    Comment


                      #11
                      Originally posted by Rama View Post
                      Powerful Debugging Tools For You

                      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

                      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

                      https://wiki.unrealengine.com/Log_Ma...ode_and_Colour

                      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

                      Comment


                        #12
                        Originally posted by GeekyPayback View Post
                        Out of interest, why do it as a separate header file?
                        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

                        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!

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

                        Code:
                        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
                        Last edited by Rama; 02-19-2015, 03:30 AM.
                        100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

                        UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                        Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

                        Comment


                          #13
                          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
                          100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

                          UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                          Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

                          Comment


                            #14
                            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

                            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
                            100+ UE4 C++ Tutorials on the UE4 Code Wiki, including UE4 Multi-Threading!

                            UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

                            Visit www.ue4code.com to see lots of videos about my C++ Creations! ♥ Rama

                            Comment


                              #15
                              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.

                              Code:
                              #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

                              Comment

                              Working...
                              X