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