masqmail
diff 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 |
line diff
1.1 --- a/src/log.c Mon Oct 27 16:21:27 2008 +0100 1.2 +++ b/src/log.c Mon Oct 27 16:23:10 2008 +0100 1.3 @@ -21,191 +21,201 @@ 1.4 #include "sysexits.h" 1.5 1.6 static char *_sysexit_strings[] = { 1.7 - "command line usage error", 1.8 - "data format error", 1.9 - "cannot open input", 1.10 - "addressee unknown", 1.11 - "host name unknown", 1.12 - "service unavailable", 1.13 - "internal software error", 1.14 - "system error (e.g., can't fork)", 1.15 - "critical OS file missing", 1.16 - "can't create (user) output file", 1.17 - "input/output error", 1.18 - "temp failure; user is invited to retry", 1.19 - "remote error in protocol", 1.20 - "permission denied", 1.21 - "configuration error" 1.22 + "command line usage error", 1.23 + "data format error", 1.24 + "cannot open input", 1.25 + "addressee unknown", 1.26 + "host name unknown", 1.27 + "service unavailable", 1.28 + "internal software error", 1.29 + "system error (e.g., can't fork)", 1.30 + "critical OS file missing", 1.31 + "can't create (user) output file", 1.32 + "input/output error", 1.33 + "temp failure; user is invited to retry", 1.34 + "remote error in protocol", 1.35 + "permission denied", 1.36 + "configuration error" 1.37 }; 1.38 1.39 -gchar *ext_strerror(int err) 1.40 +gchar* 1.41 +ext_strerror(int err) 1.42 { 1.43 - if(err < 1024) 1.44 - return strerror(err); 1.45 - else 1.46 - if(err > 1024 + EX__BASE && 1.47 - (err - 1024 - EX__BASE < sizeof(_sysexit_strings)/sizeof(_sysexit_strings[0]))) 1.48 - return _sysexit_strings[err - 1024 - EX__BASE]; 1.49 + if (err < 1024) 1.50 + return strerror(err); 1.51 + else if (err > 1024 + EX__BASE 1.52 + && (err - 1024 - EX__BASE < sizeof(_sysexit_strings) / sizeof(_sysexit_strings[0]))) 1.53 + return _sysexit_strings[err - 1024 - EX__BASE]; 1.54 1.55 - return "unknown error"; 1.56 + return "unknown error"; 1.57 } 1.58 1.59 static FILE *logfile = NULL; 1.60 static FILE *debugfile = NULL; 1.61 1.62 -gboolean logopen() 1.63 +gboolean 1.64 +logopen() 1.65 { 1.66 - gchar *filename; 1.67 - mode_t saved_mode = umask(066); 1.68 + gchar *filename; 1.69 + mode_t saved_mode = umask(066); 1.70 1.71 - if(conf.use_syslog){ 1.72 - openlog(PACKAGE, LOG_PID, LOG_MAIL); 1.73 - }else{ 1.74 - uid_t saved_uid; 1.75 - gid_t saved_gid; 1.76 - 1.77 - saved_gid = setegid(conf.mail_gid); 1.78 - saved_uid = seteuid(conf.mail_uid); 1.79 + if (conf.use_syslog) { 1.80 + openlog(PACKAGE, LOG_PID, LOG_MAIL); 1.81 + } else { 1.82 + uid_t saved_uid; 1.83 + gid_t saved_gid; 1.84 1.85 - filename = g_strdup_printf("%s/masqmail.log", conf.log_dir); 1.86 - logfile = fopen(filename, "a"); 1.87 - if(!logfile){ 1.88 - fprintf(stderr, "could not open log '%s': %s\n", filename, strerror(errno)); 1.89 - return FALSE; 1.90 - } 1.91 - g_free(filename); 1.92 + saved_gid = setegid(conf.mail_gid); 1.93 + saved_uid = seteuid(conf.mail_uid); 1.94 1.95 - seteuid(saved_uid); 1.96 - setegid(saved_gid); 1.97 - } 1.98 + filename = g_strdup_printf("%s/masqmail.log", conf.log_dir); 1.99 + logfile = fopen(filename, "a"); 1.100 + if (!logfile) { 1.101 + fprintf(stderr, "could not open log '%s': %s\n", filename, strerror(errno)); 1.102 + return FALSE; 1.103 + } 1.104 + g_free(filename); 1.105 + 1.106 + seteuid(saved_uid); 1.107 + setegid(saved_gid); 1.108 + } 1.109 1.110 #ifdef ENABLE_DEBUG 1.111 - if(conf.debug_level > 0){ 1.112 - filename = g_strdup_printf("%s/debug.log", conf.log_dir); 1.113 - debugfile = fopen(filename, "a"); 1.114 - if(!debugfile){ 1.115 - fprintf(stderr, "could not open debug log '%s'\n", filename); 1.116 - return FALSE; 1.117 - } 1.118 - g_free(filename); 1.119 - } 1.120 + if (conf.debug_level > 0) { 1.121 + filename = g_strdup_printf("%s/debug.log", conf.log_dir); 1.122 + debugfile = fopen(filename, "a"); 1.123 + if (!debugfile) { 1.124 + fprintf(stderr, "could not open debug log '%s'\n", filename); 1.125 + return FALSE; 1.126 + } 1.127 + g_free(filename); 1.128 + } 1.129 #endif 1.130 - umask(saved_mode); 1.131 - return TRUE; 1.132 + umask(saved_mode); 1.133 + return TRUE; 1.134 } 1.135 1.136 -void logclose() 1.137 +void 1.138 +logclose() 1.139 { 1.140 - if(conf.use_syslog) 1.141 - closelog(); 1.142 - else 1.143 - if(logfile) fclose(logfile); 1.144 - if(debugfile) fclose(debugfile); 1.145 + if (conf.use_syslog) 1.146 + closelog(); 1.147 + else if (logfile) 1.148 + fclose(logfile); 1.149 + if (debugfile) 1.150 + fclose(debugfile); 1.151 } 1.152 1.153 -void vlogwrite(int pri, const char *fmt, va_list args) 1.154 +void 1.155 +vlogwrite(int pri, const char *fmt, va_list args) 1.156 { 1.157 - if((conf.do_verbose && (pri & LOG_VERBOSE)) || (pri == LOG_ALERT) || (pri == LOG_WARNING)){ 1.158 - va_list args_copy; 1.159 - va_copy(args_copy, args); 1.160 - vfprintf(stdout, fmt, args_copy); 1.161 - va_end(args_copy); 1.162 - fflush(stdout); /* is this necessary? */ 1.163 - } 1.164 + if ((conf.do_verbose && (pri & LOG_VERBOSE)) || (pri == LOG_ALERT) 1.165 + || (pri == LOG_WARNING)) { 1.166 + va_list args_copy; 1.167 + va_copy(args_copy, args); 1.168 + vfprintf(stdout, fmt, args_copy); 1.169 + va_end(args_copy); 1.170 + fflush(stdout); /* is this necessary? */ 1.171 + } 1.172 1.173 - pri &= ~LOG_VERBOSE; 1.174 - if(pri){ 1.175 + pri &= ~LOG_VERBOSE; 1.176 + if (pri) { 1.177 1.178 - if(conf.use_syslog) 1.179 - vsyslog(pri, fmt, args); 1.180 - else{ 1.181 - if(pri <= conf.log_max_pri){ 1.182 - FILE *file = logfile ? logfile : stderr; 1.183 + if (conf.use_syslog) 1.184 + vsyslog(pri, fmt, args); 1.185 + else { 1.186 + if (pri <= conf.log_max_pri) { 1.187 + FILE *file = logfile ? logfile : stderr; 1.188 + time_t now = time(NULL); 1.189 + struct tm *t = localtime(&now); 1.190 + gchar buf[24]; 1.191 + uid_t saved_uid; 1.192 + gid_t saved_gid; 1.193 + 1.194 + saved_gid = setegid(conf.mail_gid); 1.195 + saved_uid = seteuid(conf.mail_uid); 1.196 + 1.197 + strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t); 1.198 + fprintf(file, "%s [%d] ", buf, getpid()); 1.199 + 1.200 + vfprintf(file, fmt, args); 1.201 + fflush(file); 1.202 + 1.203 + seteuid(saved_uid); 1.204 + setegid(saved_gid); 1.205 + } 1.206 + } 1.207 + } 1.208 +} 1.209 + 1.210 +#ifdef ENABLE_DEBUG 1.211 +void 1.212 +vdebugwrite(int pri, const char *fmt, va_list args) 1.213 +{ 1.214 time_t now = time(NULL); 1.215 struct tm *t = localtime(&now); 1.216 gchar buf[24]; 1.217 - uid_t saved_uid; 1.218 - gid_t saved_gid; 1.219 + strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t); 1.220 1.221 - saved_gid = setegid(conf.mail_gid); 1.222 - saved_uid = seteuid(conf.mail_uid); 1.223 + if (debugfile) { 1.224 + fprintf(debugfile, "%s [%d] ", buf, getpid()); 1.225 1.226 - strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t); 1.227 - fprintf(file, "%s [%d] ", buf, getpid()); 1.228 - 1.229 - vfprintf(file, fmt, args); 1.230 - fflush(file); 1.231 - 1.232 - seteuid(saved_uid); 1.233 - setegid(saved_gid); 1.234 - } 1.235 - } 1.236 - } 1.237 -} 1.238 - 1.239 -#ifdef ENABLE_DEBUG 1.240 -void vdebugwrite(int pri, const char *fmt, va_list args) 1.241 -{ 1.242 - time_t now = time(NULL); 1.243 - struct tm *t = localtime(&now); 1.244 - gchar buf[24]; 1.245 - strftime(buf, 24, "%Y-%m-%d %H:%M:%S", t); 1.246 - 1.247 - if(debugfile){ 1.248 - fprintf(debugfile, "%s [%d] ", buf, getpid()); 1.249 - 1.250 - vfprintf(debugfile, fmt, args); 1.251 - fflush(debugfile); 1.252 - }else{ 1.253 - fprintf(stderr, "no debug file, msg was:\n"); 1.254 - vfprintf(stderr, fmt, args); 1.255 - } 1.256 + vfprintf(debugfile, fmt, args); 1.257 + fflush(debugfile); 1.258 + } else { 1.259 + fprintf(stderr, "no debug file, msg was:\n"); 1.260 + vfprintf(stderr, fmt, args); 1.261 + } 1.262 } 1.263 #endif 1.264 1.265 -void logwrite(int pri, const char *fmt, ...) 1.266 +void 1.267 +logwrite(int pri, const char *fmt, ...) 1.268 { 1.269 - va_list args, args_copy; 1.270 - int saved_errno = errno; /* somewhere this is changed to EBADF */ 1.271 + va_list args, args_copy; 1.272 + int saved_errno = errno; /* somewhere this is changed to EBADF */ 1.273 1.274 - va_start(args, fmt); 1.275 + va_start(args, fmt); 1.276 #ifdef ENABLE_DEBUG 1.277 - va_copy(args_copy, args); 1.278 + va_copy(args_copy, args); 1.279 #endif 1.280 - vlogwrite(pri, fmt, args); 1.281 + vlogwrite(pri, fmt, args); 1.282 #ifdef ENABLE_DEBUG 1.283 - if(debugfile) 1.284 - vdebugwrite(pri, fmt, args_copy); 1.285 - va_end(args_copy); 1.286 + if (debugfile) 1.287 + vdebugwrite(pri, fmt, args_copy); 1.288 + va_end(args_copy); 1.289 #endif 1.290 - va_end(args); 1.291 + va_end(args); 1.292 1.293 - errno = saved_errno; 1.294 + errno = saved_errno; 1.295 } 1.296 1.297 #ifdef ENABLE_DEBUG 1.298 -void debugf(const char *fmt, ...) 1.299 +void 1.300 +debugf(const char *fmt, ...) 1.301 { 1.302 - va_list args; 1.303 - va_start(args, fmt); 1.304 + va_list args; 1.305 + va_start(args, fmt); 1.306 1.307 - vdebugwrite(LOG_DEBUG, fmt, args); 1.308 + vdebugwrite(LOG_DEBUG, fmt, args); 1.309 1.310 - va_end(args); 1.311 + va_end(args); 1.312 } 1.313 1.314 -void vdebugf(const char *fmt, va_list args) 1.315 +void 1.316 +vdebugf(const char *fmt, va_list args) 1.317 { 1.318 - vdebugwrite(LOG_DEBUG, fmt, args); 1.319 + vdebugwrite(LOG_DEBUG, fmt, args); 1.320 } 1.321 #endif 1.322 1.323 -void maillog(const char *fmt, ...) 1.324 +void 1.325 +maillog(const char *fmt, ...) 1.326 { 1.327 - va_list args; 1.328 - va_start(args, fmt); 1.329 + va_list args; 1.330 + va_start(args, fmt); 1.331 1.332 - vlogwrite(LOG_NOTICE, fmt, args); 1.333 + vlogwrite(LOG_NOTICE, fmt, args); 1.334 1.335 - va_end(args); 1.336 + va_end(args); 1.337 }