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