masqmail-0.2
view src/log.c @ 3:8c55886cacd8
man pages will be maintained in troff now
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 21:40:10 +0200 |
parents | |
children | 26e34ae9a3e3 |
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 *ext_strerror(int err)
42 {
43 if(err < 1024)
44 return strerror(err);
45 else
46 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 logopen()
57 {
58 gchar *filename;
59 mode_t saved_mode = umask(066);
61 if(conf.use_syslog){
62 openlog(PACKAGE, LOG_PID, LOG_MAIL);
63 }else{
64 uid_t saved_uid;
65 gid_t saved_gid;
67 saved_gid = setegid(conf.mail_gid);
68 saved_uid = seteuid(conf.mail_uid);
70 filename = g_strdup_printf("%s/masqmail.log", conf.log_dir);
71 logfile = fopen(filename, "a");
72 if(!logfile){
73 fprintf(stderr, "could not open log '%s': %s\n", filename, strerror(errno));
74 return FALSE;
75 }
76 g_free(filename);
78 seteuid(saved_uid);
79 setegid(saved_gid);
80 }
82 #ifdef ENABLE_DEBUG
83 if(conf.debug_level > 0){
84 filename = g_strdup_printf("%s/debug.log", conf.log_dir);
85 debugfile = fopen(filename, "a");
86 if(!debugfile){
87 fprintf(stderr, "could not open debug log '%s'\n", filename);
88 return FALSE;
89 }
90 g_free(filename);
91 }
92 #endif
93 umask(saved_mode);
94 return TRUE;
95 }
97 void logclose()
98 {
99 if(conf.use_syslog)
100 closelog();
101 else
102 if(logfile) fclose(logfile);
103 if(debugfile) fclose(debugfile);
104 }
106 void vlogwrite(int pri, const char *fmt, va_list args)
107 {
108 if((conf.do_verbose && (pri & LOG_VERBOSE)) || (pri == LOG_ALERT) || (pri == LOG_WARNING)){
109 va_list args_copy;
110 va_copy(args_copy, args);
111 vfprintf(stdout, fmt, args_copy);
112 va_end(args_copy);
113 fflush(stdout); /* is this necessary? */
114 }
116 pri &= ~LOG_VERBOSE;
117 if(pri){
119 if(conf.use_syslog)
120 vsyslog(pri, fmt, args);
121 else{
122 if(pri <= conf.log_max_pri){
123 FILE *file = logfile ? logfile : stderr;
124 time_t now = time(NULL);
125 struct tm *t = localtime(&now);
126 gchar buf[24];
127 uid_t saved_uid;
128 gid_t saved_gid;
130 saved_gid = setegid(conf.mail_gid);
131 saved_uid = seteuid(conf.mail_uid);
133 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
134 fprintf(file, "%s [%d] ", buf, getpid());
136 vfprintf(file, fmt, args);
137 fflush(file);
139 seteuid(saved_uid);
140 setegid(saved_gid);
141 }
142 }
143 }
144 }
146 #ifdef ENABLE_DEBUG
147 void vdebugwrite(int pri, const char *fmt, va_list args)
148 {
149 time_t now = time(NULL);
150 struct tm *t = localtime(&now);
151 gchar buf[24];
152 strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t);
154 if(debugfile){
155 fprintf(debugfile, "%s [%d] ", buf, getpid());
157 vfprintf(debugfile, fmt, args);
158 fflush(debugfile);
159 }else{
160 fprintf(stderr, "no debug file, msg was:\n");
161 vfprintf(stderr, fmt, args);
162 }
163 }
164 #endif
166 void logwrite(int pri, const char *fmt, ...)
167 {
168 va_list args, args_copy;
169 int saved_errno = errno; /* somewhere this is changed to EBADF */
171 va_start(args, fmt);
172 #ifdef ENABLE_DEBUG
173 va_copy(args_copy, args);
174 #endif
175 vlogwrite(pri, fmt, args);
176 #ifdef ENABLE_DEBUG
177 if(debugfile)
178 vdebugwrite(pri, fmt, args_copy);
179 va_end(args_copy);
180 #endif
181 va_end(args);
183 errno = saved_errno;
184 }
186 #ifdef ENABLE_DEBUG
187 void 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 vdebugf(const char *fmt, va_list args)
198 {
199 vdebugwrite(LOG_DEBUG, fmt, args);
200 }
201 #endif
203 void maillog(const char *fmt, ...)
204 {
205 va_list args;
206 va_start(args, fmt);
208 vlogwrite(LOG_NOTICE, fmt, args);
210 va_end(args);
211 }