[c5c522c] | 1 | /* $Id: error.c,v 1.21 2011/06/28 00:13:48 sbajic Exp $ */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | DSPAM |
---|
| 5 | COPYRIGHT (C) 2002-2012 DSPAM PROJECT |
---|
| 6 | |
---|
| 7 | This program is free software: you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU Affero General Public License as |
---|
| 9 | published by the Free Software Foundation, either version 3 of the |
---|
| 10 | License, or (at your option) any later version. |
---|
| 11 | |
---|
| 12 | This program is distributed in the hope that it will be useful, |
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | GNU Affero General Public License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU Affero General Public License |
---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | * error.c - error reporting |
---|
| 24 | * |
---|
| 25 | * DESCRIPTION |
---|
| 26 | * The error reporting facilities include: |
---|
| 27 | * LOGDEBUG Log to debug only |
---|
| 28 | * LOG Log to syslog, stderr, and debug |
---|
| 29 | */ |
---|
| 30 | |
---|
| 31 | #ifdef HAVE_CONFIG_H |
---|
| 32 | #include <auto-config.h> |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | #include <stdio.h> |
---|
| 36 | #include <string.h> |
---|
| 37 | #include <stdarg.h> |
---|
| 38 | #include <errno.h> |
---|
| 39 | #ifdef HAVE_UNISTD_H |
---|
| 40 | # include <unistd.h> |
---|
| 41 | #endif |
---|
| 42 | |
---|
| 43 | #ifdef TIME_WITH_SYS_TIME |
---|
| 44 | # include <sys/time.h> |
---|
| 45 | # include <time.h> |
---|
| 46 | #else |
---|
| 47 | # ifdef HAVE_SYS_TIME_H |
---|
| 48 | # include <sys/time.h> |
---|
| 49 | # else |
---|
| 50 | # include <time.h> |
---|
| 51 | # endif |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | #include "error.h" |
---|
| 55 | #include "util.h" |
---|
| 56 | #include "config.h" |
---|
| 57 | |
---|
| 58 | #ifdef DAEMON |
---|
| 59 | #include <pthread.h> |
---|
| 60 | pthread_mutex_t __syslog_lock; |
---|
| 61 | #endif |
---|
| 62 | |
---|
| 63 | #ifdef _WIN32 |
---|
| 64 | #include <process.h> |
---|
| 65 | #endif |
---|
| 66 | |
---|
| 67 | #ifndef _WIN32 |
---|
| 68 | void |
---|
| 69 | LOG(int priority, const char *err, ... ) |
---|
| 70 | { |
---|
| 71 | #if defined(USE_SYSLOG) || defined(LOGFILE) |
---|
| 72 | va_list ap; |
---|
| 73 | va_start (ap, err); |
---|
| 74 | #endif |
---|
| 75 | #ifdef LOGFILE |
---|
| 76 | char date[128]; |
---|
| 77 | FILE *file; |
---|
| 78 | #endif |
---|
| 79 | |
---|
| 80 | #ifdef DAEMON |
---|
| 81 | #if defined(USE_SYSLOG) && defined(LOGFILE) |
---|
| 82 | pthread_mutex_lock(&__syslog_lock); |
---|
| 83 | #endif |
---|
| 84 | #endif |
---|
| 85 | |
---|
| 86 | #ifdef USE_SYSLOG |
---|
| 87 | openlog ("dspam", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_MAIL); |
---|
| 88 | vsyslog (priority, err, ap); |
---|
| 89 | closelog (); |
---|
| 90 | #endif |
---|
| 91 | |
---|
| 92 | #ifdef LOGFILE |
---|
| 93 | char err_text[256]; |
---|
| 94 | vsnprintf(err_text, sizeof(err_text), err, ap); |
---|
| 95 | file = fopen(LOGFILE, "a"); |
---|
| 96 | if (file) { |
---|
| 97 | fprintf(file, "%ld: [%s] %s\n", (long) getpid(), format_date_r(date), err_text); |
---|
| 98 | fclose(file); |
---|
| 99 | } else { |
---|
| 100 | fprintf(stderr, "%s: %s", LOGFILE, strerror(errno)); |
---|
| 101 | fprintf(stderr, "%ld: [%s] %s\n", (long)getpid(), format_date_r(date), err_text); |
---|
| 102 | } |
---|
| 103 | #endif |
---|
| 104 | |
---|
| 105 | #if defined(USE_SYSLOG) || defined(LOGFILE) |
---|
| 106 | va_end (ap); |
---|
| 107 | #endif |
---|
| 108 | |
---|
| 109 | #ifdef DAEMON |
---|
| 110 | #if defined(USE_SYSLOG) && defined(LOGFILE) |
---|
| 111 | pthread_mutex_unlock(&__syslog_lock); |
---|
| 112 | #endif |
---|
| 113 | #endif |
---|
| 114 | |
---|
| 115 | return; |
---|
| 116 | } |
---|
| 117 | #endif |
---|
| 118 | |
---|
| 119 | #ifdef DEBUG |
---|
| 120 | void |
---|
| 121 | LOGDEBUG (const char *err, ... ) |
---|
| 122 | { |
---|
| 123 | char debug_text[1024]; |
---|
| 124 | va_list args; |
---|
| 125 | |
---|
| 126 | if (!DO_DEBUG) |
---|
| 127 | return; |
---|
| 128 | |
---|
| 129 | va_start (args, err); |
---|
| 130 | vsnprintf (debug_text, sizeof (debug_text), err, args); |
---|
| 131 | va_end (args); |
---|
| 132 | |
---|
| 133 | debug_out(debug_text); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void |
---|
| 137 | debug_out (const char *err) |
---|
| 138 | { |
---|
| 139 | FILE *file; |
---|
| 140 | char fn[MAX_FILENAME_LENGTH]; |
---|
| 141 | char buf[128]; |
---|
| 142 | |
---|
| 143 | if (DO_DEBUG == 1) { |
---|
| 144 | snprintf (fn, sizeof (fn), "%s/dspam.debug", LOGDIR); |
---|
| 145 | |
---|
| 146 | file = fopen (fn, "a"); |
---|
| 147 | if (file != NULL) { |
---|
| 148 | fprintf(file, "%ld: [%s] %s\n", (long) getpid(), format_date_r(buf), err); |
---|
| 149 | fclose(file); |
---|
| 150 | } |
---|
| 151 | } else if (DO_DEBUG == 2) { |
---|
| 152 | printf ("%ld: [%s] %s\n", (long) getpid (), format_date_r(buf), err); |
---|
| 153 | } |
---|
| 154 | return; |
---|
| 155 | } |
---|
| 156 | #endif |
---|
| 157 | |
---|
| 158 | char * |
---|
| 159 | format_date_r(char *buf) |
---|
| 160 | { |
---|
| 161 | struct tm *l; |
---|
| 162 | #ifdef HAVE_LOCALTIME_R |
---|
| 163 | struct tm lt; |
---|
| 164 | #endif |
---|
| 165 | time_t t = time(NULL); |
---|
| 166 | |
---|
| 167 | #ifdef HAVE_LOCALTIME_R |
---|
| 168 | l = localtime_r(&t, <); |
---|
| 169 | #else |
---|
| 170 | l = localtime(&t); |
---|
| 171 | #endif |
---|
| 172 | |
---|
| 173 | sprintf(buf, "%02d/%02d/%04d %02d:%02d:%02d", |
---|
| 174 | l->tm_mon+1, l->tm_mday, l->tm_year+1900, l->tm_hour, l->tm_min, |
---|
| 175 | l->tm_sec); |
---|
| 176 | return buf; |
---|
| 177 | } |
---|