identifying a specifc c line in the programm

Hello,

In my program, I log to the eeprom execution exceptions as 8 bytes records, of the structure

<epoch (4 bytes)><sketch version (1)><line of exception (2)><status (1)>

where I obtain the line from ‘LINE

This works well BUT if I edit the code and add or remove any line -including empty or comments then the line value in the eeprom is no more synced to the code, obviously!

is there a way to refer to a line not by the line number but rather by it’s essence (not sure what this would be), so that the reference becomes insensitive to such edits.

Thanks in advance for your inlighted advice

The concept of lines of code only exists in the text you submit to the compiler. When the compiler is finished with it all that remains is a stream of hex bytes.

...R

I log to the eeprom execution exceptions

You have not posted your code (HINT) but what would constitute an exception ?

guy_c:
Is there a way to refer to a line not by the line number but rather by it's essence (not sure what this would be), so that the reference becomes insensitive to such edits.

Well … you could treat a function identifier as a pointer to function and convert that to a number. That won't change if you reformat. But they will change if the code changes. C++ doesn't have the java notion of reflection.

void foo() {
  Serial.print("We are in function ");
  Serial.println((int) foo);
}

UKHeliBob:
You have not posted your code (HINT) but what would constitute an exception ?

Here are few examples of what is considered abnormal and cause a eeprom log (together with a flag stating if the machine should continue or halt or persistently halt, i.e. even after power cycling)

 else {
    report(F("failed pinging router\r\n"), LO_VERB);
    logger(__LINE__, 0x00, LOG_DONT_HALT);
  }
 else {
    report(F("failed pinging to Inet\r\n"), LO_VERB);
    logger(__LINE__, 0x00, LOG_DO_HALT);
  }
 if (!SD.begin(SD_SEL_PIN)) {
    Serial.println("Failed sd check, logging stopping");
    logger(__LINE__, 0, LOG_DO_HALT);
  }
   if (reportingIndex == SENSOR_ENTRIES) {
      if (!sensorIndexWrap) {
        logger(__LINE__, 0, LOG_PERS_HALT);
      } else reportingIndex = 0; //pers cause serious sync issue
    }

the logger writes the line and the status from the call argument and adds the epoch and the version# then it will let it will execute the specific halt request

PaulMurrayCbr:
Well … you could treat a function identifier as a pointer to function and convert that to a number. That won't change if you reformat. But they will change if the code changes. C++ doesn't have the java notion of reflection.

void foo() {

Serial.print("We are in function ");
 Serial.println((int) foo);
}

but the call to the logger are often all from the same function "loop"

the logger writes the line and the status from the call argument and adds the epoch and the version# then it will let it will execute the specific halt request

Why not allocate an error number to each error and have it log that instead ?

UKHeliBob:
Why not allocate an error number to each error and have it log that instead ?

It is a possibility
Note that the same error in different parts of the code may have distinct cause and so the specific location is / can be important

Note that the same error in different parts of the code may have distinct cause and so the specific location is / can be important

So give it a different error number so that you can identify where it occurred.

UKHeliBob:
So give it a different error number so that you can identify where it occurred.

And put the error number as a comment on the line so that you can find it with a text search.

...R

Not sure its best sw engineering practice

guy_c:
Not sure its best sw engineering practice

Using line numbers that have no meaning certainly isn't

Line # allow a direct error log to source code correspondence, why no meaning???

guy_c:
Line # allow a direct error log to source code correspondence, why no meaning???

From your original post

This works well BUT if I edit the code and add or remove any line -including empty or comments then the line value in the eeprom is no more synced to the code, obviously!

You need to identify where in the code the exception occurred but you do not need to know the line number in order to do that

Yes, true. If source changes w/o changing the version then back annotation gets dissinced and this is why i posted, to find out something as simple as it is now but yet immune to (minir) changes

Presumably these exceptions will only arise while you are debugging the program. The final version should not need to detect them.

...R

guy_c:
This works well BUT if I edit the code and add or remove any line -including empty or comments then the line value in the eeprom is no more synced to the code, obviously!

If your sketch is under version control you could log the software version. Then you could locate the line in the correct version of the file.

Robin2:
Presumably these exceptions will only arise while you are debugging the program. The final version should not need to detect them.

...R

Sure. However since I started, long ago, it evolves gets additional functionalities and I sometimes think it'll never end. That said there will always be cases / branches of if statement which are symptomatic of a logical pathology/negligence and, to my understanding, should be logged and consecutive execution be specified: e.g. I implemented i) "never mind" ii) stop and iii) stop and don't run even if power cycles. Poisoned pill in the eeprom

guy_c:
That said there will always be cases / branches of if statement which are symptomatic of a logical pathology/negligence and, to my understanding, should be logged and consecutive execution be specified: e.g. I implemented i) "never mind" ii) stop and iii) stop and don't run even if power cycles. Poisoned pill in the eeprom

I certainly won't claim to be a writer of perfect code but I have never felt the need to have "long term" debugging features built into my Arduino programs.

If I do not have enough confidence (perhaps misplaced?) in my code I don't consider it finished.

...R

johnwasser:
If your sketch is under version control you could log the software version. Then you could locate the line in the correct version of the file.

I do log the version which is derived from the sketch filename (itself a number increasing modulo 128) but when I make an edit nothing forces me to increment the file# and here starts the issue

Sure version control is the way but honestly I am lazy (or old ot both) to learn how to use github and alike and am frightened to know that google drive does not keep the history of .ino files