]> git.imager.perl.org - imager.git/blobdiff - context.c
clean up some old junk in log.c
[imager.git] / context.c
index 0dd70625d26e27cefcc18744dd6d39f353153bad..75fcd944c9b39d550c236f149bf98aba6f9b8d8e 100644 (file)
--- a/context.c
+++ b/context.c
@@ -38,8 +38,7 @@ im_context_new(void) {
   ctx->max_height = 0;
   ctx->max_bytes = DEF_BYTES_LIMIT;
 
-  ctx->slot_alloc = slot_count;
-  ctx->slots = calloc(sizeof(void *), ctx->slot_alloc);
+  ctx->slots = calloc(sizeof(void *), slot_count);
   if (!ctx->slots) {
     free(ctx);
     return NULL;
@@ -117,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
 
@@ -129,6 +128,8 @@ im_context_refdec(im_context_t ctx, const char *where) {
 
 Clone an Imager context object, returning the result.
 
+The error stack is not copied from the original context.
+
 =cut
 */
 
@@ -140,37 +141,33 @@ im_context_clone(im_context_t ctx, const char *where) {
   if (!nctx)
     return NULL;
 
-  nctx->slot_alloc = slot_count;
-  nctx->slots = calloc(sizeof(void *), ctx->slot_alloc);
+  nctx->slots = calloc(sizeof(void *), slot_count);
   if (!nctx->slots) {
     free(nctx);
     return NULL;
   }
+  nctx->slot_alloc = slot_count;
 
-  nctx->error_sp = ctx->error_sp;
+  nctx->error_sp = IM_ERROR_COUNT-1;
   for (i = 0; i < IM_ERROR_COUNT; ++i) {
-    if (ctx->error_stack[i].msg) {
-      size_t sz = ctx->error_alloc[i];
-      nctx->error_alloc[i] = sz;
-      nctx->error_stack[i].msg = mymalloc(sz);
-      memcpy(nctx->error_stack[i].msg, ctx->error_stack[i].msg, sz);
-    }
-    else {
-      nctx->error_alloc[i] = 0;
-      nctx->error_stack[i].msg = NULL;
-    }
-    nctx->error_stack[i].code = ctx->error_stack[i].code;
+    nctx->error_alloc[i] = 0;
+    nctx->error_stack[i].msg = NULL;
   }
 #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;
@@ -190,7 +187,7 @@ im_context_clone(im_context_t ctx, const char *where) {
 }
 
 /*
-=item im_context_slot_new(destructor, where)
+=item im_context_slot_new(destructor)
 
 Allocate a new context-local-storage slot.
 
@@ -198,7 +195,7 @@ Allocate a new context-local-storage slot.
 */
 
 im_slot_t
-im_context_slot_new(im_slot_destroy_t destructor, const char *where) {
+im_context_slot_new(im_slot_destroy_t destructor) {
   im_slot_t new_slot;
   im_slot_destroy_t *new_destructors;
   if (!slot_mutex)
@@ -214,11 +211,6 @@ im_context_slot_new(im_slot_destroy_t destructor, const char *where) {
 
   slot_destructors[new_slot] = destructor;
 
-#ifdef IMAGER_TRACE_CONTEXT
-  fprintf(stderr, "im_context: slot %d allocated for %s\n",
-         (int)new_slot, where);
-#endif
-
   i_mutex_unlock(slot_mutex);
 
   return new_slot;
@@ -263,11 +255,6 @@ im_context_slot_set(im_context_t ctx, im_slot_t slot, void *value) {
 
   ctx->slots[slot] = value;
 
-#ifdef IMAGER_TRACE_CONTEXT
-  fprintf(stderr, "im_context: ctx %p slot %d set to %p\n",
-         ctx, (int)slot, value);
-#endif
-
   return 1;
 }
 
@@ -291,10 +278,5 @@ im_context_slot_get(im_context_t ctx, im_slot_t slot) {
   if (slot >= ctx->slot_alloc)
     return NULL;
 
-#ifdef IMAGER_TRACE_CONTEXT
-  fprintf(stderr, "im_context: ctx %p slot %d retrieved as %p\n",
-         ctx, (int)slot, ctx->slots[slot]);
-#endif
-
   return ctx->slots[slot];
 }