Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function call identifier

6 views
Skip to first unread message

Dmitry

unread,
Jul 8, 2009, 10:49:57 PM7/8/09
to
HI,

Consider the following code:

void Foo() {
......
LOG_ERROR("I'm error 1") // call 1
.....
LOG_ERROR("I'm error 2") // call 2
.....
}

LOG_ERROR() is a macro. LOG_ERROR() should print string identifying it
in code, while the assumption is that code can change, but A::Foo()
will remain unchanged. The identifier should retain while code
changes.

This can be solved by adding error code as argument to LOG_ERROR(),
but we want to remove from the programmer the burden to manage error
codes.

Using __LINE__ is not an answer, since Foo() can move from build to
build.

Therefore I thought about identifying LOG_ERROR() relative to start of
Foo():
a. Identify by file name (__FILE__) + function name (__FUNCTION__) +
line number of LOG_ERROR() relative to Foo() start.
b. Identify by file name (__FILE__) + function name (__FUNCTION__) +
LOG_ERROR() call number in Foo().

The solution should be work with VC++ 2008 and g++ 4.1.1 at least.

Unfortunately I don't find a reprocess directive/macro solving it.

Any ideas?

Thanks
Dima

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

ldh

unread,
Jul 9, 2009, 9:41:53 AM7/9/09
to
{ Please do not top-post in this group; see the FAQ. TIA., -mod }

I don't think you can get exactly what you want but this might be
tenable:

#define ENABLE_LOG_ERROR static const int LOG_ERROR_start_line =
__LINE__
#define LOG_ERROR(s) cerr << "error #" << (__LINE__ -
LOG_ERROR_start_line) << " in " << __func__ << ": " << s << endl

void Foo() {
ENABLE_LOG_ERROR;
//...
LOG_ERROR("error 1");
int i;
LOG_ERROR("error 2");
}

On Jul 8, 10:49 pm, Dmitry <dim...@gmail.com> wrote:
> HI,
>
> Consider the following code:
>
> void Foo() {
> ......
> LOG_ERROR("I'm error 1") // call 1
> .....
> LOG_ERROR("I'm error 2") // call 2
> .....
>
> }
>
> LOG_ERROR() is a macro. LOG_ERROR() should print string identifying it
> in code, while the assumption is that code can change, but A::Foo()
> will remain unchanged. The identifier should retain while code
> changes.

Dmitry

unread,
Jul 9, 2009, 3:17:02 PM7/9/09
to

Hi,

Nice idea. I was considering it too. The problem is that I'll force
user to write ENABLE_LOG_ERROR in start of each function containing
LOG_ERROR() and there're many such functions. I'm looking for less
intrusive solution.

Thanks
Dima

GMan

unread,
Jul 10, 2009, 6:08:38 PM7/10/09
to

I have solved your problem on StackOverflow, where you also asked it:
http://stackoverflow.com/questions/1108186/c-function-call-identifier/110830
7#1108307

(Sorry if linking to other sites is bad etiquette, I don't Usenet much)

0 new messages