static void start_log(struct log_stream *log) { static int ht_initialized = 0; FILE *f; if (!log->filename || !*log->filename) { log->fp = stderr; } else { if (!ht_initialized) { hashinit(&htab_logfiles, 8); ht_initialized = 1; } if ((f = hashfind(strupper(log->filename), &htab_logfiles))) { /* We've already opened this file for another log, so just use that pointer */ log->fp = f; } else { log->fp = fopen(log->filename, "a+"); if (log->fp == NULL) { fprintf(stderr, "WARNING: cannot open log %s: %s\n", log->filename, strerror(errno)); log->fp = stderr; } else { hashadd(strupper(log->filename), log->fp, &htab_logfiles); fputs("START OF LOG.\n", log->fp); fflush(log->fp); } } } if (!log->buffer) log->buffer = allocate_bufferq(LOG_BUFFER_SIZE); }
/** Reallocate a buffer queue (to change its size) * \param bq pointer to buffer queue. * \param lines new number of lines to store in buffer queue. * \retval address of reallocated buffer queue. */ BUFFERQ * reallocate_bufferq(BUFFERQ *bq, int lines) { char *newbuff; ptrdiff_t bufflen; int bytes = lines * (BUFFER_LEN + 2 * BUFFERQLINEOVERHEAD); /* If we were accidentally called without a buffer, deal */ if (!bq) { return allocate_bufferq(lines); } /* Are we not changing size? */ if (bq->buffer_size == bytes) return bq; if (bq->buffer_size > bytes) { /* Shrinking the buffer */ if ((bq->buffer_end - bq->buffer) >= bytes) shift_bufferq(bq, bq->buffer_end - bq->buffer - bytes); } bufflen = bq->buffer_end - bq->buffer; newbuff = realloc(bq->buffer, bytes); if (newbuff) { bq->buffer = newbuff; bq->buffer_end = bq->buffer + bufflen; bq->buffer_size = bytes; } return bq; }