From 6ee0c41f7b32ec1dc40e41246f0b709c54ea119d Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Mon, 22 Oct 2012 22:18:57 +1100 Subject: [PATCH] hopefully fix the seg fault on darwin also fixes a leak where the old fh wasn't closed when init_log was called --- context.c | 21 +++++++++++++-------- imageri.h | 8 +++++++- log.c | 11 ++++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/context.c b/context.c index cab65a9f..4645ebf5 100644 --- a/context.c +++ b/context.c @@ -116,7 +116,7 @@ im_context_refdec(im_context_t ctx, const char *where) { myfree(ctx->error_stack[i].msg); } #ifdef IMAGER_LOG - if (ctx->lg_file) + if (ctx->lg_file && ctx->own_log) fclose(ctx->lg_file); #endif @@ -163,13 +163,18 @@ im_context_clone(im_context_t ctx, const char *where) { #ifdef IMAGER_LOG nctx->log_level = ctx->log_level; if (ctx->lg_file) { - /* disable buffering, this isn't perfect */ - setvbuf(ctx->lg_file, NULL, _IONBF, 0); - - /* clone that and disable buffering some more */ - nctx->lg_file = fdopen(fileno(ctx->lg_file), "a"); - if (nctx->lg_file) - setvbuf(nctx->lg_file, NULL, _IONBF, 0); + if (ctx->own_log) { + int newfd = dup(fileno(ctx->lg_file)); + nctx->own_log = 1; + nctx->lg_file = fdopen(newfd, "w"); + if (nctx->lg_file) + setvbuf(nctx->lg_file, NULL, _IONBF, BUFSIZ); + } + else { + /* stderr */ + nctx->lg_file = ctx->lg_file; + nctx->own_log = 0; + } } else { nctx->lg_file = NULL; diff --git a/imageri.h b/imageri.h index 6ef28a7a..2848bcfc 100644 --- a/imageri.h +++ b/imageri.h @@ -118,8 +118,14 @@ typedef struct im_context_tag { size_t error_alloc[IM_ERROR_COUNT]; i_errmsg error_stack[IM_ERROR_COUNT]; #ifdef IMAGER_LOG - int log_level; + /* the log file and level for this context */ FILE *lg_file; + int log_level; + + /* whether we own the lg_file, false for stderr and for cloned contexts */ + int own_log; + + /* values supplied by lhead */ const char *filename; int line; #endif diff --git a/log.c b/log.c index 34b77708..34e4a737 100644 --- a/log.c +++ b/log.c @@ -30,21 +30,30 @@ im_vloog(pIMCTX, int level, const char *fmt, va_list ap); int im_init_log(pIMCTX, const char* name,int level) { i_clear_error(); + + if (aIMCTX->lg_file) { + if (aIMCTX->own_log) + fclose(aIMCTX->lg_file); + aIMCTX->lg_file = NULL; + } + aIMCTX->log_level = level; if (level < 0) { aIMCTX->lg_file = NULL; } else { if (name == NULL) { aIMCTX->lg_file = stderr; + aIMCTX->own_log = 0; } else { if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) { im_push_errorf(aIMCTX, errno, "Cannot open file '%s': (%d)", name, errno); return 0; } + aIMCTX->own_log = 1; + setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ); } } if (aIMCTX->lg_file) { - setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ); im_log((aIMCTX, 0,"Imager - log started (level = %d)\n", level)); } -- 2.39.5