Luther said:
Since the __XXX syntax is reserved, what if the compiler defined away
someting you depended on?
What if the compiler defined this?
#define __Logger __ProprietaryLogger
I don't think it is necessarily a namespace problem. The double
underscore prefix is reserved and if you use it, I think you're
susceptible to undefined behavior.
By the way, excellent thread.
-Luther
You are right. Other posters have commented on that too. So here is a
version that is *not* vulnerable in this way:
#include <pthread.h>
#include <sstream>
#include <iostream>
void do_whatever_you_want( std::string str ) {
std::cerr << str;
}
namespace DO_NOT_USE {
static pthread_mutex_t logger_lock = PTHREAD_MUTEX_INITIALIZER;
class Logger : public std::stringstream {
public:
~Logger ( void ) {
pthread_mutex_lock( &logger_lock );
do_whatever_you_want( this->str() );
pthread_mutex_unlock( &logger_lock );
}
}; // class Logger
} // namespace DO_NOT_USE
#define rlog ( DO_NOT_USE::Logger() << std::dec << __FILE__ << " [" <<
__LINE__ << "]: " )
int main ( void ) {
rlog << "hello world!" << std::endl;
}
There is but one thing that I still do not understand: If I leave out the
sneaky std::dec in the macro definition, I see the address of the const
char * that __FILE__ expands to instead of the file name. Does someone know
why this is?
Best regards
Kai-Uwe