mliptak
11/4/2008 2:05:00 PM
On Nov 4, 2:53 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> mliptak wrote:
> > On Nov 4, 2:21 pm, James Kanze <james.ka...@gmail.com> wrote:
> >> On Nov 4, 11:54 am, mliptak <Meht...@gmail.com> wrote:
>
> >>> I'm trying to implement logging in my application, so that
> >>> each log message has its unique identifier, e.g.
> >>> log(identifier, text)
> >>> What I want to achieve is that the compiler screams if the
> >>> log() with 'identifier' is also used in some other place in
> >>> the code which would make the 'identifier' not unique.
> >>> Is that something that can be achieved in C++?
>
> >> It depends on the type of identifier, but in general, yes,
> >> provided log is a macro. In the general case, you need a local
> >> static variable to achieve it, and the actual identifier won't
> >> be known until compile time. If identifier is text, however,
> >> you can generate it with a macro using __FILE__ and __LINE__.
>
> > What if I want to set the identifier myself, not have it generated at
> > compile time (using local static variable)?
>
> > I was also thinking of using the __FILE__ and __LINE__ but the problem
> > is this could change when the code changes, and I need to have the
> > identifier fixed for each message.
>
> I think you're still to figure out exactly what you need (i.e. the
> "requirements" part of your design). What does it mean "to have the
> identifier fixed for each message"? Do you intend to have the same
> message sprinkled throughout your code and have the same identifier
> accompany it? If the messages are unique, why nave an identifier at
> all? If the messages aren't unique and can be the same, using the
> exact location in your code to generate the identifier is a nice way
> to ensure uniqueness of them.
What I meant by this is to have the "code" associated with particular
"text" and this association never changes (which is not true if I'd be
using __FILE__ and __LINE__).
You asked why have identifier at all.. Well another requirement is to
have the "code" unique throughout the program.
Example:
// a.cc
// ok
log(code1, "text1");
log(code2, "text2");
....
// invalid - code1 already used before
log(code1, "text3");
// b.cc
// invalid - code1 already used elsewhere
log(code1, "text4");
// ok
log(code4, "text5");
>
> The presence of __FILE__ and __LINE__ is very convenient if you need
> to later find which of the millions of lines in your code caused the
> output of the message.
>
> Another way would be a global counter of sorts. You could create
> a class that when instantiated would increment some counter and when
> output would produce that counter (which it would keep as a static
> data member), but then you lose control over what counter relates to
> which line since generation of the counter can depend on the execution
> order of the constructors for those objects.
>
As I mentioned before, the disadvantage of this approach is that the
codes of messages change when the source changes.
> Go with __FILE__ and __LINE__. Trust us, we've done it before.
>
> V