Jump to content
  • Advertisement

Archived

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

gimp

Preprocessor macro trick needed:

This topic is 5991 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
Advertisement

#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
Or you could try:

#define Log if(0) Log

You''d have to turn optimizations on to actually compile the if out, but even if you don''t it''ll still work as you''d expect...


codeka.com - Just click it.

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
Whoops, I didn''t realise Fruny posted the exact same thing as me

And yeah, Beer Hunter''s version is even better (that''s same way I used to get around VC++''s for scoping "feature", until they fixed it in .NET)


codeka.com - Just click it.

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
Here''s a handy macro I''ve used in situations similar to yours:

#ifdef _DEBUG
#define DebugPrint(x) cout << x
#else
#define DebugPrint /##/
#endif

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!