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

Add option to set the binary mode of the log device #33

Merged
merged 1 commit into from
Jun 19, 2019

Conversation

rafaelfranca
Copy link
Contributor

@rafaelfranca rafaelfranca commented Jun 4, 2019

Without binmode strings with incompatible encoding can't be written in
the file. This is very common in applications that log user provided
parameters.

We need to allow changing the binnary mode because right now it is impossible to use
the built-in log rotation feature when you provide a File object to the
LogDevice, and if you provide a filename you can't have binmode.

@rafaelfranca
Copy link
Contributor Author

cc @sonots @hsbt @tenderlove

@sonots
Copy link
Member

sonots commented Jun 4, 2019

Does changing defaults to binmode break any backward compatibilities? For example, what if a log file is already written with previous version of loggers?

If there are potential risks breaking backward compatibilities, I prefer adding another option.

@amatsuda
Copy link
Member

amatsuda commented Jun 5, 2019

Let's summon @nobu for this kind of quiz.

@rafaelfranca
Copy link
Contributor Author

It seems there is no backward incompatibility between the two implementation. Both creating a file using binmode and appending to it without binmode and, creating a file without binmode and appending to it with binmode work fine.

>> f = File.open("tmp.txt", (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
=> #<File:tmp.txt>
>> f.binmode
=> #<File:tmp.txt>
>> f.write("\x80".force_encoding("ASCII-8BIT"))
=> 1
>> f.close
=> nil
>> f = File.open("tmp.txt", (File::WRONLY | File::APPEND))
=> #<File:tmp.txt>
>> f.write("a")
=> 1
>> f.close
=> nil
>> f = File.open("not_binmode.txt", (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
=> #<File:not_binmode.txt>
>> f.write("a")
=> 1
>> f.close
=> nil
>> f = File.open("not_binmode.txt", (File::WRONLY | File::APPEND))
=> #<File:not_binmode.txt>
>>
>> f.write("\x80".force_encoding("ASCII-8BIT"))
=> 1
>> f.close
=> nil

@ioquatix
Copy link
Member

ioquatix commented Jun 5, 2019

This looks good, the only question I'd ask is should we change the mode of the file if it's supplied by the user?

@rafaelfranca
Copy link
Contributor Author

This looks good, the only question I'd ask is should we change the mode of the file if it's supplied by the user?

I think that should be up to the user. They should be able to pass a file using any mode they want.

@ioquatix
Copy link
Member

ioquatix commented Jun 5, 2019

Okay one last point - do you want to update the documentation too so users know what's going on?

@rafaelfranca
Copy link
Contributor Author

Yeah, that one I believe we should. I'll take a look.

@rafaelfranca
Copy link
Contributor Author

Done

@rafaelfranca rafaelfranca force-pushed the rm-default-logger-dev-to-binmode branch from 0061b3b to 496c69e Compare June 5, 2019 22:23
@ioquatix
Copy link
Member

ioquatix commented Jun 5, 2019

LGTM, but I'll let @sonots merge it.

@sonots
Copy link
Member

sonots commented Jun 5, 2019

As far as I know, on windows, binmode disables newline conversion. It breaks backward compatible behaviors, doesn't it?

@sonots
Copy link
Member

sonots commented Jun 6, 2019

Also, someone may rely on encoding conversion feature of the text mode like writing source codes and log texts in EUC-JP and outputting logs with EUC-JP on Linux and Shift_JIS on windows using the text mode feature. (although I believe recent Japanese uses only UTF-8 on both environment).

@sonots
Copy link
Member

sonots commented Jun 6, 2019

I agree the preference making defaults to binmode, I definitely change to binmode if this is my own personal gem. But, I am very conservative about this ruby standard library.

@nobu
Copy link
Member

nobu commented Jun 6, 2019

I don't see any failure in your test_write_binary_data, with the current ruby.

test/logger/test_logdevice.rb Outdated Show resolved Hide resolved
@rafaelfranca rafaelfranca force-pushed the rm-default-logger-dev-to-binmode branch from 496c69e to 9e69caa Compare June 6, 2019 16:45
@sonots
Copy link
Member

sonots commented Jun 6, 2019

Decided. Could you provide a new option like binmode: true and default to false not to break backward compatibility?

str = "\x80"
str.force_encoding("ASCII-8BIT")
logdev.write(str)
logfile = File.open(@filename)
Copy link
Member

Choose a reason for hiding this comment

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

logfile is left open.
Use File.binread.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@rafaelfranca
Copy link
Contributor Author

Could you provide a new option like binmode: true and default to false not to break backward compatibility?

Of course. Working on it.

Without binmode strings with incompatible encoding can't be written in
the file. This is very common in applications that log user provided
parameters.

We need to allow changing the binnary mode because right now it is impossible to use
the built-in log rotation feature when you provide a File object to the
LogDevice, and if you provide a filename you can't have binmode.
@rafaelfranca rafaelfranca force-pushed the rm-default-logger-dev-to-binmode branch from 9e69caa to 9114b3a Compare June 7, 2019 20:07
@rafaelfranca rafaelfranca changed the title Default log device to binmode when it is a file Add option to set the binary mode of the log device Jun 7, 2019
@rafaelfranca
Copy link
Contributor Author

Changed this to take an option and kept the default as it was.

Copy link
Member

@sonots sonots left a comment

Choose a reason for hiding this comment

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

Sorry to be late. LGTM

@sonots sonots merged commit bca4591 into ruby:master Jun 19, 2019
@rafaelfranca
Copy link
Contributor Author

Thank you!

@ioquatix
Copy link
Member

This was a great example of everyone working together and understanding each others requirements. Thanks for being great software engineers and human beings!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants