首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将g++编译错误消息从TMB库导出到文本文件

将g++编译错误消息从TMB库导出到文本文件
EN

Stack Overflow用户
提问于 2016-03-07 16:36:13
回答 2查看 738关注 0票数 2

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

我尝试了以下几点:

代码语言:javascript
复制
fileConn<-file("Fail.txt")
writeLines(compile("Mymodel.cpp"), fileConn) 
close(fileConn)

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

代码语言:javascript
复制
zz <- file("Fail.txt", open = "wt")
sink(zz, type = "message")
compile("Mymodel.cpp")
sink()

=>只打印最终错误

我忽略了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-07 17:00:33

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

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

我建议将各种参数从compile()函数传递到system2()调用,从而参数化对compile()的调用是否会导致g++的stderr到达终端或compile()函数的返回值。

代码语言:javascript
复制
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()提供一个附加参数,该参数将采用输出文件名,所有输出都将写入该文件名。

这将需要对附加论点的不正确进行几个预测:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2017-07-07 13:48:06

这是一个老问题,但我找到了一个简单的解决方案来获得TMB::compile的日志文件输出,而我在其他地方没有找到这个解决方案。

如前所述,接收器()不捕获系统命令输出;它只捕获R输出。但是,函数compile中没有列出的来自库TMB的任何参数都作为Makeconf变量传递。因此,只需以字符串格式传递具有指向目标日志文件的路径的经典输出命令:

代码语言:javascript
复制
TMB::compile(file = "Mymodel.cpp", "&> /tmp/logfile.log")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35849075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档