1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/*
* filelog.c - Logging mechanism
*
* Copyright (C) 1999 Hugo Haas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include "log.h"
extern struct loginfo logi;
/* Mutex either for stdout or for a file */
pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Variables used for last message repetition */
char last_message[BUFFER_SIZE] = "";
time_t last_repeat;
unsigned int repeats = 0;
/*
* stdout_loginfo
*
* When run with debug options, write everything to stdout (run_as_daemon is 0)
*/
void stdout_loginfo(struct loginfo *li) {
li->log = filedesc_log;
li->level_or_fd = 1;
if (li->file != NULL)
free(li->file);
li->file = NULL;
li->open = dummy_open;
li->close = dummy_close;
}
/*
* init_loginfo
*
* Initializes a log_info structure (uses syslog)
*/
void init_loginfo(struct loginfo *li) {
extern int run_as_daemon;
if (!run_as_daemon) {
stdout_loginfo(li);
return;
}
li->log = syslog;
li->level_or_fd = LOGLEVEL;
if (li->file != NULL)
free(li->file);
li->file = NULL;
li->open = syslog_open;
li->close = dummy_close;
}
/*
* log_in_file
*
* Set a loginfo structure such as it will log things in a file
*/
void log_in_file(struct loginfo *li, char *filename) {
extern int run_as_daemon;
if (!run_as_daemon) {
stdout_loginfo(li);
return;
}
li->log = filedesc_log;
li->level_or_fd = 0;
li->open = file_open;
li->close = file_close;
if (li->file != NULL)
free(li->file);
li->file = strdup(filename);
}
/*
* dummy_open, dummy_close
*
* Dummy functions
*/
void dummy_open(int *fd, const char *filename) {}
void dummy_close(int *fd, const char *filename) {}
/*
* syslog_open
*
* Open a connection to the system logging daemon. Register ippl as a daemon.
*/
void syslog_open(int *level, const char *filename) {
openlog("ippl", 0, LOG_DAEMON);
}
/*
* log_entry
*
* Log an entry into a stream
*/
void log_entry(int fd, char *entry) {
time_t current = time(NULL);
char date[27];
char repeat_message[40];
if (fd == 0)
return;
pthread_mutex_lock(&log_mutex);
if (!strncmp(last_message, entry, BUFFER_SIZE)) {
repeats++;
last_repeat = current;
pthread_mutex_unlock(&log_mutex);
return;
}
if (repeats > 0) {
snprintf(date, 27, "%s", asctime(localtime(&last_repeat)));
snprintf(repeat_message, 40, "last message repeated %d time(s)\n", repeats);
write(fd, date+4, strlen(date)-10);
write(fd, " ", 1);
write(fd, repeat_message, strlen(repeat_message));
repeats = 0;
}
snprintf(date, 27, "%s", asctime(localtime(¤t)));
write(fd, date+4, strlen(date)-10);
write(fd, " ", 1);
write(fd, entry, (strlen(entry) < 1023) ? strlen(entry) : 1023 );
write(fd, "\n", 1);
snprintf(last_message, BUFFER_SIZE, "%s", entry);
pthread_mutex_unlock(&log_mutex);
}
/*
* filedesc_log
*
* Log information into a file descriptor
*/
void filedesc_log(const int fd, const char *format, ...) {
char buffer[BUFFER_SIZE];
va_list msg;
va_start(msg, format);
vsnprintf(buffer, BUFFER_SIZE, format, msg);
va_end(msg);
log_entry(fd, buffer);
}
/*
* file_open
*
* Open a log file
*/
void file_open(int *fd, const char *filename) {
*fd = open((const char *) filename, O_WRONLY | O_APPEND | O_CREAT, 0640);
if (*fd == 0) {
logi.log(logi.level_or_fd, "Can't open log file %s.", filename);
}
}
/*
* file_close
*
* Close a log file
*/
void file_close(int *fd, const char *filename) {
fsync(*fd);
close(*fd);
}
|