[cfe-dev] How to redirect outs() to file?
Anton Smirnov
dev at antonsmirnov.name
Sat Oct 11 10:25:55 PDT 2014
Hi.
I'm using some LLVM tools (like llvm-nm) as static libraries. I.e. i copied
source llvm-nm.cpp, renamed main(..) to llvm_nm(..) and compiled it as
static library. I'd like to forward standard output to my file.
I've tried to use the next approach:
int out_fd, err_fd;
fpos_t out_pos, err_pos;
// redirect out
fflush(stdout);
fgetpos(stdout, &out_pos);
out_fd = dup(fileno(stdout));
freopen(outFilename, "w", stdout);
// execute
int ret = llvm_nm(argc_, argv_);
// restore output
fflush(stdout);
dup2(out_fd, fileno(stdout));
close(out_fd);
clearerr(stdout);
fsetpos(stdout, &out_pos);
The problem is that it's not forwarded (it works if i add printf() in nm
source code but not for nm output). I've looked to the source code and i
can see output is done using llvm::outs() stream:
outs() << "Archive map" << "\n";
And it's implemented
<http://llvm.org/docs/doxygen/html/raw__ostream_8cpp_source.html> the next
way:
/// outs() - This returns a reference to a raw_ostream for standard
output.00702 /// Use it like: outs() << "foo" << "bar";00703
raw_ostream &llvm::outs() {00704 // Set buffer settings to model
stdout behavior.00705 // Delete the file descriptor when the program
exits, forcing error00706 // detection. If you don't want this
behavior, don't use outs().00707 static raw_fd_ostream
S(STDOUT_FILENO, true);00708 return S;00709 }
How can i redirect that output to my file?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141011/3541a1f6/attachment.html>
More information about the cfe-dev
mailing list