Debug with __LINE__ and show TAB name?

I use a snippet of code to show which line I'm on in my code for debug purposes - and it works very well:

void printDebugLine(uint16_t lineNo){
  #ifdef DEBUG  
      Serial.print(F("We are at LINE: "));

It works using the following call:


It fails to be informative however when multiple tabs are used as all I get is line xxx where there are many lines with this number spread across many tabs; Is there a similar call I can use that returns the name of the tab where the printDebugLine(LINE) originates?

Try __FILE__


Just in case it wasn't clear from DrDiettrich's answer, each of the tabs shown in the Arduino IDE are actually a file. The IDE hides the file extension when it is .ino. For example, a tab named "Blink" is a file named "Blink.ino".

If you don't need the full path of __FILE__ you can remove it with a macro.

#define __NAME__ (strrchr(__FILE__,'\\')?strrchr(__FILE__,'\\')+1:__FILE__)

Use __NAME__

1 Like

That is very helpful, thank you Chris - I will build a little code around that and post it up for comment/use.


// Strip path details from the FILE information, show only the file NAME
#define __NAME__ (strrchr(__FILE__,'\\') ? strrchr(__FILE__,'\\')+1 : __FILE__)

Use this FUNCTION:

 * Handle Serial printing of line/tab info for debugging 
void printDebugLine(uint16_t lineNo, const char* FileName ){
  #ifdef DEBUG
      Serial.print(F("We are at LINE: "));
      Serial.print(F(" in TAB: "));

and call the Debug info once DEBUG is invoked by inserting this line strategically where needed:

printDebugLine(__LINE__, __NAME__);