When you pass the :verbose
flag to a FileUtils command, the command gets printed to STDOUT. Is there a way to capture the command so it can be logged or used elsewhere?
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- gem cleanup shows error: Unable to uninstall bundl
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
- ruby - simplify string multiply concatenation
If you look at the source for
FileUtils
it uses the following method for doing its verbose output:i.e. it is writing the messages to
@fileutils_output
and by default it is using$stderr
. There doesn't seem to be a method to alter@fileutils_output
but you could add one:Then if you wanted to capture the commands into a file you could do:
or if you wanted to get them in a string you could do:
Also, there is a module
FileUtils::Verbose
which basically includesFileUtils
(so has all the same methods) but defaults the options to:verbose => true
so if you wanted to capture lots of commands you could use this instead of specifying the option each time. (you would need to add thefileutils_output=
method to this module in the same way as above.)Alternatives
As Joshua says in the comments below, an alternative is to reassign
$stderr
but as he says this does mean that everything written to stderr (not just byFileUtils
) is redirected. If all theFileUtils
operations are happening in one go without anything else in between then this might not be an issue. So something along the lines of:Finally, you could reopen
FileUtils
and overridefu_output_message(msg)
itself if you need more control.To add to Mike's answer (since I can't comment), I created this wrapper
def
if you want to get the output as a string:I ended up not using it since I wanted to revert back to
@fileutils_output ||= $stderr
afterwards.