1

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?

ZedZip
  • 5,794
  • 15
  • 66
  • 119

1 Answers1

6

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanx. Btw, when the app received the "permission error" I try to fprintf(stderr, "Error: permissions...") but where I can find the stderr output? My app is app with GUI. – ZedZip Mar 07 '18 at 10:54
  • If you launch a GUI app by double-clicking it, `stderr` is redirected to `/dev/null`, so you cannot find the output anywhere. You should use `NSLog` or `os_log` instead of printing to `stderr`. Then you can find your output using the `log` command or in Console.app (*if* you launch Console.app before your app logs the message). – rob mayoff Feb 01 '21 at 03:21