【问题标题】:Exporting g++ compilation error messages from TMB library to a text file将 g++ 编译错误消息从 TMB 库导出到文本文件
【发布时间】:2016-03-07 16:36:13
【问题描述】:

我正在尝试在 R 中使用 gcc (c++) 编译模型(使用 TMB 包)。错误如此之多,以至于在 Rstudio 中,我什至无法向上滚动查看它们的开头。因此,我想将控制台中的所有内容(消息、错误和警告)打印到文本文件中。这样我还可以比较不同的模型输出(或者特别是它们的失败)。

我尝试了以下方法:

fileConn<-file("Fail.txt")
writeLines(compile("Mymodel.cpp"), fileConn) 
close(fileConn)

=> 给了我一个空文件(我或多或少期望)

zz <- file("Fail.txt", open = "wt")
sink(zz, type = "message")
compile("Mymodel.cpp")
sink()

=> 只打印最终错误

我忽略了什么?

【问题讨论】:

  • 在第一个 sink 之后添加 sink(zz, type = "output")。通过这种方式,您可以将stdoutstderr 转移到您选择的文件中。
  • 试过了,但这仍然只给我最后的错误......
  • 你确定吗?请提供一个具有预期输出的可重现示例。

标签: c++ r rstudio sink tmb


【解决方案1】:

您尚未提供您的 compile() 函数,但我假设它运行一个调用 g++ 来编译 Mymodel.cpp 的系统命令。

在这种情况下,g++ 进程会将其错误输出打印到 stderr。使用 R 捕获此输出的唯一方法是使用 stderr=T 调用 system2()。请注意,system() 不具备直接捕获 stderr 的能力(尽管它可以通过 intern=T 捕获 stdout,并且您可以将 2&gt;&amp;1 添加到 shell 命令中以捕获 stderr,因此这是另一个可行的选择)。 sink() 不捕获系统命令输出;它只捕获 R 输出。

我建议将您的compile() 函数中的可变参数传递给system2() 调用,从而参数化您对compile() 的调用是否导致g++ 的stderr 进入终端或@ 的返回值987654339@ 功能。这是如何完成的:

write('error!','test1.cpp'); ## generate a test file with invalid C++
compile <- function(file,...) system2('g++',file,...);
compile('test1.cpp'); ## output lost to the terminal
## test1.cpp:1:1: error: ‘error’ does not name a type
##  error!
##  ^
output <- compile('test1.cpp',stdout=T,stderr=T); ## capture output
## Warning message:
## running command ''g++' 'test1.cpp' 2>&1' had status 1
output;
## [1] "test1.cpp:1:1: error: ‘error’ does not name a type"
## [2] " error!"
## [3] " ^"
## attr(,"status")
## [1] 1
write(output,'output.txt'); ## write output to a text file
cat(readLines('output.txt'),sep='\n'); ## show it
## test1.cpp:1:1: error: ‘error’ does not name a type
##  error!
##  ^

如果您真的想捕获compile() 函数中生成的所有 输出,则可以将上述解决方案与sink() 结合使用,如下所示:How to save all console output to file in R?

在这种情况下,我建议放弃可变参数的想法,并为compile() 添加一个附加参数,它将采用一个输出文件名,所有输出都将写入其中。

这将需要对附加参数缺失的几个预测:

write('error!','test1.cpp'); ## generate a test file with invalid C++
compile <- function(file,outputFile) {
    if (!missing(outputFile)) {
        outputCon <- file(outputFile,'wt'); ## require file name
        sink(outputCon);
        sink(outputCon,type='message'); ## must sink messages separately
        warn.old <- options(warn=1)$warn; ## necessary to capture warnings as they occur
    }; ## end if
    cat('some random output 1\n');
    if (!missing(outputFile)) {
        output <- system2('g++',file,stdout=T,stderr=T); ## before flush to get warnings
        sink(); ## force flush before appending system command output
        sink(type='message');
        outputCon <- file(outputFile,'at'); ## must reopen connection for appending
        write(output,outputCon);
        sink(outputCon);
        sink(outputCon,type='message');
    } else {
        system2('g++',file);
    }; ## end if
    cat('some random output 2\n');
    if (!missing(outputFile)) {
        sink();
        sink(type='message');
        options(warn=warn.old);
    }; ## end if
}; ## end compile()
compile('test1.cpp'); ## output lost to the terminal
## some random output 1
## test1.cpp:1:1: error: ‘error’ does not name a type
##  error!
##  ^
## some random output 2
compile('test1.cpp','output.txt'); ## internally capture all output
cat(readLines('output.txt'),sep='\n'); ## show it
## some random output 1
## Warning: running command ''g++' test1.cpp 2>&1' had status 1
## test1.cpp:1:1: error: ‘error’ does not name a type
##  error!
##  ^
## some random output 2

【讨论】:

  • 谢谢,我学到了一些新东西。但是,当我这样做时,它只会再次创建一个空文件。我也尝试了 system(test1.dpp, intern=T) 和 system2("test1.cpp", stdout=TRUE,stdout=TRUE) ,但似乎都没有给我想要的结果(只有函数 (x) UseMethod( “t”))。我很抱歉提到 compile() 来自 TMB 包(并且做你认为它做的事情)
  • 在您粘贴到评论中的system()system2() 调用中,您的shell 命令语法不正确; g++ 必须是命令中的第一个单词。此外,在您的system2() 调用中,您指定了两次stdout=T,而不是stdout=T,stderr=T。此外,您粘贴的输出看起来像您运行 t; 时得到的输出,IOW 基本 R t() 函数的默认转储,这与此问题无关。最后,我查看了TMB::compile(),它没有提供捕获其系统命令输出的工具,因此您无法通过TMB::compile() 实现此要求。
  • 好的,谢谢!哎呀,我完全搞砸了复制粘贴/重新输入。对不起:s
【解决方案2】:

这是一个老问题,但我找到了一个简单的解决方案来获取TMB::compile 的日志文件输出,我在其他任何地方都没有找到。
如上所述, sink() 不会捕获系统命令输出;它只捕获 R 输出。 但是,库 TMB 的函数 compile 中未列出的任何参数都作为 Makeconf 变量传递。因此,您只需要以字符串格式传递带有目标日志文件路径的经典输出命令:

TMB::compile(file = "Mymodel.cpp", "&> /tmp/logfile.log")

【讨论】:

  • 一种比公认答案更优雅的方法!
猜你喜欢
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 2010-12-21
  • 2011-07-29
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多