r/UnityHelp • • Sep 25 '21

Easier way to Print Class:Method name in Debug.Log

In C/C++, whenever I print a debug statement, I always prepend my text with function/line no. for instance

printf( "%s:%d: error message\n", __FUNCTION__, __LINE__ );

Now, I've found a way to do something similar in Unity/C#and that is

string func =  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + ":" + System.Reflection.MethodBase.GetCurrentMethod().Name;

Debug.Log( func + " error message" );

My question is, is there an easier way to do this rather than declare "string func = " at the beginning of each function? I've tried looking into user #define in Unity, but as far as I can tell (and I could be wrong), it doesn't really let you define a body for your definition

so Unity allows you to do this;

#define TAG

but not

#define TAG insert custom C# code here;

for instance, can I even do something like this?

#define PRINT_HERE  Debug.Log( "Over Here" );

Also, I apologize for bombarding this site with back-to-back questions. Thanks in advance for your help.

4 Upvotes

2 comments sorted by

1

u/Bonejob Code Guru Sep 26 '21

I can't think of an easier way to do it. You could in theory create a global function and modify the base class/template so this is a property of all objects.

1

u/cone5000 Nov 16 '21
Debug.Log(nameof(YourMethod()));