Is it possible, and if so, how would you make error output include the last couple lines of code? I know about GNU g++'s support for __LINE__
and __FUNC__
but those only give the line number as an int and the function name as a string - I'd like to dump the function that failed's code or at least the last several commands executed by the program.
I was thinking maybe write a m4 script that would parse through the code base and inject string structures of the functions they're put in (excluding themselves, of course).
Any other/better ideas?
Do not change your code at all. Use __LINE__
and __FUNC__
and post process the error output. That is, run your program as my-prog > $(tty) 2>&1 | post-process
where post-process
has access to your code base and can generate the desired text. You might want to modify the code slightly by adding tags to make it easier for post-process to find the function name and line number.