masqmail
view src/log.c @ 12:9fb7ddbaf129
removed noop-code; some beautifying
author | meillo@marmaro.de |
---|---|
date | Wed, 29 Oct 2008 16:34:13 +0100 |
parents | 08114f7dcc23 |
children | f671821d8222 |
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 "masqmail.h"
21 #include "sysexits.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)
113 || (pri == LOG_WARNING)) {
114 va_list args_copy;
115 va_copy(args_copy, args);
116 vfprintf(stdout, fmt, args_copy);
117 va_end(args_copy);
118 fflush(stdout); /* is this necessary? */
119 }
121 pri &= ~LOG_VERBOSE;
122 if (pri) {
124 if (conf.use_syslog)
125 vsyslog(pri, fmt, args);
126 else {
127 if (pri <= conf.log_max_pri) {
128 FILE *file = logfile ? logfile : stderr;
129 time_t now = time(NULL);
130 struct tm *t = localtime(&now);
131 gchar buf[24];
132 uid_t saved_uid;
133 gid_t saved_gid;
135 saved_gid = setegid(conf.mail_gid);
136 saved_uid = seteuid(conf.mail_uid);
138 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
139 fprintf(file, "%s [%d] ", buf, getpid());
141 vfprintf(file, fmt, args);
142 fflush(file);
144 seteuid(saved_uid);
145 setegid(saved_gid);
146 }
147 }
148 }
149 }
151 #ifdef ENABLE_DEBUG
152 void
153 vdebugwrite(int pri, const char *fmt, va_list args)
154 {
155 time_t now = time(NULL);
156 struct tm *t = localtime(&now);
157 gchar buf[24];
158 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
160 if (debugfile) {
161 fprintf(debugfile, "%s [%d] ", buf, getpid());
163 vfprintf(debugfile, fmt, args);
164 fflush(debugfile);
165 } else {
166 fprintf(stderr, "no debug file, msg was:\n");
167 vfprintf(stderr, fmt, args);
168 }
169 }
170 #endif
172 void
173 logwrite(int pri, const char *fmt, ...)
174 {
175 va_list args, args_copy;
176 int saved_errno = errno; /* somewhere this is changed to EBADF */
178 va_start(args, fmt);
179 #ifdef ENABLE_DEBUG
180 va_copy(args_copy, args);
181 #endif
182 vlogwrite(pri, fmt, args);
183 #ifdef ENABLE_DEBUG
184 if (debugfile)
185 vdebugwrite(pri, fmt, args_copy);
186 va_end(args_copy);
187 #endif
188 va_end(args);
190 errno = saved_errno;
191 }
193 #ifdef ENABLE_DEBUG
194 void
195 debugf(const char *fmt, ...)
196 {
197 va_list args;
198 va_start(args, fmt);
200 vdebugwrite(LOG_DEBUG, fmt, args);
202 va_end(args);
203 }
205 void
206 vdebugf(const char *fmt, va_list args)
207 {
208 vdebugwrite(LOG_DEBUG, fmt, args);
209 }
210 #endif
212 void
213 maillog(const char *fmt, ...)
214 {
215 va_list args;
216 va_start(args, fmt);
218 vlogwrite(LOG_NOTICE, fmt, args);
220 va_end(args);
221 }