This is the brute-force inelegant mechanism that illustrates what I could do (likely done with MACROS in the end):
void myFunction(std::ostream& _log) { int threshold = 100; ... if (threshold >= 200) { _log << "Message with level 200 verbosity"; // Not emitted } if (threshold >= 0) { _log << "Message with level 0 verbosity"; // EMITTED } ... } Note above that when messages are "filtered out" because they do not meet the verbosity threshold, that it costs very little. The whole operation of constructing and handling those messages is skipped.
More elegant would be something like this:
class MessageFilterteringOStream : std::ostream { ... void setMessageThreshold(int unsigned threshold) { _threshold = threshold; } ... } ... void myFunction(MessageFilteringOStream& _log) { _log.setMessageThreshold(100); ... _log << _log.verbosity(200) << "Message with level 200 verbosity"; // NOT emitted _log << _log.verbosity( 0) << "Message with level 0 verbosity"; // emitted ... } To make this work, I'd need to do these things. The first two are easy. The third...?
- Subclass
std::ostreamand addthresholdandmessage_verbosityvariables. - Add the
verbosity()method that stores the verbosity of the current message intomessage_verbosity. (Copy the approach ofstd::setw()or something). - Somehow "intercept" the characters coming in and filter out everything while
threshold < message_verbosity. Probably just by subclassingoperator<<?
HOWEVER, this approach wouldn't be worth the trouble if the code didn't "short-circuit" the creation of the messages that are going to be filtered out anyway. I don't have any idea how to accomplish that... What do you say...?
EDIT: Thank you all for your advice/input. This codebase passes around std::ostream references to many different places. I was hoping to just, at the top-level, modify the verbosity of the thing before sending it down, so I didn't have to visit everywhere that messages are created. It sounds like that won't be easy. I could just interecept at the puts() level, but by then the messages have already been created, etc. (There can be MANY.) I"m still thinking about what you have said.
std::streambuf::putclevel.operator<<, then nothing executes. I don't understand what you're looking for.std::ios_baseclass and itsxalloc(),iword()andpword()methods.