是否可以在C中合并stderr和stdout ?

时间:2021-09-01 00:05:39

I need to merge stderr and stdout because I want my debug and the exceptions in the same log file. I tried

我需要合并stderr和stdout,因为我希望我的调试和异常在同一个日志文件中。我试着

    NSString *logPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Tag Monster/log.log"];
    freopen([logPath fileSystemRepresentation], "a", stderr);
    freopen([logPath fileSystemRepresentation], "a", stdout);

but this messes up the order. It prints the stderr messages at the top of the file. Or is there a better way to log in cocoa? NSLog just spams the syslog :P

但这会打乱订单。它在文件的顶部打印stderr消息。或者有没有更好的方法来登录可可?NSLog只是对syslog进行了spams:P。

Edit: Thants my log macro:

编辑:这是我的日志宏:

#ifdef DEBUG_MODE_LEVEL_KEEP
#define DLogK(...) (void)printf("%s: %s\n",  __PRETTY_FUNCTION__, [[NSString    stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define DLogK(...)
#endif

If I just redirect stderr to a logfile and log with

如果我只是重定向stderr到一个日志文件和日志

fprintf(stderr, "%s: %s\n",  __PRETTY_FUNCTION__, [[NSString    stringWithFormat:__VA_ARGS__] UTF8String])

it does not work either. Shouldn't that just work?

它也不起作用。不应该只是工作吗?

Thanks

谢谢

3 个解决方案

#1


8  

Using dup2() in unistd.h, you could close both stderr and stdout and re-direct them to a file. For instance:

在unistd使用dup2()。h,你可以关闭stderr和stdout并将它们重定向到一个文件。例如:

#include <sys/stat.h> 
#include <stdio.h> 
#include <fcntl.h> 

int log_file;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

log_file = open("my_log_file.txt", O_WRONLY | O_CREAT, mode);

if (dup2(log_file, fileno(stderr)) != fileno(stderr) ||
    dup2(log_file, fileno(stdout)) != fileno(stdout))
{
    perror("Unable to redirect output");
}

Now both stderr and stdout, when used, will write to my_log_file.txt. While I'm not sure about iOS, this should work perfectly fine on OSX.

现在,当使用stderr和stdout时,都将写入my_log_file.txt。虽然我对iOS不太确定,但它在OSX上应该可以运行得很好。

#2


7  

When you run the process redirect stderr to stdout like this:

当您运行该进程时,将stderr重定向到stdout,如下所示:

myprocess 2>&1

#3


0  

Did you have a look at Log4Cocoa?

你看过Log4Cocoa了吗?

#1


8  

Using dup2() in unistd.h, you could close both stderr and stdout and re-direct them to a file. For instance:

在unistd使用dup2()。h,你可以关闭stderr和stdout并将它们重定向到一个文件。例如:

#include <sys/stat.h> 
#include <stdio.h> 
#include <fcntl.h> 

int log_file;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;

log_file = open("my_log_file.txt", O_WRONLY | O_CREAT, mode);

if (dup2(log_file, fileno(stderr)) != fileno(stderr) ||
    dup2(log_file, fileno(stdout)) != fileno(stdout))
{
    perror("Unable to redirect output");
}

Now both stderr and stdout, when used, will write to my_log_file.txt. While I'm not sure about iOS, this should work perfectly fine on OSX.

现在,当使用stderr和stdout时,都将写入my_log_file.txt。虽然我对iOS不太确定,但它在OSX上应该可以运行得很好。

#2


7  

When you run the process redirect stderr to stdout like this:

当您运行该进程时,将stderr重定向到stdout,如下所示:

myprocess 2>&1

#3


0  

Did you have a look at Log4Cocoa?

你看过Log4Cocoa了吗?