-
Ways to insert unique ID into a logging function
I'm writing a logging function for my service app and I need some way to come up with a unique ID to use in it. Here's what I mean. Say, I wrote a function:
Code:
void ReportIntoLog(UINT nUniqueID)
{
//INFO: This is not my actual function. I'm giving it here to show the concept
CString s;
s.Format(_T("Time: %s, Error ID: %d"),
COleDateTime::GetCurrentTime().Format().GetString(),
nUniqueID);
RawWriteIntoLogFile(s);
}
And then, say if I have a situation I'd like to report in the log:
Code:
MY_BIG_ARRAY* pArray = new(std::nothrow) MY_BIG_ARRAY;
if(Array)
{
//Work with array
}
else
{
ReportIntoLog(1);
}
OK, now my question. Obviously, I will need to use unique ID each time ReportIntoLog is called. First, I was trying to come up with sequential IDs manually as I was inserting ReportIntoLog into my service, but it soon became apparent that it is quite challenging when the number of times ReportIntoLog was present in the projects went over 100. So, I was wondering, is there any way to make compiler insert a unique ID for me? I don't care what is the value of that ID as long as it's unique and doesn't change from build to build. My idea was to use __LINE__, but when I change the code that value may change as well (in regards to previous builds/versions of the service).
-
Re: Ways to insert unique ID into a logging function
What is the purpose of the UniqueID? In your example above, does the uniqueId correlate to the out of memory failure for the array?
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
Arjay
What is the purpose of the UniqueID? In your example above, does the uniqueId correlate to the out of memory failure for the array?
No, don't take my example above literally. The unique ID is simply the means for me to find the location in the source code. For instance, a client sends me their log and I see errors #126 and 156 in it. That way I can locate what actually happened in the source code.
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
So, I was wondering, is there any way to make compiler insert a unique ID for me?
Have you considered calling the API functions that generate GUID's? That is what is usually done if a unique ID for any purpose needs to be generated.
A GUID is virtually guaranteed to be unique (virtually meaning that the chance of generating duplicate GUID's is astronomically small).
Regards,
Paul McKenzie
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
Paul McKenzie
Have you considered calling the API functions that generate GUID's?
It's a good idea about GUIDs. The API wouldn't work though, because those IDs have to be hard coded in the source code. I'm picturing it as a separate app that will generate a GUID after a button click and place it onto the clipboard.
That actually gave me an idea. I can use something smaller for an ID (the problem with GUID is that it's too unwieldy to operate with). The question is, what is easier: Use a piece of paper to write down those IDs in sequential order as I add them, or write a small APP that will keep track of them and dispense them one at a time :)
Still, I believe there's nothing in the precompiler that can do this for me, is there?
-
Re: Ways to insert unique ID into a logging function
Do whatever way man, try sequencing classes, then may be try doing 1.1, 1.2 for the functions.
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
Still, I believe there's nothing in the precompiler that can do this for me, is there?
as an alternative, you can pass the CRC (or some other hash function) of __FUNCDNAME__, eventually decorated with a function-local id to disambiguate ReportIntoLog calls occurring in the same function ( of course, the acceptability of this solution depends on your criteria to match ReportIntoLog calls to their respective ids ... which is not entirely clear from your problem description )
-
Re: Ways to insert unique ID into a logging function
-
Re: Ways to insert unique ID into a logging function
I used to have a string location in my error locs, e. g.
ReportIntoLog("MyClass::myFunction begin", ...);
That is easy to find in the sources and often you don't need to switch to that source when doing so cause the 'location' together withe the error code/error tetx already tells you what happened.
Unfortunately I don't see a way to let the precompiler do the job, but the function name should be fastly reachable.
Regards, Alex
-
2 Attachment(s)
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
Skizmo
Quote:
Originally Posted by
ahmd
...My idea was to use __LINE__, but when I change the code that value may change as well (in regards to previous builds/versions of the service).
Thank you all. I wasn't able to find a helper program that I needed, so I made one. I'll share it with you since I started this thread. It would be nice if someone with knowledge of how to integrate it into the IDE did it, but until that you can use a hot key for it to generate a sequentual ID and place it on to a clipboard. So basically, to insert a new ID you hit Ctrl+Shft+I and then Ctrl+V.
-
Re: Ways to insert unique ID into a logging function
ahmd, this program doesn't work when I try to start it from a service or from an account with limited permissions. Any ideas?
-
Re: Ways to insert unique ID into a logging function
IDK, works for me. Here's how I use it. Say, I'm adding my logging functions into C++ project. When I type
ReportIntoLog(
I need the next sequential ID for the logging function. Before I would write them down on a piece of paper as I go, but now instead of doing it, I can hit Ctrl+Shft+I (or whatever hot key you set up there) and then Ctrl+V to paste the ID into my C++ project. (Obviously that program has to be running at the same time.) I don't have to worry about messing up those IDs anymore if I place it into the project I work on, so that it will be backed up, copied, upgraded, etc. with it.
I don't know why you'd want to run it from a service though? It obviously won't run since it requires user interaction. As for the limited account, I guess if you completely limit it. In a nutshell, what it does, it reads its settings from a file that it creates in the same folder where you run it from, then sets up a hot key and waits for that hot key. If it comes, it copies the current ID into Clipboard, increments it by one, makes a sound and writes the result into its storage file.
Again, I'm not asking you to debug it or do anything, it works as-is for me. I'm simply sharing it in case someone else might need to the same function. You can also change the code if you feel like doing so.
PS. Even though it was written with an earlier version of VS, I'm using it on Win7, with VS 2008 on a Standard (non admin) User account.
-
Re: Ways to insert unique ID into a logging function
Code:
else
{
ReportIntoLog(1);
}
I would warn you against reporting this way. :)
Say you have your unique id in log. This tells you that there was something wrong with your particular code line. But what was wrong? What was the value(s) of critical variable(s) (not in your simplistic sample :))? What was the last error code from Windows? Besides, it's not always critical errors you're gonna have all the time. You might want to trace warnings and informational comments as well.
And it makes you always have your source code in front of you to trace what happens step by step. Irregular numbers (imagine you decide to add another code a couple of months later) soon become a nightmare to trace.
Another issue to think about. You might want have your logs of moderate size (as all we know, when trying to catch a very tricky glitch, logs typically tend to grow fast, eat free disk space and eventually make the application/system/database suffocate :)). This should give you an idea of having log levels and log subsystems. "Level" controls the level of details. "Subsystem" controls logging in particular component to be enabled/disabled.
And the last, to the original topic. As a sort of id we (me and my teammates) use __FUNCTION__ macro. As well, we always use human readable text in every log record along with printing pairs (critical variable = value) inside the text. Besides if applicable, the text contains hints to operation personnel about possible issue reasons and misfits in settings. The final log appears relatively easy to read on client side and at the same time having enough information to analyze.
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
I don't know why you'd want to run it from a service though? It obviously won't run since it requires user interaction.
My comments were in reference to posting code in an effort to help, and then having someone comment that the code doesn't work for them or isn't exactly what they need. I'm sure you can make the connection.
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
Igor Vartanov
I would warn you against reporting this way. :)
Igor, first of all, you contradict yourself. You say that I should keep my logging to a bare minimum because of the log size concerns and then tell me to include "human readable text in every log record along with printing pairs (critical variable = value) inside the text".
Thank you for your comments though. As I said above, the example I gave is a minimalistic version of what is actually use. And, I agree with what you said, except that I always add a unique ID to every post in the log to prevent confusion when dealing with silimlar entries. It is also much easier to find the code lines in the source code by their ID rather then by a discriptive text. The exact log text entry might also get distorted when passed from one person to another. One more reason is that it's always easy to refer to a particular bug by its ID number among the members of our development team.
I did use __LINE__ and __FUNCTION__ in my logging before, but sometimes it gives out too much information about the code. I prefer to call my methods and functions with full descriptive names. (And, obviously, I don't mean our team. I'm talking about a situation when we ask a user of the product to do the testing on their side. So in that case having a unique ID in the log with the bare minimum technical information quite suffices.)
And, Arjay, I'm still not sure what you're talking about...
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
And, Arjay, I'm still not sure what you're talking about...
Think back to previous posts and what your comments were to other folks that posted code in an effort to help you. I can think of two recent examples involving Igor and myself and those are only recent ones. Think about it.
-
Re: Ways to insert unique ID into a logging function
What? :) Arjay, you didn't drink anything this morning, did you :D
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
What? :) Arjay, you didn't drink anything this morning, did you :D
Sure he didn't!
AFAIK Arjay drinks only once a year - in the last evening of Global MVP summit and not more than one shot! :D
So, ahmd, relax! Take it easy! ;)
-
Re: Ways to insert unique ID into a logging function
:D
Hey, I remember I ran into a thread here with pictures of some of this forum's mvps. Is it still somewhere?
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
:D
Hey, I remember I ran into a thread here with pictures of some of this forum's mvps. Is it still somewhere?
Maybe... I don't know ... :rolleyes:
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
Igor, first of all, you contradict yourself. You say that I should keep my logging to a bare minimum because of the log size concerns and then tell me to include "human readable text in every log record along with printing pairs (critical variable = value) inside the text".
First of all I said you should not log in spartan/laconic style, but your logs must be human readable. And keeping your logging of moderate size was only a secondary comment. So, as for me, no contradiction at all. :)
And I can see that having your logs readable enough without referring to sources makes not much sense to you. The same to levels (and subsystems, though it's not that common in practice like levels). Something makes me believe it's only a matter of time. ;)
Quote:
It is also much easier to find the code lines in the source code by their ID rather then by a discriptive text.
You must be kidding. Is that so difficult to find a descriptive text, when you know the exact function name including class name? Remember I said primary id is __FUNCTION__ but not descriptive text?
Quote:
The exact log text entry might also get distorted when passed from one person to another.
The log text must not be passed from one person to another. The log files must be instead. :)
Quote:
One more reason is that it's always easy to refer to a particular bug by its ID number among the members of our development team.
This sounds not very convincing to me: "you know, the client reported 587 tonight. Again!" Sorry, I don't believe you, my people never talk like that. :)
For your information, we use referring to a bug (inside developers team as well as between development and validation departments) not by it's placement in the source code but by its id in bug tracking system. And always it is accompanied with verbal comments about visible effects and workflow details. Very few people I know are really good at distinguishing big numbers just by hearing them.
And a bug typically appears a complex of (cross)effects caused by various combinations of reasons, and almost never it is a syntax error (or anything else same stupid) in a single line. I wish I had such bugs all the time! :D
Quote:
I did use __LINE__ and __FUNCTION__ in my logging before, but sometimes it gives out too much information about the code.
It's never too late to change your mind. Let's have a deal: you use your spartan id system say for a year or a half, and then share with us your impressions and improvements. Agree? :)
-
Re: Ways to insert unique ID into a logging function
IDK, it's just nice to add a face to words ... I see you already did that, Victor :)
Anyway, if someone knows the link, please post it here...
So, did you guys all meet at that Global MVP summit? Where was it, btw?
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
Igor Vartanov
It's never too late to change your mind. Let's have a deal: you use your spartan id system say for a year or a half, and then share with us your impressions and improvements. Agree? :)
Sure.
Well, we clearly have some different approaches to logging. I've been doing this for quite a long time and never had problems with it (nor the people that work with me.)
-
Re: Ways to insert unique ID into a logging function
Quote:
I've been doing this for quite a long time and never had problems with it
Sorry, my impression was the idea is something new to you. My bad. :)
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
Igor Vartanov
Sorry, my impression was the idea is something new to you. My bad. :)
In case I came off wrong in my previous posts, here's an example of a log that may be generated by my software:
Quote:
10193> 8/10/2010 2:55:11 PM : [0087] = >Initiating a new <name> process: Elevated. Cmd line: /ptr /log=1
10193> 8/10/2010 2:55:11 PM : [0095] = >[OS: Microsoft Windows XP Professional Service Pack 3 (build 2600). LOCALE: English_United States.1252]
10193> 8/10/2010 2:54:57 PM : [0560] = **OS_ERR: (2) "C:/Blah-blah-blah" The system cannot find the file specified.
10193> 8/10/2010 2:54:57 PM : [0567] = **INT_ERR
10193> 8/10/2010 2:54:58 PM : [0176] = Exiting process now, ret code = -24
In the sample above, 10193 is a version of the program. Then date & time when the event was logged (unless it's the Windows own Event Log), then our unique ID in the square brackets, followed by a description, where possible. In case of the OS error, it is reported too (line 3), or in case of a code/logic specific error, it's reported relative to the unique ID (line 4)
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
Hey, I remember I ran into a thread here with pictures of some of this forum's mvps. Is it still somewhere?
Looks like you are talking about the photo album thread in the Chit Chat section. The link, BTW, wouln't even really be necessary: The thread is sticky and thus always on top pf the list.
-
Re: Ways to insert unique ID into a logging function
Nice, dude! 155 pages :) It may take the whole day to go through.....
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
Nice, dude! 155 pages :) It may take the whole day to go through.....
If you're only looking for Arjay: There's an index in the first posts, or you could use the forum search feature. :cool:
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
Eri523
If you're only looking for Arjay: There's an index in the first posts, or you could use the forum search feature. :cool:
Ha-ha-ha... No, I'm not looking for Arjay :D But he is a big dude... so I should probably stop taunting him... he might kick my a_s_s :D
PS. Cool, so it was in Seattle... it's like an hour away from me.
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
ahmd
PS. Cool, so it was in Seattle... it's like an hour away from me.
And where are you (if it's not a top secret)? :)
-
Re: Ways to insert unique ID into a logging function
Quote:
Originally Posted by
VictorN
And where are you (if it's not a top secret)? :)
Tacoma, WA