0
我正在编写一个程序,它由各种子程序组成,每个子程序都记录它们自己的错误。在执行结束时,我想将遇到的所有错误转储到记录它们的程序标记的屏幕上。
理想我想有这样的事情:
Encountered 4 errors during compilation (STDOUT)
Tokenizer Errors: (STDOUT)
Line 4: Invalid symbol found '@' (STDERR)
Parser Errors: (STDOUT)
Line 7: expected 'end' but saw 'while' (STDERR)
Line 7: expected ';' but saw 'while' (STDERR)
Line 12 expected '.' but saw 'end' (STDERR)
但是当我运行我的程序$标准输出线和$ STDERR线混错。我知道,$标准输出缓冲器和$标准错误没有,所以我加了这行,打印输出的方法:我已被告知要同步的两个输出流
$stdout.sync = $stderr.sync = true
。但不幸的是,我仍然收到混乱的输出。在我想发送给$ stdout的每一行之后,我也尝试添加$stdout.flush
,但这似乎也没有效果。
这里是我使用的代码:
def self.dump()
$stdout.sync = $stderr.sync = true # attempted sync
if not @@lexer_error_log.empty? or not @@parser_error_log.empty?
puts "Encountered #{@@err_cnt} errors during compilation\n"
$stdout.flush #flush
end
if not @@lexer_error_log.empty?
puts "\nTokenizer Errors:"
$stdout.flush # flush
@@lexer_error_log.each {|e| $stderr.puts "\t#{e}\n" }
end
if not @@parser_error_log.empty?
puts "Parser Errors:"
$stdout.flush # flush
@@parser_error_log.each {|e| $stderr.puts "\t#{e}\n" }
end
end
任何帮助,将不胜感激。
由于''n'''puts'的添加是作为一个单独的写入完成的,您可能想要向'StringIO'输出'\ n'并使用'print'而不是'puts'。 – smparkes 2012-11-07 22:57:58