|
» |
|
|
|
NAMEsyslog(), openlog(), closelog(), setlogmask() — control system log SYNOPSIS#include <syslog.h> void syslog(int priority, const char *message, ...); void openlog(const char *ident, int logopt, int facility); void closelog(void); int setlogmask(int maskpri); RemarksThe ANSI C ", ...
" construct denotes a variable length argument list whose
optional [or required]
members are given in the associated comment
(/* */). DESCRIPTION- syslog()
writes a message onto the system log maintained by
syslogd
(see
syslogd(1M)).
The message is tagged with
priority.
The
message
is similar to a
printf(3S)
format string except that
%m
is replaced by the error message associated with the current value of
errno.
A trailing newline is added if needed. This message is read by
syslogd
and written to the system console, log files,
selected users' terminals, or forwarded to
syslogd
on another host as appropriate. priority
is encoded as the logical
OR
of a
level
and a
facility.
The
level
signifies the urgency of the message, and
facility
signifies the subsystem generating the message.
facility
can be encoded explicitly in
priority,
or a default
facility
can be set with
openlog()
(see below). level
is selected from an ordered list:
- LOG_EMERG
A panic condition.
This is normally broadcast to all users. - LOG_ALERT
A condition that should be corrected immediately,
such as a corrupted system database. - LOG_CRIT
Critical conditions, such as hard device errors. - LOG_ERR
Errors. - LOG_WARNING
Warning messages. - LOG_NOTICE
Conditions that are not error conditions,
but should possibly be handled specially. - LOG_INFO
Informational messages. - LOG_DEBUG
Messages that contain information
normally of use only when debugging a program.
syslog()
does not log a message that does not have a
level
set. If
syslog()
cannot pass the message to
syslogd,
it attempts to write the message on
/dev/console
if the
LOG_CONS
option is set (see below). - openlog()
can be called to initialize the log file,
if special processing is needed.
ident
is a string that precedes every message.
logopt
is a mask of bits, logically
OR'ed
together, indicating logging options.
The values for
logopt
are:
- LOG_PID
Log the process
ID
with each message; useful for identifying instantiations of daemons. - LOG_CONS
Force writing messages to the console if unable to send it to
syslogd.
This option is safe to use in daemon processes
that have no controlling terminal because
syslog()
forks before opening the console. - LOG_NDELAY
Open the connection to
syslogd
immediately.
Normally, the open is delayed until the first message is logged.
This is useful for programs that need to manage
the order in which file descriptors are allocated. - LOG_NOWAIT
Do not wait for children forked to log messages on the console.
This option should be used by processes that enable notification
of child termination via
SIGCLD,
because
syslog()
might otherwise block, waiting for a child
whose exit status has already been collected.
facility
encodes a default facility to be assigned to all messages
written subsequently by
syslog()
with no explicit facility encoded.
- LOG_KERN
Messages generated by the kernel.
These cannot be generated by any user processes. - LOG_USER
Messages generated by random user processes.
This is the default facility identifier if none is specified. - LOG_MAIL
The mail system. - LOG_DAEMON
System daemons, such as
inetd(1M),
ftpd(1M),
etc. - LOG_AUTH
The authorization system:
login(1),
su(1),
getty(1M),
etc. - LOG_SYSLOG
Messages generated internally by
syslogd
daemon. - LOG_LPR
The line printer spooling system:
lp(1),
lpsched(1M),
etc. - LOG_NEWS
Messages generated by the
news
system. - LOG_UUCP
Messages generated by the UUCP system. - LOG_CRON
Messages generated by the CRON daemon. - LOG_LOCAL0
Reserved for local use.
Similarly for
LOG_LOCAL1
through
LOG_LOCAL7.
facility and level
uses an
encoded code for logging in
syslogd
message.
The encoded code for
facility and level
are as follows.
LOG_SYSLOG F LOG_NOTICE 5
- closelog()
closes the log file. - setlogmask()
sets the log priority mask to
maskpri
and returns the previous mask.
Calls to
syslog()
with a priority not set in
maskpri
are rejected.
The mask for an individual priority
pri
is calculated by the macro
LOG_MASK(pri)
;
the mask for all priorities up to and including
toppri
is given by the macro
LOG_UPTO
(toppri).
By default, all priorities are logged.
ERRORSsyslog
fails if any of the following conditions are encountered:
- EAGAIN
The named pipe
/dev/log
is blocked for writing. - ENOENT
The named pipe
/dev/log
bold) could not be opened successfully.
EXAMPLESwho
logs a message regarding some sort of unexpected and serious error:
syslog(LOG_ALERT, "who: internal error 23"); ftpd
uses
openlog()
to arrange to log its process
ID,
to log to the console if necessary,
and to log in the name of the
daemon
facility:
openlog("ftpd", LOG_PID|LOG_CONS, LOG_DAEMON); Arrange to log messages only at levels
LOG_ERR
and lower:
setlogmask(LOG_UPTO(LOG_ERR)); Typical usage of
syslog()
to log a connection:
syslog(LOG_INFO, "Connection from host %s", CallingHost); If the facility has not been set with
openlog(),
it defaults to
LOG_USER. Explicitly set the facility for this message:
syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m"); WARNINGSA call to
syslog()
has no effect unless the syslog daemon
is running
(see
syslogd(1M)).
openlog()
does not copy and store the
ident
string internally;
it stores only a character pointer.
Therefore it is the responsibility of the programmer
to make sure that the
ident
argument points to the correct string
until the log file is closed. AUTHORsyslog()
was developed by the University of California, Berkeley.
|