proxygen
folly::LogMessage Class Reference

#include <LogMessage.h>

Public Member Functions

 LogMessage (const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, std::string &&msg)
 
 LogMessage (const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, folly::StringPiece msg)
 
 LogMessage (const LogCategory *category, LogLevel level, std::chrono::system_clock::time_point timestamp, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, std::string &&msg)
 
const LogCategorygetCategory () const
 
LogLevel getLevel () const
 
folly::StringPiece getFileName () const
 
folly::StringPiece getFileBaseName () const
 
unsigned int getLineNumber () const
 
folly::StringPiece getFunctionName () const
 
std::chrono::system_clock::time_point getTimestamp () const
 
uint64_t getThreadID () const
 
const std::stringgetMessage () const
 
const std::stringgetRawMessage () const
 
bool containsNewlines () const
 

Private Member Functions

void sanitizeMessage ()
 

Private Attributes

const LogCategory *const category_ {nullptr}
 
LogLevel const level_ {static_cast<LogLevel>(0)}
 
uint64_t const threadID_ {0}
 
std::chrono::system_clock::time_point const timestamp_
 
folly::StringPiece const filename_
 
unsigned int const lineNumber_ {0}
 
folly::StringPiece const functionName_
 
bool containsNewlines_ {false}
 
std::string const rawMessage_
 
std::string message_
 

Detailed Description

LogMessage represents a single message to be logged.

LogMessage objects are relatively temporary objects, that only exist for the time it takes to invoke all of the appropriate LogHandlers. These generally only live in the thread that logged the message, and are not modified once created. (That said, LogHandler implementations may copy and store LogMessage objects for later use if desired.)

Definition at line 38 of file LogMessage.h.

Constructor & Destructor Documentation

folly::LogMessage::LogMessage ( const LogCategory category,
LogLevel  level,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
std::string &&  msg 
)

Definition at line 24 of file LogMessage.cpp.

References category_, filename_, functionName_, folly::getOSThreadID(), level_, lineNumber_, folly::gen::move, now(), rawMessage_, sanitizeMessage(), string, threadID_, and timestamp_.

Referenced by LogMessage().

31  : category_{category},
32  level_{level},
35  filename_{filename},
36  lineNumber_{lineNumber},
37  functionName_{functionName},
38  rawMessage_{std::move(msg)} {
40 }
LogLevel const level_
Definition: LogMessage.h:125
unsigned int const lineNumber_
Definition: LogMessage.h:137
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
std::string const rawMessage_
Definition: LogMessage.h:158
folly::StringPiece const filename_
Definition: LogMessage.h:132
uint64_t getOSThreadID()
Definition: ThreadId.h:80
const LogCategory *const category_
Definition: LogMessage.h:124
void sanitizeMessage()
Definition: LogMessage.cpp:83
std::chrono::system_clock::time_point const timestamp_
Definition: LogMessage.h:127
folly::StringPiece const functionName_
Definition: LogMessage.h:142
uint64_t const threadID_
Definition: LogMessage.h:126
folly::LogMessage::LogMessage ( const LogCategory category,
LogLevel  level,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
folly::StringPiece  msg 
)
inline

Definition at line 47 of file LogMessage.h.

References LogMessage(), and string.

54  : LogMessage(
55  category,
56  level,
57  filename,
58  lineNumber,
59  functionName,
60  msg.str()) {}
std::string str() const
Definition: Range.h:591
LogMessage(const LogCategory *category, LogLevel level, folly::StringPiece filename, unsigned int lineNumber, folly::StringPiece functionName, std::string &&msg)
Definition: LogMessage.cpp:24
folly::LogMessage::LogMessage ( const LogCategory category,
LogLevel  level,
std::chrono::system_clock::time_point  timestamp,
folly::StringPiece  filename,
unsigned int  lineNumber,
folly::StringPiece  functionName,
std::string &&  msg 
)

Construct a LogMessage with an explicit timestamp. This is primarily intended for use in unit tests, so the tests can get deterministic behavior with regards to timestamps.

Member Function Documentation

bool folly::LogMessage::containsNewlines ( ) const
inline

Definition at line 117 of file LogMessage.h.

References containsNewlines_, and sanitizeMessage().

Referenced by folly::CustomLogFormatter::formatMessage(), and folly::GlogStyleFormatter::formatMessage().

117  {
118  return containsNewlines_;
119  }
const LogCategory* folly::LogMessage::getCategory ( ) const
inline

Definition at line 76 of file LogMessage.h.

References category_.

76  {
77  return category_;
78  }
const LogCategory *const category_
Definition: LogMessage.h:124
StringPiece folly::LogMessage::getFileBaseName ( ) const

Definition at line 61 of file LogMessage.cpp.

References filename_, max, folly::Range< const char * >::npos, folly::Range< Iter >::rfind(), and folly::Range< Iter >::subpiece().

Referenced by folly::CustomLogFormatter::formatMessage(), folly::GlogStyleFormatter::formatMessage(), and getFileName().

61  {
62 #if _WIN32
63  // Windows allows either backwards or forwards slash as path separator
64  auto idx1 = filename_.rfind('\\');
65  auto idx2 = filename_.rfind('/');
67  if (idx1 == StringPiece::npos) {
68  idx = idx2;
69  } else if (idx2 == StringPiece::npos) {
70  idx = idx1;
71  } else {
72  idx = std::max(idx1, idx2);
73  }
74 #else
75  auto idx = filename_.rfind('/');
76 #endif
77  if (idx == StringPiece::npos) {
78  return filename_;
79  }
80  return filename_.subpiece(idx + 1);
81 }
LogLevel max
Definition: LogLevel.cpp:31
folly::StringPiece const filename_
Definition: LogMessage.h:132
size_type rfind(value_type c) const
Definition: Range.h:761
Range subpiece(size_type first, size_type length=npos) const
Definition: Range.h:686
static const size_type npos
Definition: Range.h:197
folly::StringPiece folly::LogMessage::getFileName ( ) const
inline

Definition at line 84 of file LogMessage.h.

References filename_, and getFileBaseName().

Referenced by folly::LogCategory::admitMessage().

84  {
85  return filename_;
86  }
folly::StringPiece const filename_
Definition: LogMessage.h:132
folly::StringPiece folly::LogMessage::getFunctionName ( ) const
inline

Definition at line 93 of file LogMessage.h.

References functionName_.

Referenced by folly::CustomLogFormatter::formatMessage().

93  {
94  return functionName_;
95  }
folly::StringPiece const functionName_
Definition: LogMessage.h:142
LogLevel folly::LogMessage::getLevel ( ) const
inline
unsigned int folly::LogMessage::getLineNumber ( ) const
inline

Definition at line 89 of file LogMessage.h.

References lineNumber_.

Referenced by folly::LogCategory::admitMessage(), folly::CustomLogFormatter::formatMessage(), and folly::GlogStyleFormatter::formatMessage().

89  {
90  return lineNumber_;
91  }
unsigned int const lineNumber_
Definition: LogMessage.h:137
const std::string& folly::LogMessage::getMessage ( ) const
inline

Definition at line 105 of file LogMessage.h.

References message_, and rawMessage_.

Referenced by folly::LogCategory::admitMessage(), folly::CustomLogFormatter::formatMessage(), and folly::GlogStyleFormatter::formatMessage().

105  {
106  // If no characters needed to be sanitized, message_ will be empty.
107  if (message_.empty()) {
108  return rawMessage_;
109  }
110  return message_;
111  }
std::string const rawMessage_
Definition: LogMessage.h:158
std::string message_
Definition: LogMessage.h:168
const std::string& folly::LogMessage::getRawMessage ( ) const
inline

Definition at line 113 of file LogMessage.h.

References rawMessage_.

113  {
114  return rawMessage_;
115  }
std::string const rawMessage_
Definition: LogMessage.h:158
uint64_t folly::LogMessage::getThreadID ( ) const
inline

Definition at line 101 of file LogMessage.h.

References threadID_.

Referenced by folly::CustomLogFormatter::formatMessage(), and folly::GlogStyleFormatter::formatMessage().

101  {
102  return threadID_;
103  }
uint64_t const threadID_
Definition: LogMessage.h:126
std::chrono::system_clock::time_point folly::LogMessage::getTimestamp ( ) const
inline

Definition at line 97 of file LogMessage.h.

References timestamp_.

Referenced by folly::CustomLogFormatter::formatMessage(), and folly::GlogStyleFormatter::formatMessage().

97  {
98  return timestamp_;
99  }
std::chrono::system_clock::time_point const timestamp_
Definition: LogMessage.h:127
void folly::LogMessage::sanitizeMessage ( )
private

Definition at line 83 of file LogMessage.cpp.

References c, containsNewlines_, folly::data(), message_, and rawMessage_.

Referenced by containsNewlines(), and LogMessage().

83  {
84  // Compute how long the sanitized string will be.
85  size_t sanitizedLength = 0;
86  for (const char c : rawMessage_) {
87  if (c == '\\') {
88  // Backslashes are escaped as two backslashes
89  sanitizedLength += 2;
90  } else if (static_cast<unsigned char>(c) < 0x20) {
91  // Newlines and tabs are emitted directly with no escaping.
92  // All other control characters are emitted as \xNN (4 characters)
93  if (c == '\n') {
94  sanitizedLength += 1;
95  containsNewlines_ = true;
96  } else if (c == '\t') {
97  sanitizedLength += 1;
98  } else {
99  sanitizedLength += 4;
100  }
101  } else if (c == 0x7f) {
102  // Bytes above the ASCII range are emitted as \xNN (4 characters)
103  sanitizedLength += 4;
104  } else {
105  // This character will be emitted as-is, with no escaping.
106  ++sanitizedLength;
107  }
108  }
109 
110  // If nothing is different, just use rawMessage_ directly,
111  // and don't populate message_.
112  if (sanitizedLength == rawMessage_.size()) {
113  return;
114  }
115 
116  message_.reserve(sanitizedLength);
117  for (const char c : rawMessage_) {
118  if (c == '\\') {
119  message_.push_back('\\');
120  message_.push_back('\\');
121  } else if (static_cast<unsigned char>(c) < 0x20) {
122  if (c == '\n' || c == '\t') {
123  message_.push_back(c);
124  } else {
125  static constexpr StringPiece hexdigits{"0123456789abcdef"};
126  std::array<char, 4> data{
127  {'\\', 'x', hexdigits[(c >> 4) & 0xf], hexdigits[c & 0xf]}};
128  message_.append(data.data(), data.size());
129  }
130  } else if (c == 0x7f) {
131  constexpr std::array<char, 4> data{{'\\', 'x', '7', 'f'}};
132  message_.append(data.data(), data.size());
133  } else {
134  message_.push_back(c);
135  }
136  }
137 }
std::string const rawMessage_
Definition: LogMessage.h:158
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
std::string message_
Definition: LogMessage.h:168
Range< const char * > StringPiece
char c

Member Data Documentation

const LogCategory* const folly::LogMessage::category_ {nullptr}
private

Definition at line 124 of file LogMessage.h.

Referenced by getCategory(), and LogMessage().

bool folly::LogMessage::containsNewlines_ {false}
private

containsNewlines_ will be true if the message contains internal newlines.

This allows log handlers that perform special handling of multi-line messages to easily detect if a message contains multiple lines or not.

Definition at line 150 of file LogMessage.h.

Referenced by containsNewlines(), and sanitizeMessage().

folly::StringPiece const folly::LogMessage::filename_
private

The name of the source file that generated this log message.

Definition at line 132 of file LogMessage.h.

Referenced by getFileBaseName(), getFileName(), and LogMessage().

folly::StringPiece const folly::LogMessage::functionName_
private

The name of the function that generated this log message.

Definition at line 142 of file LogMessage.h.

Referenced by getFunctionName(), and LogMessage().

LogLevel const folly::LogMessage::level_ {static_cast<LogLevel>(0)}
private

Definition at line 125 of file LogMessage.h.

Referenced by getLevel(), and LogMessage().

unsigned int const folly::LogMessage::lineNumber_ {0}
private

The line number in the source file that generated this log message.

Definition at line 137 of file LogMessage.h.

Referenced by getLineNumber(), and LogMessage().

std::string folly::LogMessage::message_
private

message_ contains a sanitized version of the log message.

nul bytes and unprintable characters have been escaped. This message may still contain newlines, however. LogHandler classes are responsible for deciding how they want to handle log messages with internal newlines.

Definition at line 168 of file LogMessage.h.

Referenced by getMessage(), and sanitizeMessage().

std::string const folly::LogMessage::rawMessage_
private

rawMessage_ contains the original message.

This may contain arbitrary binary data, including unprintable characters and nul bytes.

Definition at line 158 of file LogMessage.h.

Referenced by getMessage(), getRawMessage(), LogMessage(), and sanitizeMessage().

uint64_t const folly::LogMessage::threadID_ {0}
private

Definition at line 126 of file LogMessage.h.

Referenced by getThreadID(), and LogMessage().

std::chrono::system_clock::time_point const folly::LogMessage::timestamp_
private

Definition at line 127 of file LogMessage.h.

Referenced by getTimestamp(), and LogMessage().


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