1
0
forked from bip/bip

[log] Use canonical name to detect need for log rotation

Also reset store on part from server to close more file descriptor.
This commit is contained in:
Arnaud Cornet 2009-06-28 16:33:29 +02:00
parent 4532234426
commit 405f8b4afc
3 changed files with 30 additions and 16 deletions

View File

@ -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) if (irc_line_count(line) != 2 && irc_line_count(line) != 3)
return ERR_PROTOCOL; return ERR_PROTOCOL;
cname = irc_line_elem(line, 1); cname = (char *)irc_line_elem(line, 1);
log_reset_store(LINK(irc)->log, cname);
log_drop(LINK(irc)->log, cname);
if ((ci = hash_remove_if_exists(&LINK(irc)->chan_infos, if ((ci = hash_remove_if_exists(&LINK(irc)->chan_infos,
cname)) != NULL) { 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, log_part(LINK(server)->log, line->origin, s_chan,
irc_line_count(line) == 3 ? irc_line_elem(line, 2) : irc_line_count(line) == 3 ? irc_line_elem(line, 2) :
NULL); NULL);
log_reset_store(LINK(server)->log, s_chan);
log_drop(LINK(server)->log, s_chan);
hash_remove(&server->channels, s_chan); hash_remove(&server->channels, s_chan);
channel_free(channel); 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_elem(line, 2),
irc_line_count(line) == 4 ? irc_line_count(line) == 4 ?
irc_line_elem(line, 3) : NULL); 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 (LINK(server)->autojoin_on_kick) {
if (!channel->key) if (!channel->key)

View File

@ -137,8 +137,8 @@ char *log_build_filename(log_t *logdata, const char *destination)
struct tm *now; struct tm *now;
time_t s; time_t s;
char *dest = bip_strdup(destination); char *dest = bip_strdup(destination);
strtolower(dest);
strtolower(dest);
logfile = (char *)bip_malloc(MAX_PATH_LEN + 1); logfile = (char *)bip_malloc(MAX_PATH_LEN + 1);
time(&s); time(&s);
@ -257,18 +257,23 @@ static int log_add_file(log_t *logdata, const char *destination,
FILE *f; FILE *f;
logstore_t *store; logstore_t *store;
char *uniq_fname; char *uniq_fname;
char *canonical_fname = NULL;
logfile_t *lf = NULL; logfile_t *lf = NULL;
if (conf_log) { 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); uniq_fname = filename_uniq(filename);
else } else {
canonical_fname = bip_strdup(filename);
uniq_fname = bip_strdup(filename); uniq_fname = bip_strdup(filename);
}
f = fopen(uniq_fname, "a+"); f = fopen(uniq_fname, "a+");
if (!f) { if (!f) {
mylog(LOG_ERROR, "fopen(%s) %s", uniq_fname, mylog(LOG_ERROR, "fopen(%s) %s", uniq_fname,
strerror(errno)); strerror(errno));
free(uniq_fname); free(uniq_fname);
free(canonical_fname);
return 0; 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, mylog(LOG_ERROR, "fseek(%s) %s", uniq_fname,
strerror(errno)); strerror(errno));
free(uniq_fname); free(uniq_fname);
free(canonical_fname);
fclose(f); fclose(f);
return 0; return 0;
} }
@ -284,11 +290,11 @@ static int log_add_file(log_t *logdata, const char *destination,
lf->file = f; lf->file = f;
lf->len = ftell(f); lf->len = ftell(f);
lf->filename = uniq_fname; lf->filename = uniq_fname;
lf->canonical_filename = canonical_fname;
log_updatelast(lf); log_updatelast(lf);
} }
store = hash_get(&logdata->logfgs, destination); store = hash_get(&logdata->logfgs, destination);
if (!store) { if (!store) {
store = bip_calloc(sizeof(logstore_t), 1); store = bip_calloc(sizeof(logstore_t), 1);
list_init(&store->file_group, NULL); list_init(&store->file_group, NULL);
@ -322,6 +328,8 @@ void logfile_free(logfile_t *lf)
fclose(lf->file); fclose(lf->file);
if (lf->filename) if (lf->filename)
free(lf->filename); free(lf->filename);
if (lf->canonical_filename)
free(lf->canonical_filename);
free(lf); free(lf);
} }
@ -330,8 +338,6 @@ logstore_t *log_find_file(log_t *logdata, const char *destination)
logfile_t *lf; logfile_t *lf;
logstore_t *store; logstore_t *store;
char *filename = NULL; char *filename = NULL;
time_t t;
struct tm *ltime;
struct link *l; struct link *l;
store = hash_get(&logdata->logfgs, destination); 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 */ /* This is reached if store already exists */
lf = list_get_last(&store->file_group);
time_t t;
struct tm *ltime;
time(&t); time(&t);
ltime = localtime(&t); ltime = localtime(&t);
lf = list_get_last(&store->file_group); if (ltime->tm_hour != lf->last_log.tm_hour) {
if (ltime->tm_mday != lf->last_log.tm_mday) {
logfile_t *oldlf; logfile_t *oldlf;
/* day changed, we might want to rotate logfile */ /* 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) if (!filename)
return NULL; return NULL;
if (strcmp(lf->filename, filename) == 0) { if (strcmp(lf->canonical_filename, filename) == 0) {
/* finally we don't */ /* finally we don't */
free(filename); free(filename);
return store; return store;
} }
/* we do want do rotate logfiles */ /* we do want to rotate logfiles */
mylog(LOG_DEBUG, "Rotating logfile for %s from", destination); mylog(LOG_DEBUG, "Rotating logfile for %s %s %s", destination,
lf->filename, filename);
oldlf = list_get_last(&store->file_group); oldlf = list_get_last(&store->file_group);
if (!log_add_file(logdata, destination, filename)) { if (!log_add_file(logdata, destination, filename)) {
free(filename); free(filename);
@ -662,7 +674,7 @@ void log_store_free(logstore_t *store)
logfile_t *lf; logfile_t *lf;
log_reset(store); log_reset(store);
if ((lf = list_remove_first(&store->file_group))) while ((lf = list_remove_first(&store->file_group)))
logfile_free(lf); logfile_free(lf);
free(store->name); free(store->name);
if (store->memlog) if (store->memlog)

View File

@ -31,6 +31,7 @@ typedef struct logfile
{ {
FILE *file; FILE *file;
char *filename; char *filename;
char *canonical_filename;
struct tm last_log; struct tm last_log;
size_t len; size_t len;
} logfile_t; } logfile_t;