Commit fcfaecbc authored by Phil Willoughby's avatar Phil Willoughby Committed by Facebook Github Bot

make glogstyleformatter stop guessing

Summary: Might as well store the count of newlines in the Log Message - as the field is already padded to <pointersize> bytes by the alignment requirements of the field either side it will not use any more space than the existing `bool`

Reviewed By: simpkins

Differential Revision: D20200298

fbshipit-source-id: 84cc52898069eae1af5bd0f103d4ab3177c45206
parent 6c881171
......@@ -96,12 +96,8 @@ std::string GlogStyleFormatter::formatMessage(
header.reserve(headerLengthGuess);
headerFormatter.appendTo(header);
// Make a guess at how many lines will be in the message, just to make an
// initial buffer allocation. If the guess is too small then the string
// will reallocate and grow as necessary, it will just be slightly less
// efficient than if we had guessed enough space.
size_t numLinesGuess = 4;
buffer.reserve(((header.size() + 1) * numLinesGuess) + msgData.size());
buffer.reserve(
((header.size() + 1) * message.getNumNewlines()) + msgData.size());
size_t idx = 0;
while (true) {
......
......@@ -84,6 +84,7 @@ StringPiece LogMessage::getFileBaseName() const {
void LogMessage::sanitizeMessage() {
// Compute how long the sanitized string will be.
size_t sanitizedLength = 0;
size_t numNewlines = 0;
for (const char c : rawMessage_) {
if (c == '\\') {
// Backslashes are escaped as two backslashes
......@@ -93,7 +94,7 @@ void LogMessage::sanitizeMessage() {
// All other control characters are emitted as \xNN (4 characters)
if (c == '\n') {
sanitizedLength += 1;
containsNewlines_ = true;
++numNewlines;
} else if (c == '\t') {
sanitizedLength += 1;
} else {
......@@ -107,7 +108,7 @@ void LogMessage::sanitizeMessage() {
++sanitizedLength;
}
}
numNewlines_ = numNewlines;
// If nothing is different, just use rawMessage_ directly,
// and don't populate message_.
if (sanitizedLength == rawMessage_.size()) {
......
......@@ -116,7 +116,11 @@ class LogMessage {
}
bool containsNewlines() const {
return containsNewlines_;
return numNewlines_ > 0;
}
size_t getNumNewlines() const {
return numNewlines_;
}
private:
......@@ -143,12 +147,13 @@ class LogMessage {
folly::StringPiece const functionName_;
/**
* containsNewlines_ will be true if the message contains internal newlines.
* containedNewlines_ counts the number of internal newlines in the message.
*
* This allows log handlers that perform special handling of multi-line
* messages to easily detect if a message contains multiple lines or not.
* messages to easily detect if a message contains multiple lines or not and
* size their buffers appropriately.
*/
bool containsNewlines_{false};
size_t numNewlines_{0};
/**
* rawMessage_ contains the original message.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment