proxygen
folly::LogStreamProcessor Class Reference

#include <LogStreamProcessor.h>

Public Types

enum  AppendType { APPEND }
 
enum  FormatType { FORMAT }
 

Public Member Functions

 LogStreamProcessor (const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
 
template<typename... Args>
 LogStreamProcessor (const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType, Args &&...args) noexcept
 
template<typename... Args>
 LogStreamProcessor (const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, FormatType, folly::StringPiece fmt, Args &&...args) noexcept
 
 LogStreamProcessor (XlogCategoryInfo< true > *categoryInfo, LogLevel level, folly::StringPiece categoryName, bool isCategoryNameOverridden, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
 
template<typename... Args>
 LogStreamProcessor (XlogCategoryInfo< true > *categoryInfo, LogLevel level, folly::StringPiece categoryName, bool isCategoryNameOverridden, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType, Args &&...args) noexcept
 
template<typename... Args>
 LogStreamProcessor (XlogCategoryInfo< true > *categoryInfo, LogLevel level, folly::StringPiece categoryName, bool isCategoryNameOverridden, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, FormatType, folly::StringPiece fmt, Args &&...args) noexcept
 
 ~LogStreamProcessor () noexcept
 
void operator& (std::ostream &stream) noexcept
 
void operator& (LogStream &&stream) noexcept
 
std::ostream & stream () noexcept
 
void logNow () noexcept
 

Private Types

enum  InternalType { INTERNAL }
 

Private Member Functions

 LogStreamProcessor (const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, InternalType, std::string &&msg) noexcept
 
 LogStreamProcessor (XlogCategoryInfo< true > *categoryInfo, LogLevel level, folly::StringPiece categoryName, bool isCategoryNameOverridden, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, InternalType, std::string &&msg) noexcept
 
 LogStreamProcessor (XlogFileScopeInfo *fileScopeInfo, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, InternalType, std::string &&msg) noexcept
 
std::string extractMessageString (LogStream &stream) noexcept
 
template<typename... Args>
FOLLY_NOINLINE std::string createLogString (Args &&...args) noexcept
 
template<typename... Args>
FOLLY_NOINLINE std::string formatLogString (folly::StringPiece fmt, const Args &...args) noexcept
 
template<typename Arg1 , typename... Args>
void fallbackFormat (std::string *str, const Arg1 &arg1, const Args &...remainder)
 
template<typename Arg >
void fallbackFormat (std::string *str, const Arg &arg)
 

Private Attributes

const LogCategory *const category_
 
LogLevel const level_
 
folly::StringPiece filename_
 
unsigned int lineNumber_
 
folly::StringPiece functionName_
 
std::string message_
 
LogStream stream_
 

Detailed Description

LogStreamProcessor receives a LogStream and logs it.

This class is primarily intended to be used through the FB_LOG*() macros. Its API is designed to support these macros, and is not designed for other use.

The operator&() method is used to trigger the logging. This operator is used because it has lower precedence than <<, but higher precedence than the ? ternary operator, allowing it to bind with the correct precedence in the log macro implementations.

Definition at line 89 of file LogStreamProcessor.h.

Member Enumeration Documentation

Enumerator
INTERNAL 

Definition at line 326 of file LogStreamProcessor.h.

Constructor & Destructor Documentation

folly::LogStreamProcessor::LogStreamProcessor ( const LogCategory category,
LogLevel  level,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
AppendType   
)
noexcept

LogStreamProcessor constructor for use with a LOG() macro with no extra arguments.

Note that the filename argument is not copied. The caller should ensure that it points to storage that will remain valid for the lifetime of the LogStreamProcessor. (This is always the case for the FILE preprocessor macro.)

Definition at line 23 of file LogStreamProcessor.cpp.

Referenced by LogStreamProcessor().

31  category,
32  level,
33  filename,
34  lineNumber,
35  functionName,
36  INTERNAL,
37  std::string()) {}
LogStreamProcessor(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
const char * string
Definition: Conv.cpp:212
template<typename... Args>
folly::LogStreamProcessor::LogStreamProcessor ( const LogCategory category,
LogLevel  level,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
AppendType  ,
Args &&...  args 
)
inlinenoexcept

LogStreamProcessor constructor for use with a LOG() macro with arguments to be concatenated with folly::to<std::string>()

Note that the filename argument is not copied. The caller should ensure that it points to storage that will remain valid for the lifetime of the LogStreamProcessor. (This is always the case for the FILE preprocessor macro.)

Definition at line 121 of file LogStreamProcessor.h.

References testing::Args().

130  category,
131  level,
132  filename,
133  lineNumber,
134  functionName,
135  INTERNAL,
136  createLogString(std::forward<Args>(args)...)) {}
FOLLY_NOINLINE std::string createLogString(Args &&...args) noexcept
LogStreamProcessor(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
template<typename... Args>
folly::LogStreamProcessor::LogStreamProcessor ( const LogCategory category,
LogLevel  level,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
FormatType  ,
folly::StringPiece  fmt,
Args &&...  args 
)
inlinenoexcept

LogStreamProcessor constructor for use with a LOG() macro with arguments to be concatenated with folly::to<std::string>()

Note that the filename argument is not copied. The caller should ensure that it points to storage that will remain valid for the lifetime of the LogStreamProcessor. (This is always the case for the FILE preprocessor macro.)

Definition at line 148 of file LogStreamProcessor.h.

References testing::Args(), and folly::pushmi::__adl::noexcept().

158  category,
159  level,
160  filename,
161  lineNumber,
162  functionName,
163  INTERNAL,
164  formatLogString(fmt, std::forward<Args>(args)...)) {}
LogStreamProcessor(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
FOLLY_NOINLINE std::string formatLogString(folly::StringPiece fmt, const Args &...args) noexcept
folly::LogStreamProcessor::LogStreamProcessor ( XlogCategoryInfo< true > *  categoryInfo,
LogLevel  level,
folly::StringPiece  categoryName,
bool  isCategoryNameOverridden,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
AppendType   
)
noexcept

Definition at line 39 of file LogStreamProcessor.cpp.

49  categoryInfo,
50  level,
51  categoryName,
52  isCategoryNameOverridden,
53  filename,
54  lineNumber,
55  functionName,
56  INTERNAL,
57  std::string()) {}
LogStreamProcessor(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
const char * string
Definition: Conv.cpp:212
template<typename... Args>
folly::LogStreamProcessor::LogStreamProcessor ( XlogCategoryInfo< true > *  categoryInfo,
LogLevel  level,
folly::StringPiece  categoryName,
bool  isCategoryNameOverridden,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
AppendType  ,
Args &&...  args 
)
inlinenoexcept

Definition at line 185 of file LogStreamProcessor.h.

References testing::Args().

196  categoryInfo,
197  level,
198  categoryName,
199  isCategoryNameOverridden,
200  filename,
201  lineNumber,
202  functionName,
203  INTERNAL,
204  createLogString(std::forward<Args>(args)...)) {}
FOLLY_NOINLINE std::string createLogString(Args &&...args) noexcept
LogStreamProcessor(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
template<typename... Args>
folly::LogStreamProcessor::LogStreamProcessor ( XlogCategoryInfo< true > *  categoryInfo,
LogLevel  level,
folly::StringPiece  categoryName,
bool  isCategoryNameOverridden,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
FormatType  ,
folly::StringPiece  fmt,
Args &&...  args 
)
inlinenoexcept

Definition at line 206 of file LogStreamProcessor.h.

References testing::Args(), folly::pushmi::__adl::noexcept(), and folly::operator&().

218  categoryInfo,
219  level,
220  categoryName,
221  isCategoryNameOverridden,
222  filename,
223  lineNumber,
224  functionName,
225  INTERNAL,
226  formatLogString(fmt, std::forward<Args>(args)...)) {}
LogStreamProcessor(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, AppendType) noexcept
FOLLY_NOINLINE std::string formatLogString(folly::StringPiece fmt, const Args &...args) noexcept
folly::LogStreamProcessor::~LogStreamProcessor ( )
noexcept

Definition at line 177 of file LogStreamProcessor.cpp.

References logNow().

177  {
178  // The LogStreamProcessor destructor is responsible for logging the message.
179  // Doing this in the destructor avoids an separate function call to log the
180  // message being emitted inline at every log statement site.
181  logNow();
182 }
folly::LogStreamProcessor::LogStreamProcessor ( const LogCategory category,
LogLevel  level,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
InternalType  ,
std::string &&  msg 
)
privatenoexcept
folly::LogStreamProcessor::LogStreamProcessor ( XlogCategoryInfo< true > *  categoryInfo,
LogLevel  level,
folly::StringPiece  categoryName,
bool  isCategoryNameOverridden,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
InternalType  ,
std::string &&  msg 
)
privatenoexcept

Construct a LogStreamProcessor from an XlogCategoryInfo.

We intentionally define this in LogStreamProcessor.cpp instead of LogStreamProcessor.h to avoid having it inlined at every XLOG() call site, to reduce the emitted code size.

Definition at line 94 of file LogStreamProcessor.cpp.

References category_, filename_, functionName_, INTERNAL, level_, lineNumber_, LogStreamProcessor(), message_, folly::gen::move, folly::pushmi::__adl::noexcept(), stream_, and string.

104  : category_{getXlogCategory(
105  categoryInfo,
106  categoryName,
107  isCategoryNameOverridden)},
108  level_{level},
109  filename_{filename},
110  lineNumber_{lineNumber},
111  functionName_{functionName},
112  message_{std::move(msg)},
113  stream_{this} {}
folly::StringPiece filename_
const LogCategory *const category_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::StringPiece functionName_
folly::LogStreamProcessor::LogStreamProcessor ( XlogFileScopeInfo fileScopeInfo,
LogLevel  level,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
InternalType  ,
std::string &&  msg 
)
privatenoexcept

Member Function Documentation

template<typename... Args>
FOLLY_NOINLINE std::string folly::LogStreamProcessor::createLogString ( Args &&...  args)
inlineprivatenoexcept

Construct a log message string using folly::to<std::string>()

This function attempts to avoid throwing exceptions. If an error occurs during formatting, a message including the error details is returned instead. This is done to help ensure that log statements do not generate exceptions, but instead just log an error string when something goes wrong.

Definition at line 365 of file LogStreamProcessor.h.

References testing::Args().

365  {
366  try {
367  return folly::to<std::string>(std::forward<Args>(args)...);
368  } catch (const std::exception& ex) {
369  // This most likely means there was some error converting the arguments
370  // to strings. Handle the exception here, rather than letting it
371  // propagate up, since callers generally do not expect log statements to
372  // throw.
373  //
374  // Just log an error message letting indicating that something went wrong
375  // formatting the log message.
376  return folly::to<std::string>(
377  "error constructing log message: ", ex.what());
378  }
379  }
std::string folly::LogStreamProcessor::extractMessageString ( LogStream stream)
privatenoexcept

Definition at line 201 of file LogStreamProcessor.cpp.

References message_, and folly::gen::move.

Referenced by logNow().

202  {
203  if (stream.empty()) {
204  return std::move(message_);
205  }
206 
207  if (message_.empty()) {
208  return stream.extractString();
209  }
210  message_.append(stream.extractString());
211  return std::move(message_);
212 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<typename Arg1 , typename... Args>
void folly::LogStreamProcessor::fallbackFormat ( std::string str,
const Arg1 &  arg1,
const Args &...  remainder 
)
inlineprivate

Helper function generate a fallback version of the arguments in case folly::sformat() throws an exception.

This attempts to convert each argument to a string using a similar mechanism to folly::to<std::string>(), if supported.

Definition at line 423 of file LogStreamProcessor.h.

References folly::detail::fallbackFormatOneArg().

423  {
424  detail::fallbackFormatOneArg(str, &arg1, 0);
425  str->append(", ");
426  fallbackFormat(str, remainder...);
427  }
void fallbackFormat(std::string *str, const Arg1 &arg1, const Args &...remainder)
auto fallbackFormatOneArg(std::string *str, const Arg *arg, int) -> decltype(toAppend(std::declval< Arg >(), std::declval< std::string * >()), std::declval< void >())
template<typename Arg >
void folly::LogStreamProcessor::fallbackFormat ( std::string str,
const Arg &  arg 
)
inlineprivate

Definition at line 430 of file LogStreamProcessor.h.

References folly::detail::fallbackFormatOneArg().

430  {
431  detail::fallbackFormatOneArg(str, &arg, 0);
432  }
auto fallbackFormatOneArg(std::string *str, const Arg *arg, int) -> decltype(toAppend(std::declval< Arg >(), std::declval< std::string * >()), std::declval< void >())
template<typename... Args>
FOLLY_NOINLINE std::string folly::LogStreamProcessor::formatLogString ( folly::StringPiece  fmt,
const Args &...  args 
)
inlineprivatenoexcept

Construct a log message string using folly::sformat()

This function attempts to avoid throwing exceptions. If an error occurs during formatting, a message including the error details is returned instead. This is done to help ensure that log statements do not generate exceptions, but instead just log an error string when something goes wrong.

Definition at line 390 of file LogStreamProcessor.h.

References testing::Args(), folly::Range< Iter >::data(), folly::sformat(), folly::Range< Iter >::size(), and string.

392  {
393  try {
394  return folly::sformat(fmt, args...);
395  } catch (const std::exception& ex) {
396  // This most likely means that the caller had a bug in their format
397  // string/arguments. Handle the exception here, rather than letting it
398  // propagate up, since callers generally do not expect log statements to
399  // throw.
400  //
401  // Log the format string and as much of the arguments as we can convert,
402  // to aid debugging.
403  std::string result;
404  result.append("error formatting log message: ");
405  result.append(ex.what());
406  result.append("; format string: \"");
407  result.append(fmt.data(), fmt.size());
408  result.append("\", arguments: ");
409  fallbackFormat(&result, args...);
410  return result;
411  }
412  }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
constexpr size_type size() const
Definition: Range.h:431
void fallbackFormat(std::string *str, const Arg1 &arg1, const Args &...remainder)
constexpr Iter data() const
Definition: Range.h:446
const char * string
Definition: Conv.cpp:212
void folly::LogStreamProcessor::logNow ( )
noexcept

Definition at line 184 of file LogStreamProcessor.cpp.

References folly::LogCategory::admitMessage(), category_, extractMessageString(), filename_, functionName_, level_, lineNumber_, and stream_.

Referenced by folly::LogStreamVoidify< true >::operator&(), and ~LogStreamProcessor().

184  {
185  // Note that admitMessage() is not noexcept and theoretically may throw.
186  // However, the only exception that should be possible is std::bad_alloc if
187  // we fail to allocate memory. We intentionally let our noexcept specifier
188  // crash in that case, since the program likely won't be able to continue
189  // anyway.
190  //
191  // Any other error here is unexpected and we also want to fail hard
192  // in that situation too.
193  category_->admitMessage(LogMessage{category_,
194  level_,
195  filename_,
196  lineNumber_,
199 }
folly::StringPiece filename_
const LogCategory *const category_
std::string extractMessageString(LogStream &stream) noexcept
folly::StringPiece functionName_
void admitMessage(const LogMessage &message) const
Definition: LogCategory.cpp:49
void folly::LogStreamProcessor::operator& ( std::ostream &  stream)
noexcept

This version of operator&() is typically used when the user specifies log arguments using the << stream operator. The operator<<() generally returns a std::ostream&

void folly::LogStreamProcessor::operator& ( LogStream &&  stream)
noexcept

This version of operator&() is used when no extra arguments are specified with the << operator. In this case the & operator is applied directly to the temporary LogStream object.

std::ostream& folly::LogStreamProcessor::stream ( )
inlinenoexcept

Definition at line 319 of file LogStreamProcessor.h.

References folly::pushmi::__adl::noexcept().

Referenced by folly::LogStreamVoidify< true >::operator&().

319  {
320  return stream_;
321  }

Member Data Documentation

const LogCategory* const folly::LogStreamProcessor::category_
private

Definition at line 434 of file LogStreamProcessor.h.

Referenced by logNow(), and LogStreamProcessor().

folly::StringPiece folly::LogStreamProcessor::filename_
private

Definition at line 436 of file LogStreamProcessor.h.

Referenced by logNow(), and LogStreamProcessor().

folly::StringPiece folly::LogStreamProcessor::functionName_
private

Definition at line 438 of file LogStreamProcessor.h.

Referenced by logNow(), and LogStreamProcessor().

LogLevel const folly::LogStreamProcessor::level_
private

Definition at line 435 of file LogStreamProcessor.h.

Referenced by logNow(), and LogStreamProcessor().

unsigned int folly::LogStreamProcessor::lineNumber_
private

Definition at line 437 of file LogStreamProcessor.h.

Referenced by logNow(), and LogStreamProcessor().

std::string folly::LogStreamProcessor::message_
private

Definition at line 439 of file LogStreamProcessor.h.

Referenced by extractMessageString(), and LogStreamProcessor().

LogStream folly::LogStreamProcessor::stream_
private

Definition at line 440 of file LogStreamProcessor.h.

Referenced by logNow(), and LogStreamProcessor().


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