I'm using perror() to print error messages, like:
pid = fork();
if (pid < 0) {
perror("couldn't fork");
exit(EXIT_FAILURE);
}
Is it possible to use errno/perror()
facilities but direct the produced messages to the system log (/var/log/syslog
)?
I ask this in the context of a program which can be run in both daemon and non-daemon modes. In daemon mode, perror()
messages won't appear on syslog.
Use strerror
to get the error message based on an error code, without printing it. Then pass it to syslog
like any other log message:
syslog(LOG_ERR, "Couldn't fork: %s", strerror(errno));
The easiest way is to redirect stderr (and possibly stdout) when you call your program. For example: ./myprog 2>&1 | logger
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With