Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/googletest.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,12 @@ static inline string GetCapturedTestStderr() {
return GetCapturedTestOutput(STDERR_FILENO);
}

static const std::size_t kLoggingPrefixLength = 9;

// Check if the string is [IWEF](\d{8}|YEARDATE)
static inline bool IsLoggingPrefix(const string& s) {
if (s.size() != 9) return false;
if (s.size() != kLoggingPrefixLength)
return false;
if (!strchr("IWEF", s[0])) return false;
for (size_t i = 1; i <= 8; ++i) {
if (!isdigit(s[i]) && s[i] != "YEARDATE"[i-1]) return false;
Expand All @@ -421,17 +424,22 @@ static inline bool IsLoggingPrefix(const string& s) {
// I20200102 030405 logging_unittest.cc:345] RAW: vlog -1
// => IYEARDATE TIME__ logging_unittest.cc:LINE] RAW: vlog -1
static inline string MungeLine(const string& line) {
std::istringstream iss(line);
string before, logcode_date, time, thread_lineinfo;
iss >> logcode_date;
while (!IsLoggingPrefix(logcode_date)) {
before += " " + logcode_date;
if (!(iss >> logcode_date)) {
// We cannot find the header of log output.
return before;
std::size_t begin_of_logging_prefix = 0;
for (; begin_of_logging_prefix + kLoggingPrefixLength < line.size();
++begin_of_logging_prefix) {
if (IsLoggingPrefix(
line.substr(begin_of_logging_prefix, kLoggingPrefixLength))) {
break;
}
}
if (!before.empty()) before += " ";
if (begin_of_logging_prefix + kLoggingPrefixLength >= line.size()) {
return line;
} else if (begin_of_logging_prefix > 0) {
before = line.substr(0, begin_of_logging_prefix - 1);
}
std::istringstream iss(line.substr(begin_of_logging_prefix));
iss >> logcode_date;
iss >> time;
iss >> thread_lineinfo;
CHECK(!thread_lineinfo.empty());
Expand Down
5 changes: 4 additions & 1 deletion src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,6 @@ void LogMessage::Init(const char* file,
}
}

stream().fill('0');
data_->preserved_errno_ = errno;
data_->severity_ = severity;
data_->line_ = line;
Expand All @@ -1614,6 +1613,9 @@ void LogMessage::Init(const char* file,
// (log level, GMT year, month, date, time, thread_id, file basename, line)
// We exclude the thread_id for the default thread.
if (FLAGS_log_prefix && (line != kNoLogPrefix)) {
std::ios saved_fmt(NULL);
saved_fmt.copyfmt(stream());
stream().fill('0');
#ifdef GLOG_CUSTOM_PREFIX_SUPPORT
if (custom_prefix_callback == NULL) {
#endif
Expand Down Expand Up @@ -1645,6 +1647,7 @@ void LogMessage::Init(const char* file,
stream() << " ";
}
#endif
stream().copyfmt(saved_fmt);
}
data_->num_prefix_chars_ = data_->stream_.pcount();

Expand Down
3 changes: 2 additions & 1 deletion src/logging_custom_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ void TestLogging(bool check_counts) {
int j = 1000;
LOG(ERROR) << string("foo") << ' '<< j << ' ' << setw(10) << j << " "
<< setw(1) << hex << j;
LOG(INFO) << "foo " << std::setw(10) << 1.0;

{
google::LogMessage outer(__FILE__, __LINE__, GLOG_ERROR);
Expand All @@ -321,7 +322,7 @@ void TestLogging(bool check_counts) {
LogMessage("foo", LogMessage::kNoLogPrefix, GLOG_INFO).stream() << "no prefix";

if (check_counts) {
CHECK_EQ(base_num_infos + 14, LogMessage::num_messages(GLOG_INFO));
CHECK_EQ(base_num_infos + 15, LogMessage::num_messages(GLOG_INFO));
CHECK_EQ(base_num_warning + 3, LogMessage::num_messages(GLOG_WARNING));
CHECK_EQ(base_num_errors + 17, LogMessage::num_messages(GLOG_ERROR));
}
Expand Down
3 changes: 2 additions & 1 deletion src/logging_custom_prefix_unittest.err
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ IYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] Log if every 1
WYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] log_if this
IYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] array
IYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] const array
EYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] foo 1000 0000001000 3e8
EYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] foo 1000 1000 3e8
IYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] foo 1
EYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] inner
EYEARDATE TIME__ THREADID logging_custom_prefix_unittest.cc:LINE] outer
no prefix
Expand Down
3 changes: 2 additions & 1 deletion src/logging_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ void TestLogging(bool check_counts) {
int j = 1000;
LOG(ERROR) << string("foo") << ' '<< j << ' ' << setw(10) << j << " "
<< setw(1) << hex << j;
LOG(INFO) << "foo " << std::setw(10) << 1.0;

{
google::LogMessage outer(__FILE__, __LINE__, GLOG_ERROR);
Expand All @@ -298,7 +299,7 @@ void TestLogging(bool check_counts) {
LogMessage("foo", LogMessage::kNoLogPrefix, GLOG_INFO).stream() << "no prefix";

if (check_counts) {
CHECK_EQ(base_num_infos + 14, LogMessage::num_messages(GLOG_INFO));
CHECK_EQ(base_num_infos + 15, LogMessage::num_messages(GLOG_INFO));
CHECK_EQ(base_num_warning + 3, LogMessage::num_messages(GLOG_WARNING));
CHECK_EQ(base_num_errors + 17, LogMessage::num_messages(GLOG_ERROR));
}
Expand Down
3 changes: 2 additions & 1 deletion src/logging_unittest.err
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ IYEARDATE TIME__ THREADID logging_unittest.cc:LINE] Log if every 1, iteration 10
WYEARDATE TIME__ THREADID logging_unittest.cc:LINE] log_if this
IYEARDATE TIME__ THREADID logging_unittest.cc:LINE] array
IYEARDATE TIME__ THREADID logging_unittest.cc:LINE] const array
EYEARDATE TIME__ THREADID logging_unittest.cc:LINE] foo 1000 0000001000 3e8
EYEARDATE TIME__ THREADID logging_unittest.cc:LINE] foo 1000 1000 3e8
IYEARDATE TIME__ THREADID logging_unittest.cc:LINE] foo 1
EYEARDATE TIME__ THREADID logging_unittest.cc:LINE] inner
EYEARDATE TIME__ THREADID logging_unittest.cc:LINE] outer
no prefix
Expand Down