From 405f8b4afc33bac9fa8d7afffd526e9a02c81f0d Mon Sep 17 00:00:00 2001 From: Arnaud Cornet Date: Sun, 28 Jun 2009 16:33:29 +0200 Subject: [PATCH] [log] Use canonical name to detect need for log rotation Also reset store on part from server to close more file descriptor. --- src/irc.c | 9 +++++---- src/log.c | 36 ++++++++++++++++++++++++------------ src/log.h | 1 + 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/irc.c b/src/irc.c index 2bad674..d67cce2 100644 --- a/src/irc.c +++ b/src/irc.c @@ -1067,10 +1067,7 @@ static int irc_cli_part(struct link_client *irc, struct line *line) if (irc_line_count(line) != 2 && irc_line_count(line) != 3) return ERR_PROTOCOL; - cname = irc_line_elem(line, 1); - - log_reset_store(LINK(irc)->log, cname); - log_drop(LINK(irc)->log, cname); + cname = (char *)irc_line_elem(line, 1); if ((ci = hash_remove_if_exists(&LINK(irc)->chan_infos, cname)) != NULL) { @@ -1515,6 +1512,8 @@ static int irc_part(struct link_server *server, struct line *line) log_part(LINK(server)->log, line->origin, s_chan, irc_line_count(line) == 3 ? irc_line_elem(line, 2) : NULL); + log_reset_store(LINK(server)->log, s_chan); + log_drop(LINK(server)->log, s_chan); hash_remove(&server->channels, s_chan); channel_free(channel); @@ -1791,6 +1790,8 @@ static int irc_kick(struct link_server *server, struct line *line) irc_line_elem(line, 2), irc_line_count(line) == 4 ? irc_line_elem(line, 3) : NULL); + log_reset_store(LINK(server)->log, channel->name); + log_drop(LINK(server)->log, channel->name); if (LINK(server)->autojoin_on_kick) { if (!channel->key) diff --git a/src/log.c b/src/log.c index 4b929d7..6fa16ce 100644 --- a/src/log.c +++ b/src/log.c @@ -137,8 +137,8 @@ char *log_build_filename(log_t *logdata, const char *destination) struct tm *now; time_t s; char *dest = bip_strdup(destination); - strtolower(dest); + strtolower(dest); logfile = (char *)bip_malloc(MAX_PATH_LEN + 1); time(&s); @@ -257,18 +257,23 @@ static int log_add_file(log_t *logdata, const char *destination, FILE *f; logstore_t *store; char *uniq_fname; + char *canonical_fname = NULL; logfile_t *lf = NULL; if (conf_log) { - if (log_has_file(logdata, filename)) + if (log_has_file(logdata, filename)) { + canonical_fname = bip_strdup(filename); uniq_fname = filename_uniq(filename); - else + } else { + canonical_fname = bip_strdup(filename); uniq_fname = bip_strdup(filename); + } f = fopen(uniq_fname, "a+"); if (!f) { mylog(LOG_ERROR, "fopen(%s) %s", uniq_fname, strerror(errno)); free(uniq_fname); + free(canonical_fname); return 0; } @@ -276,6 +281,7 @@ static int log_add_file(log_t *logdata, const char *destination, mylog(LOG_ERROR, "fseek(%s) %s", uniq_fname, strerror(errno)); free(uniq_fname); + free(canonical_fname); fclose(f); return 0; } @@ -284,11 +290,11 @@ static int log_add_file(log_t *logdata, const char *destination, lf->file = f; lf->len = ftell(f); lf->filename = uniq_fname; + lf->canonical_filename = canonical_fname; log_updatelast(lf); } store = hash_get(&logdata->logfgs, destination); - if (!store) { store = bip_calloc(sizeof(logstore_t), 1); list_init(&store->file_group, NULL); @@ -322,6 +328,8 @@ void logfile_free(logfile_t *lf) fclose(lf->file); if (lf->filename) free(lf->filename); + if (lf->canonical_filename) + free(lf->canonical_filename); free(lf); } @@ -330,8 +338,6 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) logfile_t *lf; logstore_t *store; char *filename = NULL; - time_t t; - struct tm *ltime; struct link *l; store = hash_get(&logdata->logfgs, destination); @@ -373,10 +379,14 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) } /* This is reached if store already exists */ + lf = list_get_last(&store->file_group); + + time_t t; + struct tm *ltime; + time(&t); ltime = localtime(&t); - lf = list_get_last(&store->file_group); - if (ltime->tm_mday != lf->last_log.tm_mday) { + if (ltime->tm_hour != lf->last_log.tm_hour) { logfile_t *oldlf; /* day changed, we might want to rotate logfile */ @@ -384,14 +394,16 @@ logstore_t *log_find_file(log_t *logdata, const char *destination) if (!filename) return NULL; - if (strcmp(lf->filename, filename) == 0) { + if (strcmp(lf->canonical_filename, filename) == 0) { /* finally we don't */ free(filename); return store; } - /* we do want do rotate logfiles */ - mylog(LOG_DEBUG, "Rotating logfile for %s from", destination); + /* we do want to rotate logfiles */ + mylog(LOG_DEBUG, "Rotating logfile for %s %s %s", destination, + lf->filename, filename); + oldlf = list_get_last(&store->file_group); if (!log_add_file(logdata, destination, filename)) { free(filename); @@ -662,7 +674,7 @@ void log_store_free(logstore_t *store) logfile_t *lf; log_reset(store); - if ((lf = list_remove_first(&store->file_group))) + while ((lf = list_remove_first(&store->file_group))) logfile_free(lf); free(store->name); if (store->memlog) diff --git a/src/log.h b/src/log.h index 9a214ab..2966899 100644 --- a/src/log.h +++ b/src/log.h @@ -31,6 +31,7 @@ typedef struct logfile { FILE *file; char *filename; + char *canonical_filename; struct tm last_log; size_t len; } logfile_t;