00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <stdio.h>
00023 #include <stdarg.h>
00024 #include <errno.h>
00025 #include <stdlib.h>
00026 #include <time.h>
00027
00028 #ifdef HAVE_SYSLOG_H
00029 #include <syslog.h>
00030 #endif
00031
00032 #ifdef HAVE_STRING_H
00033 #include <string.h>
00034 #endif
00035
00036 #ifdef HAVE_STRINGS_H
00037 #include <strings.h>
00038 #endif
00039
00040 #ifdef HAVE_UNISTD_H
00041 #include <unistd.h>
00042 #endif
00043
00044 #ifdef HAVE_SYS_TYPES_H
00045 #include <sys/types.h>
00046 #endif
00047
00048 #ifdef HAVE_SYS_STAT_H
00049 #include <sys/stat.h>
00050 #endif
00051
00052 #include "monitor.h"
00053
00054
00055
00056 static FILE *LOG= NULL;
00057 static char time_str[STRLEN];
00058
00059
00060 static int open_log();
00061 static char *timestr(void);
00062
00063
00077
00078
00079
00084 int log_init() {
00085
00086 if ( !Run.dolog ) {
00087
00088 return TRUE;
00089
00090 }
00091
00092 if ( !open_log() ) {
00093
00094 return FALSE;
00095
00096 }
00097
00098
00099
00100 atexit(log_close);
00101
00102 return TRUE;
00103
00104 }
00105
00106
00111 void log(const char *format, ...) {
00112
00113 char msg[STRLEN];
00114 va_list ap;
00115 va_start(ap,format);
00116 vsnprintf(msg, STRLEN, format, ap);
00117 va_end(ap);
00118
00119 if ( ! Run.dolog ) goto nolog;
00120
00121 if ( Run.use_syslog ) {
00122
00123 syslog(LOG_ERR|LOG_USER, "%s", msg);
00124
00125 }
00126 else if ( LOG ) {
00127
00128 fprintf(LOG,"[%s] %s", timestr(), msg);
00129 fflush(LOG);
00130
00131 }
00132
00133 nolog:
00134
00135 if ( have_tty && Run.debug ) {
00136
00137 fprintf(stdout, "%s", msg);
00138 fflush(stdout);
00139
00140 }
00141
00142 }
00143
00144
00148 void log_close() {
00149
00150 if ( Run.use_syslog ) {
00151
00152 closelog();
00153
00154 }
00155
00156 if ( LOG && 0 != fclose(LOG) ) {
00157
00158 error("%s: Error closing the log file -- %s\n",
00159 prog, STRERROR);
00160
00161 }
00162
00163 LOG= NULL;
00164
00165 }
00166
00167
00172 int get_log_fd() {
00173
00174 return ( LOG!=NULL?fileno(LOG):-1 );
00175
00176 }
00177
00178
00179
00180
00181
00185 static int open_log() {
00186
00187 if ( Run.use_syslog ) {
00188
00189 openlog(prog, LOG_PID, LOG_USER);
00190
00191 } else {
00192
00193 umask(LOGMASK);
00194
00195 if ((LOG= fopen(Run.logfile,"a+")) == (FILE *)NULL) {
00196
00197 error("%s: Error opening the log file '%s' for writing -- %s\n",
00198 prog, Run.logfile, STRERROR);
00199
00200 return(FALSE);
00201
00202 }
00203
00204 }
00205
00206 return TRUE;
00207
00208 }
00209
00210
00215 static char *timestr() {
00216
00217 time_t now;
00218
00219 time(&now);
00220 if ( !strftime(time_str, STRLEN, TIMEFORMAT, localtime(&now)) ) {
00221
00222 memset(time_str, 0, STRLEN);
00223
00224 }
00225
00226 return time_str;
00227
00228 }
00229
00230
00231
00232