masqmail

view src/log.c @ 367:b27f66555ba8

Reformated multiline comments to have leading asterisks on each line Now we use: /* ** comment */ This makes the indent style simpler, too.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:20:59 +0200
parents e507c854a63e
children 9bc3e47b0222
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 <sysexits.h>
22 #include "masqmail.h"
24 static char *_sysexit_strings[] = {
25 "command line usage error",
26 "data format error",
27 "cannot open input",
28 "addressee unknown",
29 "host name unknown",
30 "service unavailable",
31 "internal software error",
32 "system error (e.g., can't fork)",
33 "critical OS file missing",
34 "can't create (user) output file",
35 "input/output error",
36 "temp failure; user is invited to retry",
37 "remote error in protocol",
38 "permission denied",
39 "configuration error"
40 };
42 gchar*
43 ext_strerror(int err)
44 {
45 if (err < 1024)
46 return strerror(err);
47 else if (err > 1024 + EX__BASE
48 && (err - 1024 - EX__BASE < sizeof(_sysexit_strings) / sizeof(_sysexit_strings[0])))
49 return _sysexit_strings[err - 1024 - EX__BASE];
51 return "unknown error";
52 }
54 static FILE *logfile = NULL;
55 static FILE *debugfile = NULL;
57 gboolean
58 logopen()
59 {
60 gchar *filename;
61 mode_t saved_mode = umask(066);
63 if (conf.use_syslog) {
64 openlog(PACKAGE, LOG_PID, LOG_MAIL);
65 } else {
66 uid_t saved_uid;
67 gid_t saved_gid;
69 if (!conf.run_as_user) {
70 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid);
71 }
73 filename = g_strdup_printf("%s/masqmail.log", conf.log_dir);
74 logfile = fopen(filename, "a");
75 if (!logfile) {
76 fprintf(stderr, "could not open log '%s': %s\n", filename, strerror(errno));
77 return FALSE;
78 }
79 g_free(filename);
81 if (!conf.run_as_user) {
82 set_euidgid(saved_uid, saved_gid, NULL, NULL);
83 }
84 }
86 #ifdef ENABLE_DEBUG
87 if (conf.debug_level > 0) {
88 filename = g_strdup_printf("%s/debug.log", conf.log_dir);
89 debugfile = fopen(filename, "a");
90 if (!debugfile) {
91 fprintf(stderr, "could not open debug log '%s'\n", filename);
92 return FALSE;
93 }
94 g_free(filename);
95 }
96 #endif
97 umask(saved_mode);
98 return TRUE;
99 }
101 void
102 logclose()
103 {
104 if (conf.use_syslog)
105 closelog();
106 else if (logfile)
107 fclose(logfile);
108 if (debugfile)
109 fclose(debugfile);
110 }
112 void
113 vlogwrite(int pri, const char *fmt, va_list args)
114 {
115 if ((conf.do_verbose && (pri & LOG_VERBOSE)) || (pri == LOG_ALERT) || (pri == LOG_WARNING)) {
116 va_list args_copy;
117 va_copy(args_copy, args);
118 vfprintf(stdout, fmt, args_copy);
119 va_end(args_copy);
120 fflush(stdout); /* in case output ends not with newline */
121 }
123 pri &= ~LOG_VERBOSE;
124 if (!pri) {
125 return;
126 }
127 if (conf.use_syslog)
128 vsyslog(pri, fmt, args);
129 else if (pri <= conf.log_max_pri) {
130 FILE *file = logfile ? logfile : stderr;
131 time_t now = time(NULL);
132 struct tm *t = localtime(&now);
133 gchar buf[24];
135 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
136 fprintf(file, "%s [%d] ", buf, getpid());
138 vfprintf(file, fmt, args);
139 fflush(file);
140 }
141 }
143 #ifdef ENABLE_DEBUG
144 void
145 vdebugwrite(int pri, const char *fmt, va_list args)
146 {
147 time_t now = time(NULL);
148 struct tm *t = localtime(&now);
149 gchar buf[24];
150 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
152 if (debugfile) {
153 fprintf(debugfile, "%s [%d] ", buf, getpid());
154 vfprintf(debugfile, fmt, args);
155 fflush(debugfile);
156 } else {
157 fprintf(stderr, "no debug file, msg was:\n");
158 vfprintf(stderr, fmt, args);
159 }
160 }
161 #endif
163 void
164 logwrite(int pri, const char *fmt, ...)
165 {
166 va_list args, args_copy;
167 int saved_errno = errno; /* somewhere this is changed to EBADF */
169 va_start(args, fmt);
170 #ifdef ENABLE_DEBUG
171 va_copy(args_copy, args);
172 #endif
173 vlogwrite(pri, fmt, args);
174 #ifdef ENABLE_DEBUG
175 if (debugfile)
176 vdebugwrite(pri, fmt, args_copy);
177 va_end(args_copy);
178 #endif
179 va_end(args);
181 errno = saved_errno;
182 }
184 #ifdef ENABLE_DEBUG
185 void
186 debugf(const char *fmt, ...)
187 {
188 va_list args;
189 va_start(args, fmt);
191 vdebugwrite(LOG_DEBUG, fmt, args);
193 va_end(args);
194 }
196 void
197 vdebugf(const char *fmt, va_list args)
198 {
199 vdebugwrite(LOG_DEBUG, fmt, args);
200 }
201 #endif
203 void
204 maillog(const char *fmt, ...)
205 {
206 va_list args;
207 va_start(args, fmt);
209 vlogwrite(LOG_NOTICE, fmt, args);
211 va_end(args);
212 }