Syslog Continued

This week I will present the rest of the functions declared

in "sys/syslog.h", namely openlog(), closelog(), and setlogmask():

--OPENLOG

Although syslog() is sufficient for basic message logging, for more

advanced uses you may need to call openlog() before syslog(). openlog()

sets process attributes that affect subsequent calls to syslog(). Here

is the prototype of this function:

void openlog(const char *str, int options, int facility);

The first argument is a string that is prepended to every message. The

second argument describes various logging options that are indicated by

combining zero or more of the following constants using the bitwise OR

operator:

LOG_PID

Log the process ID with each message. This is useful for distinguishing

between daemon processes that have forked.

LOG_CONS

Direct messages to the system console if they cannot be sent to

syslogd.

LOG_NDELAY

Open the connection to syslogd immediately, without waiting for the

first message to be logged.

LOG_ODELAY

Delay open until the first call to syslog().

LOG_NOWAIT

Do not wait for child processes that have been forked to log messages

onto the console.

The third argument of openlog() specifies a default facility to be

assigned to all messages that do not include an explicit facility code.

The following openlog() call ...

openlog("mailer", LOG_PID, LOG_MAIL);

... prepends the string "mailer" to every log message, ensures that

the message contains the process's ID, and -- if no explicit facility

code is provided in the syslog() call -- it ensures that the message

bears the LOG_MAIL facility code.

--SETLOGMASK

The function setlogmask() has the following prototype:

int setlogmask(int maskpri);

It sets the log priority mask for the current process to maskpri and

returns the previous mask. If the maskpri argument is 0, the current

log mask isn't changed. Any calls from the current process to syslog()

with a priority code not set in maskpri are rejected. Use the macro

LOG_MASK(p) to calculate the mask for an individual priority p. To

obtain the mask for all priorities up to and including p_x, use the

macro LOG_UPT(p_x). The default log mask allows all priorities to be

logged. The following setlogmask() call ...

setlogmask(LOG_UPTO(LOG_ERR));

... ensures that only messages whose priority is LOG_ERR and above,

e.g., LOG_CRIT and LOG_EMERG, are logged; messages with lower

priorities such as LOG_DEBUG, are rejected.

--CLOSELOG

Finally, the function closelog() closes any open file descriptors

allocated by previous calls to openlog() or syslog().

What’s wrong? The new clean desk test
Join the discussion
Be the first to comment on this article. Our Commenting Policies