12

我正在寻找一种在 C 中以编程方式(即,不使用命令行重定向)实现“tee”功能的方法,以便我的标准输出同时进入标准输出和日志文件。这需要对我的代码和输出到标准输出的所有链接库都有效。有什么办法可以做到这一点?

4

5 回答 5

7

你可以popen()tee 程序。

或者您可以通过这样的子进程进行fork()管道stdout传输(改编自我编写的真实实时程序,所以它可以工作!):

void tee(const char* fname) {
    int pipe_fd[2];
    check(pipe(pipe_fd));
    const pid_t pid = fork();
    check(pid);
    if(!pid) { // our log child
        close(pipe_fd[1]); // Close unused write end
        FILE* logFile = fname? fopen(fname,"a"): NULL;
        if(fname && !logFile)
            fprintf(stderr,"cannot open log file \"%s\": %d (%s)\n",fname,errno,strerror(errno));
        char ch;
        while(read(pipe_fd[0],&ch,1) > 0) {
            //### any timestamp logic or whatever here
            putchar(ch);
            if(logFile)
                fputc(ch,logFile);
            if('\n'==ch) {
                fflush(stdout);
                if(logFile)
                    fflush(logFile);
            }
        }
        putchar('\n');
        close(pipe_fd[0]);
        if(logFile)
            fclose(logFile);
        exit(EXIT_SUCCESS);
    } else {
        close(pipe_fd[0]); // Close unused read end
        // redirect stdout and stderr
        dup2(pipe_fd[1],STDOUT_FILENO);  
        dup2(pipe_fd[1],STDERR_FILENO);  
        close(pipe_fd[1]);  
    }
}
于 2009-11-19T09:22:56.390 回答
6

popen()发球台”的答案是正确的。这是一个完全执行此操作的示例程序:

#include "stdio.h"
#include "unistd.h"

int main (int argc, const char * argv[])
{
    printf("pre-tee\n");

    if(dup2(fileno(popen("tee out.txt", "w")), STDOUT_FILENO) < 0) {
        fprintf(stderr, "couldn't redirect output\n");
        return 1;
    }

    printf("post-tee\n");

    return 0;
}

解释:

popen()返回 a FILE*,但dup2()需要一个文件描述符 (fd),因此将 afileno()转换FILE*为 fd。然后dup2(..., STDOUT_FILENO)说用来自的 fd 替换 stdout popen()

意思是,您生成一个子进程 ( popen),将其所有输入复制到标准输出和一个文件,然后将标准输出移植到该进程。

于 2013-09-18T22:32:51.383 回答
2

您可以使用pipe(2)并将dup2(2)您的标准输出连接到您可以读取的文件描述符。然后你可以有一个单独的线程监视该文件描述符,将它获得的所有内容写入日志文件和原始标准输出(dup2在连接管道之前保存到另一个文件描述符)。但是你需要一个后台线程。

实际上,我认为 vatine 建议的 popen tee 方法可能更简单、更安全(只要您不需要对日志文件做任何额外的事情,例如时间戳或编码等)。

于 2009-11-19T10:40:02.700 回答
1

您可以使用forkpty()withexec()来执行带有参数的监控程序。forkpty()返回一个文件描述符,该描述符被重定向到程序 stdin 和 stdout。写入文件描述符的任何内容都是程序的输入。程序写入的任何内容都可以从文件描述符中读取。

第二部分是循环读取程序的输出并将其写入文件并将其打印到标准输出。

例子:

pid = forkpty(&fd, NULL, NULL, NULL);
if (pid<0)
    return -1;

if (!pid) /* Child */
{
execl("/bin/ping", "/bin/ping", "-c", "1", "-W", "1", "192.168.3.19", NULL);
}

/* Parent */
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
于 2009-11-19T09:28:42.227 回答
-2

在 C 中没有简单的方法可以做到这一点。我怀疑最简单的方法是调用 popen(3),将 tee 作为命令,将所需的日志文件作为参数,然后 dup2(2) 新打开的文件描述符FILE* 到 fd 1。

但这看起来有点难看,我必须说我没有尝试过。

于 2009-11-19T09:26:10.433 回答