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
00116 ASSERT(format);
00117
00118 va_start(ap,format);
00119 vsnprintf(msg, STRLEN, format, ap);
00120 va_end(ap);
00121
00122 if(! Run.dolog) goto nolog;
00123
00124 if(Run.use_syslog) {
00125
00126 syslog(LOG_ERR|LOG_USER, "%s", msg);
00127
00128 } else if(LOG) {
00129
00130 fprintf(LOG,"[%s] %s", timestr(), msg);
00131 fflush(LOG);
00132
00133 }
00134
00135 nolog:
00136 if(Run.have_tty) {
00137
00138 fprintf(stdout, "%s", msg);
00139 fflush(stdout);
00140
00141 }
00142
00143 }
00144
00145
00149 void log_close() {
00150
00151 if(Run.use_syslog) {
00152
00153 closelog();
00154
00155 }
00156
00157 if(LOG && (0 != fclose(LOG))) {
00158
00159 error("%s: Error closing the log file -- %s\n",
00160 prog, STRERROR);
00161
00162 }
00163
00164 LOG= NULL;
00165
00166 }
00167
00168
00173 int get_log_fd() {
00174
00175 return (LOG!=NULL?fileno(LOG):-1);
00176
00177 }
00178
00179
00180
00181
00182
00186 static int open_log() {
00187
00188 if(Run.use_syslog) {
00189
00190 openlog(prog, LOG_PID, LOG_USER);
00191
00192 } else {
00193
00194 umask(LOGMASK);
00195
00196 if((LOG= fopen(Run.logfile,"a+")) == (FILE *)NULL) {
00197
00198 error("%s: Error opening the log file '%s' for writing -- %s\n",
00199 prog, Run.logfile, STRERROR);
00200
00201 return(FALSE);
00202
00203 }
00204
00205 }
00206
00207 return TRUE;
00208
00209 }
00210
00211
00216 static char *timestr() {
00217
00218 time_t now;
00219
00220 time(&now);
00221 if(!strftime(time_str, STRLEN, TIMEFORMAT, localtime(&now))) {
00222
00223 memset(time_str, 0, STRLEN);
00224
00225 }
00226
00227 return time_str;
00228
00229 }
00230
00231
00232
00233