简体   繁体   English

Mac OS X:应用程序可以在哪里创建日志文件?

[英]Mac OS X: where app can create a log file?

My app creates its log file like this我的应用程序像这样创建它的日志文件

FILE *ftemp = NULL;
ftemp=fopen("/var/log/x.log", "ab+");
if(ftemp) 
{
    fprintf(stderr, "ftemp: log created\n");
}
else
{
    fprintf(stderr, "ftemp: log error:%s\n", strerror(errno));
}

The output is:输出是:

ftemp: log error:Permission denied

I will deploy it to other machines.我会把它部署到其他机器上。 Is there any location where my app has permissions to create its log file on any other machines?是否有任何位置我的应用程序有权在任何其他计算机上创建其日志文件?

If your program runs as root (which it apparently does not, judging from your “Permission denied” error), either /var/log or /Library/Logs is appropriate.如果您的程序以 root 身份运行(显然不是,从您的“权限被拒绝”错误判断),则/var/log/Library/Logs是合适的。

If your program runs as an ordinary user, ~/Library/Logs is appropriate.如果您的程序以普通用户身份运行,则~/Library/Logs是合适的。

It would be appropriate to create a subdirectory (in whatever log directory you end up using) named after your program, and write your logs in the subdirectory.最好创建一个以您的程序命名的子目录(在您最终使用的任何日志目录中),并将您的日志写入该子目录中。 For example, Apache on macOS writes its logs to /var/log/apache2 by default;例如,macOS 上的 Apache 默认将其日志写入/var/log/apache2 the Notes app writes its logs to ~/Library/Logs/com.apple.Notes . Notes 应用程序将其日志写入~/Library/Logs/com.apple.Notes

If your program runs as a system account (not root but not an ordinary user account), then perhaps your installer can create the log subdirectory under /var/log or /Library/Logs as root, then chown it to that system account to make it writable by your program.如果您的程序以系统帐户(不是 root 而是普通用户帐户)运行,那么也许您的安装程序可以以 root 身份在/var/log/Library/Logs下创建 log 子目录,然后将其chown到该系统帐户以进行它可由您的程序写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM