Is there a way to identify (at runtime) what line number a command exists on in the source file.
Like to generate an error msg and tell what line the error occurred on.
Just like the compiler does.
Right now I would have to hard code the line number in and if I insert an additional line in the source then I have to go edit all the lines from there down.
I have a function called ErrMsg() that returns the text of the error code created by various functions.
'Specting lots of errors Larry .. :)
I don't know of a way .. by the time the executable is running, the source code has disappeared in the distance. :)
Presumably, there are only specific places where you might generate an error in ErrMsg(). Could you perhaps scatter some Labels or Error comment lines around those areas, and refer the user to those. At least they wouldn't change if you add more lines of code. :)
all the best, :)
Graham
I doubt it as the program is compiled. Interpreted programs can do it.
I have the same desire, but what I do is give each error a number and keep a list of the subroutines in which they would occur.
It also helps to write error-free code ;)
As opposed to line numbers - Use the section of the code you are putting out the error as part of the message.
This is for a xml class lib I'm working on.
So...it's not my errors but the user's errors that I'm concerned about.
The lib is almost 100% pointer based so almost all my functions return "0" for okay and "> 0" for an error.
I have to do that because of the possibility of winding up with NULL pointers.
I have the ErrMsg method to call when return <> 0.
It gives a "human" description of the error made.
I know that once compiled line numbers are a done deal. I was hoping there was a $ or # directive that counld insert the line number for me while it was compiling.
I guess I'll just add an optional parameter in the ErrMsg method so the user can pass the line # in if they want some additional help in finding a bug.
Thanks for all the comments guys.
We need some "macros" like that - it would be really easy to do in the compiler - just add new keywords that return a string containing the current filename, line number, compile time/date.
In Lex, it would look something like this:
__LINE__ { yylval.intValue = current_line; return INTCONSTANT; }
__LINE__, __FILE__, __TIME__, __DATE__ are supported in all major compilers and would be very helpful.
In Aurora, I use debug code in which I indicate method and line numbers:
#ifdef DEBUG
OutputDebugStringA("Method: So and So");
OutputDebugStringA(" Line No. 332");
#endif
John,
#ifdef DEBUG
OutputDebugStringA("Method: So and So");
OutputDebugStringA(" Line No. 332");
#endif
I've used a "flag" like that for years
Point is, what happens when you insert a line of code somewhere before line #322.
That's what I was trying to get around.
And again, it's not for my programming errors but for the errors created by users of my class library.
Could you use error numbers rather than line numbers?
I realize they would be out of sequence, but you could search on the number.
NASM has built-in __LINE__ macro that returns the current line in assembly code. To use this feature in not-nasm, create a integer variable as the first local variable (can be global), then copy the line to it (compile with debug info):
sub mysub
int lineno /* always first */
_asm mov dword[ebp-4], __LINE__ + 1
_endasm
print lineno
endsub
or with a global variable:
int lineno
sub mysub
_asm mov dword[lineno], __LINE__ + 1
_endasm
print lineno
Quote from: REDEBOLT on June 12, 2007, 02:11:39 PM
Could you use error numbers rather than line numbers?
I realize they would be out of sequence, but you could search on the number.
I use error codes to return the nature of the error from a subroutine.
Problem is the subroutine could be called in 100 different places in a users application.
Sapero
I don't think your solution will work for me either.
I need the line number where my routine was CALLED from( controlled by user, not me) and not some line number inside my subroutine.
The asm stuff would have to exist as an automatic parameter to my routine but require no programming by the user.
Ok, here is what you need: declare __LINE__()
call_it_1()
call_it_2()
int a
print "press enter"
input(a)
end
sub LogTheCaller(param1 as int,param2 as int,opt linenumber = &__LINE__ as int)
Color(15, 0)
print "function LogTheCaller was called from line " , linenumber
Color(7, 0)
return
endsub
sub call_it_1()
print "in function call_it_1"
LogTheCaller(1, 2)
return
endsub
sub call_it_2()
print "in function call_it_2"
LogTheCaller(1, 2)
return
endsub
It works as expected (tested with ibasic)
Sapero
As always, you da man! ;D
Thanks!!!