This commit is contained in:
Pan Qiancheng 2025-03-26 15:58:49 +08:00
parent 9302254d57
commit 4f81d28174
2 changed files with 18 additions and 0 deletions

View File

@ -117,11 +117,29 @@ void duplicate_output_to_log() {
int log_fd = open(LOG_OUT_FILE, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (log_fd < 0) return;
int stdout_copy = dup(STDOUT_FILENO);
int stderr_copy = dup(STDERR_FILENO);
dup2(log_fd, STDOUT_FILENO);
dup2(log_fd, STDERR_FILENO);
close(log_fd);
// 让 stdout 和 stderr 同时输出到日志文件和控制台
FILE *log_file = fdopen(log_fd, "a");
if (log_file) {
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
stdout = log_file;
stderr = log_file;
}
dup2(stdout_copy, STDOUT_FILENO);
dup2(stderr_copy, STDERR_FILENO);
close(stdout_copy);
close(stderr_copy);
}
typedef int (*orig_execve_type)(const char *filename, char *const argv[],
char *const envp[]);

Binary file not shown.