DEBUG macro for log message postponing

A pass first collects some log data in form of strings and at some later point decides depending on program state whether to print it to dbgs() or not. In other words the debug stream would be totally flooded if every string was printed. I was thinking of guarding a std::stringstream object with the DEBUG macro. Something like

DEBUG(std::stringstream ss);
DEBUG(ss << "Conditional logging data" << I << "\n");

... at some later stage ...

DEBUG( if (log_me) dbgs() << ss.str());

However, LLVM doesn't like the above construct. In LLVM, how can one postpone some debugging logs for later printing?

Many thanks,
Frank

A pass first collects some log data in form of strings and at some later
point decides depending on program state whether to print it to dbgs()
or not. In other words the debug stream would be totally flooded if
every string was printed. I was thinking of guarding a std::stringstream
object with the DEBUG macro. Something like

DEBUG(std::stringstream ss);
DEBUG(ss << "Conditional logging data" << I << "\n");

... at some later stage ...

DEBUG( if (log_me) dbgs() << ss.str());

However, LLVM doesn't like the above construct. In LLVM, how can one
postpone some debugging logs for later printing?

What you're asking for is usually written as:

   #ifndef NDEBUG
   std::stringstream ss
   #endif
   DEBUG(ss << "Conditional logging data" << I << "\n");
   DEBUG( if (log_me) dbgs() << ss.str());

Jon