“T恤”就像 UNIX 中的编程 api
拥有类似的东西
$> ps -ax | tee -a processes.txt
我想在 UNIX C 编程环境中
,这意味着不通过 shell 脚本。基本上有一个 API,以便我可以在 STDIN 和/或 STDOUT 上进行操作,以便我可以自动将 CLI 上出现的任何内容记录到文件中。想象一下,有一个前台进程与用户交互并响应一些输出到终端。我希望将终端中显示的所有内容也保存在一个文件中以供以后检查。
我会想象这样神奇的事情:
tee(STDIN, "append", logFile);
谢谢!
后续,这是我根据Lars编写的程序(见下面的回答部分),但不完全是我想要的:
int main(int argc, char** argv){
int pfd[2];
if (pipe(pfd) == -1) { perror("pipe"); }
if (fork()==0) { // child reads from pipe
close(pfd[1]);
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
int ufd = open("user_study.log", O_CREAT | O_APPEND | O_WRONLY, mode);
if (ufd == -1) {
perror("Cannot open output file"); exit(1);
} // ++
char buf;
while (read(pfd[0], &buf, 1) > 0) {
write(STDOUT_FILENO, &buf, 1); // write to stdout
write(ufd, &buf,1); // and to log
}
close(pfd[0]);
} else { // parent write to pipe
// dup STDOUT to the pipe
close(pfd[0]);
dup2(pfd[1], STDOUT_FILENO);
char msg[200];
msg[0] = "";
do {
scanf("%s", msg);
printf("program response..");
} while (strcmp(msg, "exit")!=0);
close(pfd[1]);
}
return 1;
}
实际运行:
[feih@machine ~/mytest]$ ./a.out
abc
haha
exit
program response.. <---- the output is delayed by the chld process, not desired
program response..
program response..
[feih@machine ~/mytest]$ less user_study.log
program response.. <---- the log doesn't contain input
program response..
program response..
期望的运行和日志(模拟):
[feih@machine ~/mytest]$ ./a.out
abc
program response..
haha
program response..
exit
program response..
[feih@machine ~/mytest]$ less user_study.log
abc
program response.. <--- the log should be the same as the running
haha
program response..
exit
program response..
所以到目前为止,这个问题还没有完全解决解决了。
I want to have something like this
gt; ps -ax | tee -a processes.txt
In a UNIX C programming environment, meaning not via shell scripts.
Basically is there a API so that I can tee on STDIN and/or STDOUT so that I can recording automatically anything appears on the CLI down to a file. Imagine there's a foreground process interact with user and response with some output to the terminal. I want to have everything shown in the terminal also in a file for later examination.
I would imagine something as magic as this:
tee(STDIN, "append", logFile);
Thanks!
Follow-up, this is the program I wrote according to Lars (see Answer section below), but not exactly what I desired:
int main(int argc, char** argv){
int pfd[2];
if (pipe(pfd) == -1) { perror("pipe"); }
if (fork()==0) { // child reads from pipe
close(pfd[1]);
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
int ufd = open("user_study.log", O_CREAT | O_APPEND | O_WRONLY, mode);
if (ufd == -1) {
perror("Cannot open output file"); exit(1);
} // ++
char buf;
while (read(pfd[0], &buf, 1) > 0) {
write(STDOUT_FILENO, &buf, 1); // write to stdout
write(ufd, &buf,1); // and to log
}
close(pfd[0]);
} else { // parent write to pipe
// dup STDOUT to the pipe
close(pfd[0]);
dup2(pfd[1], STDOUT_FILENO);
char msg[200];
msg[0] = "";
do {
scanf("%s", msg);
printf("program response..");
} while (strcmp(msg, "exit")!=0);
close(pfd[1]);
}
return 1;
}
Actual running:
[feih@machine ~/mytest]$ ./a.out
abc
haha
exit
program response.. <---- the output is delayed by the chld process, not desired
program response..
program response..
[feih@machine ~/mytest]$ less user_study.log
program response.. <---- the log doesn't contain input
program response..
program response..
Desired running and log(simulated):
[feih@machine ~/mytest]$ ./a.out
abc
program response..
haha
program response..
exit
program response..
[feih@machine ~/mytest]$ less user_study.log
abc
program response.. <--- the log should be the same as the running
haha
program response..
exit
program response..
So up till now, this problem hasn't been fully resolved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
pipe(2)
)您需要处理很多棘手的问题,但它是可行的。
如果没有额外的进程,您就无法做到这一点,因为 printf 只写入一个文件描述符(标准输出)。
You can do this:
pipe(2)
)dup2(2)
)There's a bunch of trickiness you need to take care of, but it's doable.
You can't do it this without an extra process, since printf only writes to one file descriptor (the stdout one).