Cleanup zed logging
This is a set of minor cleanup changes related to zed logging: - Remove the program identity prefix from messages written to stderr since systemd already prepends this output with the program name. - Replace the copy of the program identity string with a ptr reference. - Replace "pid" with "PID" for consistency in comments & strings. - Rename the zed_log.c struct _ctx component "level" to "priority". - Add the LOG_PID option for messages written to syslog. Signed-off-by: Chris Dunlap <cdunlap@llnl.gov> Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov> Issue #2252
This commit is contained in:
parent
5a8855b716
commit
8125fb7190
|
@ -103,7 +103,7 @@ zed_conf_destroy(struct zed_conf *zcp)
|
||||||
if (zcp->pid_file) {
|
if (zcp->pid_file) {
|
||||||
if ((unlink(zcp->pid_file) < 0) && (errno != ENOENT))
|
if ((unlink(zcp->pid_file) < 0) && (errno != ENOENT))
|
||||||
zed_log_msg(LOG_WARNING,
|
zed_log_msg(LOG_WARNING,
|
||||||
"Failed to remove pid file \"%s\": %s",
|
"Failed to remove PID file \"%s\": %s",
|
||||||
zcp->pid_file, strerror(errno));
|
zcp->pid_file, strerror(errno));
|
||||||
}
|
}
|
||||||
if (zcp->conf_file)
|
if (zcp->conf_file)
|
||||||
|
@ -450,14 +450,14 @@ zed_conf_write_pid(struct zed_conf *zcp)
|
||||||
|
|
||||||
if (!zcp || !zcp->pid_file) {
|
if (!zcp || !zcp->pid_file) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
zed_log_msg(LOG_ERR, "Failed to write pid file: %s",
|
zed_log_msg(LOG_ERR, "Failed to write PID file: %s",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
n = strlcpy(dirbuf, zcp->pid_file, sizeof (dirbuf));
|
n = strlcpy(dirbuf, zcp->pid_file, sizeof (dirbuf));
|
||||||
if (n >= sizeof (dirbuf)) {
|
if (n >= sizeof (dirbuf)) {
|
||||||
errno = ENAMETOOLONG;
|
errno = ENAMETOOLONG;
|
||||||
zed_log_msg(LOG_WARNING, "Failed to write pid file: %s",
|
zed_log_msg(LOG_WARNING, "Failed to write PID file: %s",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
@ -479,13 +479,13 @@ zed_conf_write_pid(struct zed_conf *zcp)
|
||||||
umask(mask);
|
umask(mask);
|
||||||
|
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
zed_log_msg(LOG_WARNING, "Failed to open pid file \"%s\": %s",
|
zed_log_msg(LOG_WARNING, "Failed to open PID file \"%s\": %s",
|
||||||
zcp->pid_file, strerror(errno));
|
zcp->pid_file, strerror(errno));
|
||||||
} else if (fprintf(fp, "%d\n", (int) getpid()) == EOF) {
|
} else if (fprintf(fp, "%d\n", (int) getpid()) == EOF) {
|
||||||
zed_log_msg(LOG_WARNING, "Failed to write pid file \"%s\": %s",
|
zed_log_msg(LOG_WARNING, "Failed to write PID file \"%s\": %s",
|
||||||
zcp->pid_file, strerror(errno));
|
zcp->pid_file, strerror(errno));
|
||||||
} else if (fclose(fp) == EOF) {
|
} else if (fclose(fp) == EOF) {
|
||||||
zed_log_msg(LOG_WARNING, "Failed to close pid file \"%s\": %s",
|
zed_log_msg(LOG_WARNING, "Failed to close PID file \"%s\": %s",
|
||||||
zcp->pid_file, strerror(errno));
|
zcp->pid_file, strerror(errno));
|
||||||
} else {
|
} else {
|
||||||
return (0);
|
return (0);
|
||||||
|
@ -564,7 +564,7 @@ zed_conf_open_state(struct zed_conf *zcp)
|
||||||
zcp->state_file);
|
zcp->state_file);
|
||||||
} else if (pid > 0) {
|
} else if (pid > 0) {
|
||||||
zed_log_msg(LOG_WARNING,
|
zed_log_msg(LOG_WARNING,
|
||||||
"Found pid %d bound to state file \"%s\"",
|
"Found PID %d bound to state file \"%s\"",
|
||||||
pid, zcp->state_file);
|
pid, zcp->state_file);
|
||||||
} else {
|
} else {
|
||||||
zed_log_msg(LOG_WARNING,
|
zed_log_msg(LOG_WARNING,
|
||||||
|
|
|
@ -145,7 +145,7 @@ zed_file_unlock(int fd)
|
||||||
/*
|
/*
|
||||||
* Test whether an exclusive advisory lock could be obtained for the open
|
* Test whether an exclusive advisory lock could be obtained for the open
|
||||||
* file descriptor [fd].
|
* file descriptor [fd].
|
||||||
* Return 0 if the file is not locked, >0 for the pid of another process
|
* Return 0 if the file is not locked, >0 for the PID of another process
|
||||||
* holding a conflicting lock, or -1 on error (with errno set).
|
* holding a conflicting lock, or -1 on error (with errno set).
|
||||||
*/
|
*/
|
||||||
pid_t
|
pid_t
|
||||||
|
|
|
@ -33,38 +33,40 @@
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include "zed_log.h"
|
#include "zed_log.h"
|
||||||
|
|
||||||
#define ZED_LOG_MAX_ID_LEN 64
|
|
||||||
#define ZED_LOG_MAX_LOG_LEN 1024
|
#define ZED_LOG_MAX_LOG_LEN 1024
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
unsigned do_stderr:1;
|
unsigned do_stderr:1;
|
||||||
unsigned do_syslog:1;
|
unsigned do_syslog:1;
|
||||||
int level;
|
const char *identity;
|
||||||
char id[ZED_LOG_MAX_ID_LEN];
|
int priority;
|
||||||
int pipe_fd[2];
|
int pipe_fd[2];
|
||||||
} _ctx;
|
} _ctx;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize the logging subsystem.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
zed_log_init(const char *identity)
|
zed_log_init(const char *identity)
|
||||||
{
|
{
|
||||||
const char *p;
|
|
||||||
|
|
||||||
if (identity) {
|
if (identity) {
|
||||||
p = (p = strrchr(identity, '/')) ? p + 1 : identity;
|
const char *p = strrchr(identity, '/');
|
||||||
strlcpy(_ctx.id, p, sizeof (_ctx.id));
|
_ctx.identity = (p != NULL) ? p + 1 : identity;
|
||||||
} else {
|
} else {
|
||||||
_ctx.id[0] = '\0';
|
_ctx.identity = NULL;
|
||||||
}
|
}
|
||||||
_ctx.pipe_fd[0] = -1;
|
_ctx.pipe_fd[0] = -1;
|
||||||
_ctx.pipe_fd[1] = -1;
|
_ctx.pipe_fd[1] = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Shutdown the logging subsystem.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
zed_log_fini()
|
zed_log_fini(void)
|
||||||
{
|
{
|
||||||
if (_ctx.do_syslog) {
|
zed_log_stderr_close();
|
||||||
closelog();
|
zed_log_syslog_close();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -158,78 +160,72 @@ zed_log_pipe_wait(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Start logging messages at the syslog [priority] level or higher to stderr.
|
||||||
|
* Refer to syslog(3) for valid priority values.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
zed_log_stderr_open(int level)
|
zed_log_stderr_open(int priority)
|
||||||
{
|
{
|
||||||
_ctx.do_stderr = 1;
|
_ctx.do_stderr = 1;
|
||||||
_ctx.level = level;
|
_ctx.priority = priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Stop logging messages to stderr.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
zed_log_stderr_close(void)
|
zed_log_stderr_close(void)
|
||||||
{
|
{
|
||||||
_ctx.do_stderr = 0;
|
if (_ctx.do_stderr)
|
||||||
|
_ctx.do_stderr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Start logging messages to syslog.
|
||||||
|
* Refer to syslog(3) for valid option/facility values.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
zed_log_syslog_open(int facility)
|
zed_log_syslog_open(int facility)
|
||||||
{
|
{
|
||||||
const char *identity;
|
|
||||||
|
|
||||||
_ctx.do_syslog = 1;
|
_ctx.do_syslog = 1;
|
||||||
identity = (_ctx.id[0] == '\0') ? NULL : _ctx.id;
|
openlog(_ctx.identity, LOG_NDELAY | LOG_PID, facility);
|
||||||
openlog(identity, LOG_NDELAY, facility);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Stop logging messages to syslog.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
zed_log_syslog_close(void)
|
zed_log_syslog_close(void)
|
||||||
{
|
{
|
||||||
_ctx.do_syslog = 0;
|
if (_ctx.do_syslog) {
|
||||||
closelog();
|
_ctx.do_syslog = 0;
|
||||||
|
closelog();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Auxiliary function to log a message to syslog and/or stderr.
|
||||||
|
*/
|
||||||
static void
|
static void
|
||||||
_zed_log_aux(int priority, const char *fmt, va_list vargs)
|
_zed_log_aux(int priority, const char *fmt, va_list vargs)
|
||||||
{
|
{
|
||||||
char buf[ZED_LOG_MAX_LOG_LEN];
|
char buf[ZED_LOG_MAX_LOG_LEN];
|
||||||
char *syslogp;
|
|
||||||
char *p;
|
|
||||||
int len;
|
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
assert(fmt != NULL);
|
if (!fmt)
|
||||||
|
return;
|
||||||
|
|
||||||
syslogp = NULL;
|
n = vsnprintf(buf, sizeof (buf), fmt, vargs);
|
||||||
p = buf;
|
if ((n < 0) || (n >= sizeof (buf))) {
|
||||||
len = sizeof (buf);
|
buf[sizeof (buf) - 2] = '+';
|
||||||
|
buf[sizeof (buf) - 1] = '\0';
|
||||||
if (_ctx.id[0] != '\0') {
|
|
||||||
n = snprintf(p, len, "%s: ", _ctx.id);
|
|
||||||
if ((n < 0) || (n >= len)) {
|
|
||||||
p += len - 1;
|
|
||||||
len = 0;
|
|
||||||
} else {
|
|
||||||
p += n;
|
|
||||||
len -= n;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if ((len > 0) && fmt) {
|
|
||||||
syslogp = p;
|
|
||||||
n = vsnprintf(p, len, fmt, vargs);
|
|
||||||
if ((n < 0) || (n >= len)) {
|
|
||||||
p += len - 1;
|
|
||||||
len = 0;
|
|
||||||
} else {
|
|
||||||
p += n;
|
|
||||||
len -= n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*p = '\0';
|
|
||||||
|
|
||||||
if (_ctx.do_syslog && syslogp)
|
if (_ctx.do_syslog)
|
||||||
syslog(priority, "%s", syslogp);
|
syslog(priority, "%s", buf);
|
||||||
|
|
||||||
if (_ctx.do_stderr && priority <= _ctx.level)
|
if (_ctx.do_stderr && (priority <= _ctx.priority))
|
||||||
fprintf(stderr, "%s\n", buf);
|
fprintf(stderr, "%s\n", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ void zed_log_pipe_close_writes(void);
|
||||||
|
|
||||||
void zed_log_pipe_wait(void);
|
void zed_log_pipe_wait(void);
|
||||||
|
|
||||||
void zed_log_stderr_open(int level);
|
void zed_log_stderr_open(int priority);
|
||||||
|
|
||||||
void zed_log_stderr_close(void);
|
void zed_log_stderr_close(void);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue