Filename String Pooling (PB_THIS_FILE)

All of the logging calls have the source code’s filename passed in to them. This string ultimately comes from the preprocessor’s expansion of the __FILE__ macro via the location macros. Since each log call has a filename literal as one of its arguments, this technique has the potential to create a lot of duplicate strings in the data segment. The problem can be addressed in one of two manners described below.

The first and easiest is to simply use the 'merge duplicate strings' compiler switch which will replace all duplicate strings in the data segment with a pointer to a single copy. This option is in fact typically enabled as part of the default compiler settings for release builds and is the preferred method for string pooling as it requires no intervention on the part of the developer.

There are times, however, where for various reasons the ‘merge duplicate strings’ compiler flag cannot be used. The PB_THIS_FILE macros were created for these situations and are essentially ‘the layer of indirection that solves any problem.’ The location macros actually do not use the __FILE__ macro directly but refer instead to the preprocessor macro PB_THIS_FILE which is in turn mapped either to the __FILE__ macro or to a static variable. To eliminate duplicate filename strings by using the technique of mapping PB_THIS_FILE to a single static variable just add the following code at the top of each source file after the #include <PB.h> line. (This form requires inclusion of tchar.h.)

#undef PB_THIS_FILE
static TCHAR PB_THIS_FILE[] = _T(__FILE__);

When explicitly using the ANSI and/or Unicode versions of the logging calls independent of the UNICODE preprocessor macro then the following form must be used instead to get both versions of the filename strings. Note that the character set independent PB_THIS_FILE macro with no A,W suffix will still automatically define itself in terms of the other two as usual.

#undef PB_THIS_FILEA
#undef PB_THIS_FILEW
static CHAR PB_THIS_FILEA[] = __FILE__;
static WCHAR PB_THIS_FILEW[] = _L(__FILE__);

Finally, PB_THIS_FILE can also be used to alias the filename to something else entirely - Binroe, AOXOMOXOA, Packers, whatever works.