restore the stream format after writing the log prefix (#731)
* Fix MungeLine() for log lines that are longer than 5 characters * Match order of log statements in logging_unittest.cc and logging_unittest.err * Update logging_custom_prefix_unittest.cc and logging_custom_prefix_unittest.err to match logging_unittest.cc and logging_unittest.err * Save the stream state and set the fill character before calling into a custom_prefix_callback
This commit is contained in:
parent
68964b8db6
commit
33696664cb
@ -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;
|
||||
@ -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());
|
||||
|
||||
@ -1601,7 +1601,6 @@ void LogMessage::Init(const char* file,
|
||||
}
|
||||
}
|
||||
|
||||
stream().fill('0');
|
||||
data_->preserved_errno_ = errno;
|
||||
data_->severity_ = severity;
|
||||
data_->line_ = line;
|
||||
@ -1627,6 +1626,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
|
||||
@ -1658,6 +1660,7 @@ void LogMessage::Init(const char* file,
|
||||
stream() << " ";
|
||||
}
|
||||
#endif
|
||||
stream().copyfmt(saved_fmt);
|
||||
}
|
||||
data_->num_prefix_chars_ = data_->stream_.pcount();
|
||||
|
||||
|
||||
@ -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);
|
||||
@ -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));
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
@ -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));
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user