[devel] [PATCH hasher-priv v1 1/3] logging.c
Arseny Maslennikov
ar на cs.msu.ru
Чт Сен 17 16:12:21 MSK 2020
On Fri, Dec 13, 2019 at 12:42:03PM +0100, Alex Gladkov wrote:
> diff --git a/hasher-priv/logging.c b/hasher-priv/logging.c
> new file mode 100644
> index 0000000..9adac47
> --- /dev/null
> +++ b/hasher-priv/logging.c
> @@ -0,0 +1,64 @@
> +
> +/*
> + Copyright (C) 2019 Alexey Gladkov <legion на altlinux.org>
> +
> + A logging functions for the hasher-privd program.
> +
> + SPDX-License-Identifier: GPL-2.0-or-later
> +*/
> +
> +#include <stdio.h>
> +#include <stdarg.h>
> +#include <strings.h>
> +#include <errno.h>
> +#include <syslog.h>
> +
> +#include "logging.h"
> +
> +int log_priority = -1;
> +
> +int logging_level(const char *name)
> +{
> + if (!strcasecmp(name, "debug"))
> + return LOG_DEBUG;
> +
> + if (!strcasecmp(name, "info"))
> + return LOG_INFO;
> +
> + if (!strcasecmp(name, "warning"))
> + return LOG_WARNING;
> +
> + if (!strcasecmp(name, "error"))
> + return LOG_ERR;
> +
> + return 0;
> +}
> +
> +void logging_init(int loglevel, int stderr)
> +{
> + int options = LOG_PID;
> + if (stderr)
> + options |= LOG_PERROR;
> + log_priority = loglevel;
> + openlog(program_invocation_short_name, options, LOG_DAEMON);
> +}
> +
> +void logging_close(void)
> +{
> + closelog();
> +}
> +
> +void
> +message(int priority, const char *fmt, ...)
> +{
> + va_list ap;
> +
> + va_start(ap, fmt);
> + if (priority <= log_priority)
> + vsyslog(priority, fmt, ap);
If the daemon is invoked with `-f', why not log to stderr only,
prepending the message priority? That behaviour would fit better with
service managers that take care of process daemonization / pid tracking
themselves and direct the service's stderr to system log.
If log message duplication is desired, we can add a command line
argument to enforce that.
To produce messages of different priorities, we can prepend "<%d>" to
each log message.
> + else if (log_priority < 0) {
> + vfprintf(stderr, fmt, ap);
> + fprintf(stderr, "\n");
> + }
> + va_end(ap);
> +}
> diff --git a/hasher-priv/logging.h b/hasher-priv/logging.h
> new file mode 100644
> index 0000000..9d28fc8
> --- /dev/null
> +++ b/hasher-priv/logging.h
> @@ -0,0 +1,55 @@
> +
> +/*
> + Copyright (C) 2019 Alexey Gladkov <legion на altlinux.org>
> +
> + A logging functions for the hasher-privd program.
> +
> + SPDX-License-Identifier: GPL-2.0-or-later
> +*/
> +
> +#ifndef _LOGGING_H_
> +#define _LOGGING_H_
> +
> +#include <syslog.h>
> +#include <stdlib.h>
> +
> +void logging_init(int, int);
> +void logging_close(void);
> +int logging_level(const char *lvl) __attribute__((nonnull(1)));
> +
> +void message(int priority, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
> +
> +#define fatal(format, arg...) \
> + do { \
> + message(LOG_CRIT, \
> + "%s(%d): %s: " format, \
> + __FILE__, __LINE__, __FUNCTION__, \
> + ##arg); \
> + exit(EXIT_FAILURE); \
> + } while (0)
> +
> +#define err(format, arg...) \
> + do { \
> + message(LOG_ERR, \
> + "%s(%d): %s: " format, \
> + __FILE__, __LINE__, __FUNCTION__, \
> + ##arg); \
> + } while (0)
> +
> +#define info(format, arg...) \
> + do { \
> + message(LOG_INFO, \
> + "%s(%d): %s: " format, \
> + __FILE__, __LINE__, __FUNCTION__, \
> + ##arg); \
> + } while (0)
> +
> +#define dbg(format, arg...) \
> + do { \
> + message(LOG_DEBUG, \
> + "%s(%d): %s: " format, \
> + __FILE__, __LINE__, __FUNCTION__, \
> + ##arg); \
> + } while (0)
> +
> +#endif /* _LOGGING_H_ */
----------- следующая часть -----------
Было удалено вложение не в текстовом формате...
Имя : signature.asc
Тип : application/pgp-signature
Размер : 833 байтов
Описание: отсутствует
Url : <http://lists.altlinux.org/pipermail/devel/attachments/20200917/0bc99cbf/attachment.bin>
Подробная информация о списке рассылки Devel