Log path informations to eventlog, Even better.

23 03 2008

Icon Description
In most of our projects, when some error occurs, it’s being logged to the evenlog with line number and filename. Its done by using __FILE__ and __LINE__ macro.

But the file macros expands to the fullpath. for e.g. If i am building the delivery in my personal folder – “C:\Jijo\Build\MyProduct”, the __FILE__ macro will include this full path and finally in eventlog will contain funny paths which the end-user might see. You can see some 3ed party eventlogs in event viewer which contains authors name in path. 🙂

Icon How Can I Do It?
You can use #line directive to modify __LINE__ and __FILENAME__. The syntax is as follows,
#line lineno “FileName”

Please see an example below.

#line __LINE__ "MyProduct\Sources\JobDll\Job.cpp"

Icon Note
While experimenting I’ve found that – Its safe to use #line after all #includes. If its used at the top of file, It can be reinitialized by the include files. At first it was not working for me. Latter works. Anyway take care.