Wanting to create an error handling function, I want it to return the line without having to pass an int value for it. I've seen functions given to me in books and so forth do it, but I have no idea how to get that value.

Recommended Answers

All 6 Replies

What do you mean by "without having to pass an int value for it"? Can you give an example of the kind of output you're looking for?

There is the macro __LINE__ which gets replaced with the current line number, but it's an integer.

oh I just wanted to make a function that looks like

Function(WSTRING,WSTRING)
{....}

not

Function(int,Wstring,Wstring)
{...}

Than within the function code, have it determine the line number the function was called on and print whatever message I put into it along with the line number in a message box.

Would be helpful so I might even be able to do something like

Function(bool,wstring,wstring)
{...}

I use the hr values with the SUCEEDED() and FAILED() functions on most of my tests but these simply return a bool value and don't give me an error message, I have to type that in every time.

so than I could do something with the function above like.

Function(SUCEEDED(function that does something), wstring,wstring);

To completely encapsulate all my error checking without wasting time recreating a bunch of if statements and messagebox's unless it's a special circumstance.

>> There is the macro __LINE__ which gets replaced with the current line number, but it's an integer.

that's the recommended way to do this.Your debugger done this using something called
debug symbolic information.In other words it's a symbol , where it consists a tuple
for each line in a source file.The tuple has source file name , source line number and
the address of the line. That's how the debugger keep the information.That data is stored in a section called symbolic data.
see here.
http://en.wikipedia.org/wiki/COFF

it's nothing more than another data structure.And nothing to hack or reverse engineer that too,they are open information.If you prefer to write a debugger you
can. But its very hard believe me.


Note: In the mingw world there are tools like 'objdump' that you can extract this
information very easily into a file using command line standarded output redirection.

for a example.
command prompt> objdump your_executable.exe -d -l > symbolic_data.sym

In my machine part of the symbolic_data.sym for the executable that I'm currently
working is look like this.

_ZSt17__verify_groupingPKcjRKSs():
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2493
  401290:	55                   	push   %ebp
  401291:	89 e5                	mov    %esp,%ebp
  401293:	83 ec 28             	sub    $0x28,%esp
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2494
  401296:	8b 45 10             	mov    0x10(%ebp),%eax
  401299:	89 04 24             	mov    %eax,(%esp)
  40129c:	e8 ef fb 00 00       	call   410e90 <__ZNKSs4sizeEv>
  4012a1:	48                   	dec    %eax
  4012a2:	89 45 fc             	mov    %eax,0xfffffffc(%ebp)
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2495
  4012a5:	8b 45 0c             	mov    0xc(%ebp),%eax
  4012a8:	48                   	dec    %eax
  4012a9:	89 45 f4             	mov    %eax,0xfffffff4(%ebp)
  4012ac:	8d 45 f4             	lea    0xfffffff4(%ebp),%eax
  4012af:	89 44 24 04          	mov    %eax,0x4(%esp)
  4012b3:	8d 45 fc             	lea    0xfffffffc(%ebp),%eax
  4012b6:	89 04 24             	mov    %eax,(%esp)
  4012b9:	e8 ce 94 03 00       	call   43a78c <__ZSt3minIjERKT_S2_S2_>
  4012be:	8b 00                	mov    (%eax),%eax
  4012c0:	89 45 f8             	mov    %eax,0xfffffff8(%ebp)
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2496
  4012c3:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
  4012c6:	89 45 f0             	mov    %eax,0xfffffff0(%ebp)
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2497
  4012c9:	c6 45 ef 01          	movb   $0x1,0xffffffef(%ebp)
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2502
  4012cd:	c7 45 e8 00 00 00 00 	movl   $0x0,0xffffffe8(%ebp)
  4012d4:	8b 45 e8             	mov    0xffffffe8(%ebp),%eax
  4012d7:	3b 45 f8             	cmp    0xfffffff8(%ebp),%eax
  4012da:	73 39                	jae    401315 <__ZSt17__verify_groupingPKcjRKSs+0x85>
  4012dc:	80 7d ef 00          	cmpb   $0x0,0xffffffef(%ebp)
  4012e0:	74 33                	je     401315 <__ZSt17__verify_groupingPKcjRKSs+0x85>
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2503
  4012e2:	8b 45 f0             	mov    0xfffffff0(%ebp),%eax
  4012e5:	89 44 24 04          	mov    %eax,0x4(%esp)
  4012e9:	8b 45 10             	mov    0x10(%ebp),%eax
  4012ec:	89 04 24             	mov    %eax,(%esp)
  4012ef:	e8 cc 01 01 00       	call   4114c0 <__ZNKSsixEj>
  4012f4:	89 c1                	mov    %eax,%ecx
  4012f6:	8b 45 08             	mov    0x8(%ebp),%eax
  4012f9:	8b 55 e8             	mov    0xffffffe8(%ebp),%edx
  4012fc:	01 c2                	add    %eax,%edx
  4012fe:	0f b6 01             	movzbl (%ecx),%eax
  401301:	3a 02                	cmp    (%edx),%al
  401303:	0f 94 c0             	sete   %al
  401306:	88 45 ef             	mov    %al,0xffffffef(%ebp)
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2502
  401309:	8d 45 f0             	lea    0xfffffff0(%ebp),%eax
  40130c:	ff 08                	decl   (%eax)
  40130e:	8d 45 e8             	lea    0xffffffe8(%ebp),%eax
  401311:	ff 00                	incl   (%eax)
  401313:	eb bf                	jmp    4012d4 <__ZSt17__verify_groupingPKcjRKSs+0x44>
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2504
  401315:	83 7d f0 00          	cmpl   $0x0,0xfffffff0(%ebp)
  401319:	74 34                	je     40134f <__ZSt17__verify_groupingPKcjRKSs+0xbf>
  40131b:	80 7d ef 00          	cmpb   $0x0,0xffffffef(%ebp)
  40131f:	74 2e                	je     40134f <__ZSt17__verify_groupingPKcjRKSs+0xbf>
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2505
  401321:	8b 45 f0             	mov    0xfffffff0(%ebp),%eax
  401324:	89 44 24 04          	mov    %eax,0x4(%esp)
  401328:	8b 45 10             	mov    0x10(%ebp),%eax
  40132b:	89 04 24             	mov    %eax,(%esp)
  40132e:	e8 8d 01 01 00       	call   4114c0 <__ZNKSsixEj>
  401333:	89 c1                	mov    %eax,%ecx
  401335:	8b 45 08             	mov    0x8(%ebp),%eax
  401338:	8b 55 f8             	mov    0xfffffff8(%ebp),%edx
  40133b:	01 c2                	add    %eax,%edx
  40133d:	0f b6 01             	movzbl (%ecx),%eax
  401340:	3a 02                	cmp    (%edx),%al
  401342:	0f 94 c0             	sete   %al
  401345:	88 45 ef             	mov    %al,0xffffffef(%ebp)
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2504
  401348:	8d 45 f0             	lea    0xfffffff0(%ebp),%eax
  40134b:	ff 08                	decl   (%eax)
  40134d:	eb c6                	jmp    401315 <__ZSt17__verify_groupingPKcjRKSs+0x85>
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2508
  40134f:	c7 44 24 04 00 00 00 	movl   $0x0,0x4(%esp)
  401356:	00 
  401357:	8b 45 10             	mov    0x10(%ebp),%eax
  40135a:	89 04 24             	mov    %eax,(%esp)
  40135d:	e8 5e 01 01 00       	call   4114c0 <__ZNKSsixEj>
  401362:	89 c1                	mov    %eax,%ecx
  401364:	8b 45 08             	mov    0x8(%ebp),%eax
  401367:	8b 55 f8             	mov    0xfffffff8(%ebp),%edx
  40136a:	01 c2                	add    %eax,%edx
  40136c:	0f b6 01             	movzbl (%ecx),%eax
  40136f:	3a 02                	cmp    (%edx),%al
  401371:	7f 0c                	jg     40137f <__ZSt17__verify_groupingPKcjRKSs+0xef>
  401373:	0f b6 45 ef          	movzbl 0xffffffef(%ebp),%eax
  401377:	83 e0 01             	and    $0x1,%eax
  40137a:	88 45 e7             	mov    %al,0xffffffe7(%ebp)
  40137d:	eb 04                	jmp    401383 <__ZSt17__verify_groupingPKcjRKSs+0xf3>
  40137f:	c6 45 e7 00          	movb   $0x0,0xffffffe7(%ebp)
  401383:	0f b6 45 e7          	movzbl 0xffffffe7(%ebp),%eax
  401387:	88 45 ef             	mov    %al,0xffffffef(%ebp)
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2509
  40138a:	0f b6 45 ef          	movzbl 0xffffffef(%ebp),%eax
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2510

Then filter the lines that only contain the line , You say this is difficult.
But life is easy with open sources. Use the 'grep' tool.In windows ,you have to
download wingrep I think.
http://www.wingrep.com/

using wingrep it's very easy to parse a line like this,(usng regex).

C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/locale_facets.tcc:2510

After filtering them into a file, you can open that file in your program and
fill the data structure by reading it.

If that's my other option passing in __LINE__ everytime doesn't seem that bad, lol

Simply make a MACRO to wrap the function call:

//in MyDebugUtilityFunctions.h

void ReportError(int lineNumber,Wstring ErrorTitle,Wstring ErrorMessage)
{
  ... report the error ... 
};

#define MY_DEBUG_ERROR_REPORT(X,Y) ReportError(__LINE__,X,Y)

//say in the main.cpp, or anywhere else.
int main() {
 ...
  MY_DEBUG_ERROR_REPORT("Fatal Error!","The main function crashed here!");
 ...
};

The MACRO will ensure that the __LINE__ corresponds to the right place, and no just the line of your print function. Also, you may want to consider using the __FILE__ define as well, which is set by the compiler to be the file name of the source file or header in which it is (you would add that to the MACRO and as a parameter to your report error function).

I didn't know you could wrap function calls like that. Thanks very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.