In iOS development, I would like to test the output of the log to the local file. Use the redirection can be done to "NSLog" output to the file, but if I use the "printf" output when the use freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
can not do. Is there any way to make "printf" output is also redirected to the log ?
Asked
Active
Viewed 60 times
0

David
- 1
- 2
1 Answers
0
i had fine the answer.
you can use dup2
instand of freopen
,just do like this:
//将缓冲区禁止
setvbuf(stdout,NULL,_IONBF,0);
//用创建的文件描述符替换掉 标准输出和错误输出
//ignore logFilePath(just a file address).
int fd = open([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],(O_RDWR | O_CREAT), 0644);
dup2(fd,STDOUT_FILENO);
dup2(fd, STDERR_FILENO)
then between NSLog
and printf
will redirected to your local.

David
- 1
- 2