comparison src/log.c @ 10:26e34ae9a3e3

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