问题描述
当您将:verbose
标志传递给FileUtils命令时,该命令会打印到STDOUT。是否有一种方法可以捕获该命令,以便可以在其他地方记录或使用它? 如果您查看 FileUtils
它使用以下方法来完成它的详细输出:
def fu_output_message (msg)#:nodoc:
@fileutils_output || = $ stderr
@fileutils_label || =''
@ fileutils_output.puts @fileutils_label + msg
结束
即它将消息写入 @fileutils_output
,默认情况下它使用 $ stderr
。似乎没有方法可以改变 @fileutils_output
,但您可以添加一个:
module FileUtils
def FileUtils.fileutils_output =(new_out)
@fileutils_output = new_out
end
end
然后,如果您想将命令捕获到文件中,您可以这样做:
my_fu_log = open('fu_log.log','w')
FileUtils.fileutils_output = my_fu_log
#FileUtils操作:verbose => true here
my_fu_log.close
FileUtils.fileutils_output = $ stderr#如果要
$,则还原写入到stderr b $ b
或者如果你想让它们在一个字符串中,你可以这样做:
log = StringIO.new
FileUtils.fileutils_output = log
#FileUtils操作:verbose => true在这里
#命令在log.string
另外,还有一个模块 FileUtils :: Verbose
它基本上包含 FileUtils
(所有方法都一样),但默认选项为:verbose => true
,所以如果你想捕获很多命令,你可以使用它而不是每次指定选项。 (您需要像上面一样将 fileutils_output =
方法添加到此模块中。)
替代品
作为在下面的评论中说,另一种方法是重新分配 $ stderr
,但正如他所说的,这意味着所有写入stderr的东西(不仅仅是 FileUtils
)被重定向。如果所有的 FileUtils
操作都是一次性发生而没有其他任何东西,那么这可能不是问题。所以,我们有以下几点:
orig_stderr = $ stderr#保留原始stderr的引用
$ stderr = my_fu_log
#在这里使用FileUtils
$ stderr = orig_stderr#restore stderr
最后,你如果您需要更多的控制,可以重新打开 FileUtils
并覆盖 fu_output_message(msg)
本身。
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?
If you look at the source for FileUtils
it uses the following method for doing its verbose output:
def fu_output_message(msg) #:nodoc:
@fileutils_output ||= $stderr
@fileutils_label ||= ''
@fileutils_output.puts @fileutils_label + msg
end
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:
module FileUtils
def FileUtils.fileutils_output=(new_out)
@fileutils_output = new_out
end
end
Then if you wanted to capture the commands into a file you could do:
my_fu_log = open('fu_log.log', 'w')
FileUtils.fileutils_output = my_fu_log
# FileUtils operations with :verbose => true here
my_fu_log.close
FileUtils.fileutils_output = $stderr # restore writing to stderr if you want
or if you wanted to get them in a string you could do:
log = StringIO.new
FileUtils.fileutils_output = log
# FileUtils operations with :verbose => true here
# commands are in log.string
Also, there is a module FileUtils::Verbose
which basically includes FileUtils
(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 the fileutils_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 by FileUtils
) is redirected. If all the FileUtils
operations are happening in one go without anything else in between then this might not be an issue. So something along the lines of:
orig_stderr = $stderr # keep reference to original stderr
$stderr = my_fu_log
# use FileUtils here
$stderr = orig_stderr # restore stderr
Finally, you could reopen FileUtils
and override fu_output_message(msg)
itself if you need more control.
这篇关于从FileUtils获取执行的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!