Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

c++ - 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
");
}
else
{
    fprintf(stderr, "ftemp: log error:%s
", 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

If your program runs as an ordinary user, ~/Library/Logs is appropriate.

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; the Notes app writes its logs to ~/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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...