proxygen
folly::LogStreamVoidify< Fatal > Class Template Reference

#include <LogStreamProcessor.h>

Public Member Functions

void operator& (std::ostream &) noexcept
 

Detailed Description

template<bool Fatal>
class folly::LogStreamVoidify< Fatal >

LogStreamVoidify() is a helper class used in the FB_LOG() and XLOG() macros.

It's only purpose is to provide an & operator overload that returns void. This allows the log macros to expand roughly to:

(logEnabled) ? (void)0 : LogStreamVoidify{} & LogStreamProcessor{}.stream() << "msg";

This enables the right hand (':') side of the ternary ? expression to have a void type, and allows various streaming operator expressions to be placed on the right hand side of the expression.

Operator & is used since it has higher precedence than ?:, but lower precedence than <<.

This class is templated on whether the log message is fatal so that the operator& can be declared [[noreturn]] for fatal log messages. This prevents the compiler from complaining about functions that do not return a value after a fatal log statement.

Definition at line 465 of file LogStreamProcessor.h.

Member Function Documentation

template<bool Fatal>
void folly::LogStreamVoidify< Fatal >::operator& ( std::ostream &  )
inlinenoexcept

In the default (non-fatal) case, the & operator implementation is a no-op.

We perform the actual logging in the LogStreamProcessor destructor. It feels slightly hacky to perform logging in the LogStreamProcessor destructor instead of here, since the LogStreamProcessor destructor is not evaluated until the very end of the statement. In practice log statements really shouldn't be in the middle of larger statements with other side effects, so this ordering distinction shouldn't make much difference.

However, by keeping this function a no-op we reduce the amount of code generated for log statements. This function call can be completely eliminated by the compiler, leaving only the LogStreamProcessor destructor invocation, which cannot be eliminated.

Definition at line 483 of file LogStreamProcessor.h.

483 {}

The documentation for this class was generated from the following file: