Archived

This topic is now archived and is closed to further replies.

Preprocessor macro trick needed:

This topic is 5647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve just worked out what has been slowing down all my startup code(which is considerable in size). My logging seems to have consumed nearly ~1ms per line to log out to a console and a file. However it turns out that a considerable portion of the slowdown is to do with stringstreams that are created along the way and other parts of my nice logging method. I love to have the ability to ''compile out'' the logging. Here is a typical line being logged: Log << "Loaded " << Name << " in " << Timer.ElapsedMiliseconds() << "ms." << Endl; I have a few small defines already in place: #define Log CLog::GetInstance() #define Endl Log.EndLine() It just so happens that every logged line finishes with a Endl define. So, I thought, easy: #define Log /* #define Endl *\ Nope, even though it''s in the preprocessor, it still see''s the comment flags properly. Any idea''s? Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites

#ifdef LOGGING
#define LOG(X) X
#else
#define LOG(X)
#endif

LOG( Log << "Loaded " << endl; )


Edit: The following is actually safer (and no slower), as it requires X to be a standalone statement.

#ifdef LOGGING
#define LOG(X) if(1) {X}
#else
#define LOG(X) {}
#endif


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 25, 2002 6:44:13 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
#define LOG(X) if(1) {X}
Consider what this expands to:

if(player.status() == DEAD)
LOG( Log << "Player died" << endl; )
else
do_something_else();


Following the general idea, I suggest this instead: #define LOG(X) if(0) {} else {X}

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
Following the general idea, I suggest this instead: #define LOG(X) if(0) {} else {X}


You are right, I missed that one. I bow before Your Hackery.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Instead of wrapping every single log line in macros, do this:


#ifdef NO_LOGGING
class NullLog
{
}
Log;

template <typename T>
inline NullLog & operator << (NullLog & log, const T &)
{
return log;
}
#endif // NO_LOGGING


You can just leave all your log calls the way they are. Instead of writting stuff to a file, everything gets filtered through the dummy operator<< which even brain-dead optimizers can get rid of completely.

Your code will look neater and there's no worrying about that brace placement business.

-Mike


[edited by - Anon Mike on June 25, 2002 1:09:34 PM]

Share this post


Link to post
Share on other sites