1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/* This file is part of Mailfromd.
Copyright (C) 2005-2024 Sergey Poznyakoff
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include "libmf.h"
#include <mailutils/mailutils.h>
#include <gacopyz.h>
#ifdef USE_SYSLOG_ASYNC
# include "syslog_async.h"
#endif
static void
syslog_default_close()
{
closelog();
}
static void
syslog_default_open()
{
mu_stdstream_strerr_setup(MU_STRERR_SYSLOG);
}
static void
syslog_default_logtext(int prio, const char *text)
{
syslog(prio, "%s", text);
}
#ifdef USE_SYSLOG_ASYNC
static void
syslog_async_close()
{
closelog_async();
}
static void
syslog_async_open()
{
int rc;
mu_stdstream_strerr_setup(MU_STRERR_SYSLOG);
rc = mu_stream_ioctl(mu_strerr, MU_IOCTL_SYSLOGSTREAM,
MU_IOCTL_SYSLOGSTREAM_SET_LOGGER, syslog_async);
if (rc)
mu_error(_("cannot configure asynchronous syslog: %s; "
"falling back to system one"), mu_strerror (rc));
closelog(); /* we won't use it */
/* LOG_NDELAY ensures that the log descriptor is ready.
It is needed for logger_fdset to work (see mf_srvcfg_log_setup). */
openlog_async(mu_log_tag, LOG_NDELAY|LOG_PID, mu_log_facility);
mu_onexit(syslog_async_close, NULL);
}
static void
syslog_async_fdset(fd_set *fds)
{
FD_SET(log_fd_async(), fds);
}
static void
syslog_async_logtext(int prio, const char *text)
{
syslog_async(prio, "%s", text);
}
#endif
static void
stderr_open()
{
mu_stdstream_strerr_setup(MU_STRERR_STDERR);
}
static void
stderr_fdset(fd_set *fds)
{
FD_SET(fileno(stderr), fds);
}
static void
mf_gacopyz_log_printer(int level, char *fmt, va_list ap)
{
switch (level) {
case SMI_LOG_PROTO:
case SMI_LOG_DEBUG:
level = MU_LOG_DEBUG;
break;
case SMI_LOG_INFO:
level = MU_LOG_INFO;
break;
case SMI_LOG_WARN:
level = MU_LOG_WARNING;
break;
case SMI_LOG_ERR:
level = MU_LOG_ERROR;
break;
case SMI_LOG_FATAL:
default:
level = MU_LOG_EMERG;
}
mu_diag_voutput(level, fmt, ap);
}
static struct logger loggertab[] = {
{ "stderr",
LOGF_STDERR,
stderr_open,
NULL,
stderr_fdset,
NULL },
{ "syslog",
0,
syslog_default_open,
syslog_default_close,
NULL,
syslog_default_logtext },
#ifdef USE_SYSLOG_ASYNC
{ "syslog:async",
0,
syslog_async_open,
syslog_async_close,
syslog_async_fdset,
syslog_async_logtext },
#endif
{ NULL }
};
static int current_logger;
int
logger_flags(int mask)
{
return loggertab[current_logger].flags & mask;
}
int
logger_select(const char *name)
{
int i;
for (i = 0; loggertab[i].name; i++) {
if (strcmp(loggertab[i].name, name) == 0) {
current_logger = i;
return 0;
}
}
return -1;
}
void
logger_open()
{
int mode;
struct logger *lp = loggertab + current_logger;
if (lp->log_open)
lp->log_open();
mode = MU_DEBUG_LEVEL_MASK(MU_LOG_ERROR);
mu_stream_ioctl(mu_strerr, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_SEVERITY_MASK, &mode);
gacopyz_set_logger(mf_gacopyz_log_printer);
}
void
logger_close()
{
struct logger *lp = loggertab + current_logger;
if (lp->log_close)
lp->log_close();
}
void
logger_fdset(fd_set *set)
{
struct logger *lp = loggertab + current_logger;
if (lp->log_fdset)
lp->log_fdset(set);
}
void
logger_text(int prio, const char *text)
{
struct logger *lp = loggertab + current_logger;
(lp->log_text ? lp->log_text : syslog_default_logtext)(prio, text);
}
void
logger_set_print_severity(int enable)
{
mu_log_print_severity = enable;
mu_stream_ioctl(mu_strerr, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_MODE, &enable);
}
|