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?
When you pass the :verbose flag to a FileUtils command, the command gets printed
Share
If you look at the source for
FileUtilsit uses the following method for doing its verbose output:i.e. it is writing the messages to
@fileutils_outputand by default it is using$stderr. There doesn’t seem to be a method to alter@fileutils_outputbut 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::Verbosewhich basically includesFileUtils(so has all the same methods) but defaults the options to:verbose => trueso 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
$stderrbut as he says this does mean that everything written to stderr (not just byFileUtils) is redirected. If all theFileUtilsoperations 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
FileUtilsand overridefu_output_message(msg)itself if you need more control.