Вопрос

I have been reading about how to keep error/warning logs of a C program on Linux environment. Is it better to write the errno to a file as it is done here or is it better to use syslog or setlogmask system calls or another method? I would appreciate an answer with an example code.

Это было полезно?

Решение

I would strongly suggest using syslog for logging errors as its more standard way of logging.

you can look at the example implemenation here (bio3d.colorado.edu/tor/sadocs/misc/syslog.html)

Also unix network programming book by richard stevens also contains good explanation of using syslog

Другие советы

The above link no longer works - try this one here: https://www.gnu.org/software/libc/manual/html_node/Syslog-Example.html#Syslog-Example

....which contains the following example:

#include <syslog.h>

setlogmask (LOG_UPTO (LOG_NOTICE));

openlog ("exampleprog", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog (LOG_NOTICE, "Program started by User %d", getuid ());
syslog (LOG_INFO, "A tree falls in a forest");
closelog ();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top