Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logsink: fix multiple issues with LogSink::ToString() #852

Merged
merged 1 commit into from
Aug 13, 2022
Merged

logsink: fix multiple issues with LogSink::ToString() #852

merged 1 commit into from
Aug 13, 2022

Conversation

anpol
Copy link
Contributor

@anpol anpol commented Aug 13, 2022

  1. Initializing std::ostringstream with a string makes no sense, as the string becomes an initial value of an underlying buffer; seek-to-end is not performed, so the initial value gets completely overwritten by subsequent writing.

  2. Flag log_year_in_prefix should be considered, as if formatting a regular logging message.

  3. Writing a buffer to std::ostream is better expressed with stream.write(s,n).

@codecov-commenter
Copy link

codecov-commenter commented Aug 13, 2022

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.21%. Comparing base (278ed96) to head (a1608cc).
Report is 174 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #852      +/-   ##
==========================================
+ Coverage   73.17%   73.21%   +0.04%     
==========================================
  Files          17       17              
  Lines        3277     3282       +5     
==========================================
+ Hits         2398     2403       +5     
  Misses        879      879              
Files with missing lines Coverage Δ
src/logging.cc 74.76% <100.00%> (+0.09%) ⬆️
src/windows/port.h 100.00% <ø> (ø)

... and 1 file with indirect coverage changes

Copy link
Collaborator

@sergiud sergiud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR. Looks mostly good to me. However, it would be great if you can clarify why write is expected to be defined as a macro by MSVC. I'm not confident this is actually right.

src/logging.cc Outdated
Comment on lines 2137 to 2140
#ifdef _MSC_VER
#pragma push_macro("write")
#undef write
#endif // _MSC_VER
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate why this is suddenly necessary? I'm not seeing any warnings issued by Github runners.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error was

'_write': is not a member of 'std::ostringstream
https://buildkite.com/bazel/google-logging/builds/2869#01829651-f13d-4665-9c42-dfd29c8ac36a

The source of the problem is

src/windows/port.h:87:#define write   _write

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Instead of undefing the macro, you can prevent its expansion by protecting the write calls as follows:

(stream.write)(message, static_cast<std::streamsize>(message_len));

Please also add a comment for the reason why macro expansion is prevented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thank you. That would require redefining write macro with three parameters.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I missed that.

1. Initializing std::ostringstream with a string makes no sense, as the
   string becomes an initial value of an underlying buffer; seek-to-end
   is not performed, so the initial value gets completely overwritten by
   subsequent writing.

2. Flag `log_year_in_prefix` should be considered, as if formatting a
   regular logging message.

3. Writing a buffer to std::ostream is better expressed with write(s,n).
@sergiud sergiud merged commit acc60d0 into google:master Aug 13, 2022
@sergiud sergiud added this to the 0.7 milestone Aug 13, 2022
@anpol anpol deleted the log-sink-to-string branch August 13, 2022 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants