Determining Current Function in C++. Track down this bounty hunter, you must, Obi-Wan.

I like comically verbose debug outputs. I tend to produce a lot of information in debug logs attached to applications that I write. Rarely does anyone else see the logs, but as the whole purpose of the debug logs is to track down and fix faults, the more information the better.

I found a very useful way of reporting the current function name without having to manually type the function name into the log entry.

void TestFunction()
	Logging_LogDebug("%s() Start\r\n", __FUNCTION__);

	// Perform Function Logic

	Logging_LogDebug("%s() End\r\n", __FUNCTION__);


I’ve used Logging_LogDebug (similar to Logging_LogSystem from my previous post here). This function works in the same way as printf. As you can see though, I’m passing the __FUNCTION__ macro, which will be replaced by the compiler with the actual function name, in this case TestFunction. Handy. There are numerous other macros that can be used as well, including __LINE__, __FILE__, __DATE__, __TIME__. These macros provide you with the current line number in the code, the current code file, and the date/time of the current code file being compiled. These can be extremely useful to include in verbose debug outputs. This code will work on Linux systems as well.

As per the example above, I tend to include these lines in most of the functions I create. The Logging_LogDebug function will only output information when debug mode is enabled within my application. It is very handy being able to track the flow of an application with functions and macros like these. You can also use functions similar to this to generate flow data into a database or structured file so that you can track the performance of various functions within your application. A bit off topic, but it’s a good process to follow.

~ Mike

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s