Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While the question is about ways to generate unique line identifiers within a function for logging purposes, I'm going to take a step back and look at the actual problem to be solved: How to generate log output which can easily identify the source code line without putting the burden on the writer of the code.</p> <p>Let's assume you are embedding a unique build version in each release of your program (which is a good idea in general). Let's also assume you are using a source code control mechanism which keeps the history of your source code (which is also a very good idea to be doing anyway) and which can present you with the source as it was for any requested build version of the program.</p> <p>If those assumptions hold true, then a solution is to have your program write it's current version into the log file. Then each individual logging entry can simply record the line number via <code>__LINE__</code>.</p> <p>Thus, when someone needs to use the log: they can look at the version number in the log, grab the corresponding source from the source code control repository, and use the line numbers from the log to go to the proper source lines. This puts a bit more burden on the person using the log output. However, if logged code depends on or is influenced by other code which could change from version to version, then the historical state of the source code might be required anyway.</p> <p>Furthermore, a benefit of working this way is that it removes to need to assume that any given function will remain unchanged, as was originally part of the question. So this method has a much broader application.</p> <hr> <p>As far as implementation goes, you could either log the program version when the program starts up or you could make the logging macro include it in each entry.</p> <p>If the program version is normally stored somewhere not easily accessible in normal source code, then you could create a pre-build step which would extract the version and write it into a simple version.h file as a #define or const string. Then the logging code or macro could automatically use that to always output the current version of the program.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload