To get file details automatically into all my sketches I've been using the code below, (sourced from a thread a year or so ago that I cannot now find). It's been very handy, especially when running the sketch away from the PC. However on a few occasions it has caused obscure issues. And today I've had such weird behaviour stemming from it that I'm looking for alternatives.
However, reading this two year old thread https://forum.arduino.cc/t/sketch-name-as-preprocessor-value/116152/14
I'm coming to the conclusion that it's probably not a good idea. But before I abandon it and renew my resolve to add the sketch name manually, are there any newer robust methods anyone can recommend please?
// Code to show filename, date & compilation time
const char * compileFilename = __FILE__;
Serial.begin(115200);
delay(200);
Serial.println(F("============================================"));
Serial.println( F("Code running comes from file ") );
Serial.print("Full path : ");
Serial.println(compileFilename);
const char * lastSeparator = strrchr(compileFilename, '\\');
if (lastSeparator != nullptr)
{
Serial.print("File name : ");
Serial.println(lastSeparator);
}
Serial.print( F("Compiled: ") );
Serial.print( F(__DATE__) );
Serial.print( F(" ") );
Serial.println( F(__TIME__) );
Serial.println();
I did not find anything in the cited topic, except for the reasoning that the line can occupy some part of the memory.
This may only matter on older AVR boards, and even then not in every project. For most other boards, this point is not significant.
Posts 5 & 7 were negative about it but in addition
As mentioned I've had issues before
I went on to read various other threads and although I didn't follow some of the complex code I got the impression that all were demanding on resources
However the main reason is that the behaviour today (endlessly repeating the file details, never getting even to the end of the setup() section ) was so obscure/illogical that it's eroded my confidence.
I'm currently working on a compromise solution. I'll use my program 'Macro Express Pro' (MEP) to write a script that on using a hotkey will automatically paste the filename at the text cursor. It won't include the path but that's relatively unimportant.
Unfortunately MEP needs to be installed, or I'd share it here. But it's very simple and the manual steps needed are:
Click the IDE's top right arrow icon to display its menu
Click the Rename item
Ctrl + C to copy the name
Esc to close the Rename box and menu
Position the text cursor (already done with my macro); typically after
Serial.begin(115200); delay(200); in my case.
Although it's not an issue I want to spend much more time on, I am certainly keen to know what caused it. But unfortunately I pressed on with editing the sketch for my project and cannot now reproduce the exact original. After a couple of hours the closest I can get is shown in the minimal example below. My guess is some conflict with the DFR library (which I've used successfully in several large projects).
I cannot faithfully copy/paste the monitor output. The following few lines are all I can capture. I think the 'inverted ?' character is as far as the copy gets. To show the indefinite repetition, a screenshot is attached.
======== Setup started ===============
20:22:38.164 ->
20:22:38.164 -> Code running comes from file
20:22:38.164 -> Full path : C:\Users\terry\Dropbox\Electronics\Arduino\Arduino PROJECTS\Sleep Fader Proj\TryingReproduce\TryingReproduce.ino
20:22:38.164 -> File
For the time being I've removed the file info code. Even though I accept it may not be the root cause of the bizarre behaviour (which prompted me to post looking for alternatives), it does consume significant resource. FWIW the last comparison I did was:
With/without it included: Bytes of flash memory 9206 v 7224; Global variables 727 (35%) v 539 (26%)
I'll return to this topic again when I've finshed the current project, but meanwhile welcome any insights.