Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...