masqmail
view src/log.c @ 394:c8e3d1a79313
Refactoring in conf.c: read_rval().
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 18 Feb 2012 19:32:40 +0100 |
parents | b27f66555ba8 |
children | 5f0829f8e6c7 |
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2001 Oliver Kurth
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include <sys/stat.h>
21 #include <sysexits.h>
23 #include "masqmail.h"
25 static char *_sysexit_strings[] = {
26 "command line usage error",
27 "data format error",
28 "cannot open input",
29 "addressee unknown",
30 "host name unknown",
31 "service unavailable",
32 "internal software error",
33 "system error (e.g., can't fork)",
34 "critical OS file missing",
35 "can't create (user) output file",
36 "input/output error",
37 "temp failure; user is invited to retry",
38 "remote error in protocol",
39 "permission denied",
40 "configuration error"
41 };
43 gchar*
44 ext_strerror(int err)
45 {
46 if (err < 1024)
47 return strerror(err);
48 else if (err > 1024 + EX__BASE
49 && (err - 1024 - EX__BASE < sizeof(_sysexit_strings) / sizeof(_sysexit_strings[0])))
50 return _sysexit_strings[err - 1024 - EX__BASE];
52 return "unknown error";
53 }
55 static FILE *logfile = NULL;
56 static FILE *debugfile = NULL;
58 gboolean
59 logopen()
60 {
61 gchar *filename;
62 mode_t saved_mode = umask(066);
64 if (conf.use_syslog) {
65 openlog(PACKAGE, LOG_PID, LOG_MAIL);
66 } else {
67 uid_t saved_uid;
68 gid_t saved_gid;
70 if (!conf.run_as_user) {
71 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
72 }
74 filename = g_strdup_printf("%s/masqmail.log", conf.log_dir);
75 logfile = fopen(filename, "a");
76 if (!logfile) {
77 fprintf(stderr, "could not open log '%s': %s\n", filename, strerror(errno));
78 return FALSE;
79 }
80 g_free(filename);
82 if (!conf.run_as_user) {
83 set_euidgid(saved_uid, saved_gid, NULL, NULL);
84 }
85 }
87 #ifdef ENABLE_DEBUG
88 if (conf.debug_level > 0) {
89 filename = g_strdup_printf("%s/debug.log", conf.log_dir);
90 debugfile = fopen(filename, "a");
91 if (!debugfile) {
92 fprintf(stderr, "could not open debug log '%s'\n", filename);
93 return FALSE;
94 }
95 g_free(filename);
96 }
97 #endif
98 umask(saved_mode);
99 return TRUE;
100 }
102 void
103 logclose()
104 {
105 if (conf.use_syslog)
106 closelog();
107 else if (logfile)
108 fclose(logfile);
109 if (debugfile)
110 fclose(debugfile);
111 }
113 void
114 vlogwrite(int pri, const char *fmt, va_list args)
115 {
116 if ((conf.do_verbose && (pri & LOG_VERBOSE)) || (pri == LOG_ALERT) || (pri == LOG_WARNING)) {
117 va_list args_copy;
118 va_copy(args_copy, args);
119 vfprintf(stdout, fmt, args_copy);
120 va_end(args_copy);
121 fflush(stdout); /* in case output ends not with newline */
122 }
124 pri &= ~LOG_VERBOSE;
125 if (!pri) {
126 return;
127 }
128 if (conf.use_syslog)
129 vsyslog(pri, fmt, args);
130 else if (pri <= conf.log_max_pri) {
131 FILE *file = logfile ? logfile : stderr;
132 time_t now = time(NULL);
133 struct tm *t = localtime(&now);
134 gchar buf[24];
136 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
137 fprintf(file, "%s [%d] ", buf, getpid());
139 vfprintf(file, fmt, args);
140 fflush(file);
141 }
142 }
144 #ifdef ENABLE_DEBUG
145 void
146 vdebugwrite(int pri, const char *fmt, va_list args)
147 {
148 time_t now = time(NULL);
149 struct tm *t = localtime(&now);
150 gchar buf[24];
151 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
153 if (debugfile) {
154 fprintf(debugfile, "%s [%d] ", buf, getpid());
155 vfprintf(debugfile, fmt, args);
156 fflush(debugfile);
157 } else {
158 fprintf(stderr, "no debug file, msg was:\n");
159 vfprintf(stderr, fmt, args);
160 }
161 }
162 #endif
164 void
165 logwrite(int pri, const char *fmt, ...)
166 {
167 va_list args, args_copy;
168 int saved_errno = errno; /* somewhere this is changed to EBADF */
170 va_start(args, fmt);
171 #ifdef ENABLE_DEBUG
172 va_copy(args_copy, args);
173 #endif
174 vlogwrite(pri, fmt, args);
175 #ifdef ENABLE_DEBUG
176 if (debugfile)
177 vdebugwrite(pri, fmt, args_copy);
178 va_end(args_copy);
179 #endif
180 va_end(args);
182 errno = saved_errno;
183 }
185 #ifdef ENABLE_DEBUG
186 void
187 debugf(const char *fmt, ...)
188 {
189 va_list args;
190 va_start(args, fmt);
192 vdebugwrite(LOG_DEBUG, fmt, args);
194 va_end(args);
195 }
197 void
198 vdebugf(const char *fmt, va_list args)
199 {
200 vdebugwrite(LOG_DEBUG, fmt, args);
201 }
202 #endif
204 void
205 maillog(const char *fmt, ...)
206 {
207 va_list args;
208 va_start(args, fmt);
210 vlogwrite(LOG_NOTICE, fmt, args);
212 va_end(args);
213 }