Index: ctx.c
===================================================================
--- ctx.c	(revision 4122)
+++ ctx.c	(working copy)
@@ -209,7 +209,7 @@
 	int err = 0;
 	const scconf_list *list;
 	const char *val, *s_internal = "internal";
-    const char *debug = NULL;
+	const char *debug = NULL;
 
 	ctx->debug = scconf_get_int(block, "debug", ctx->debug);
 	debug = getenv("OPENSC_DEBUG");
@@ -227,6 +227,13 @@
 		else
 		        ctx->debug_file = fopen(val, "a");
 	}
+	val = getenv("OPENSC_DEBUG_FILE");
+	if (val) {
+		if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout))
+			fclose(ctx->debug_file);
+		ctx->debug_file = fopen(val, "a");
+	}
+
 	val = scconf_get_str(block, "force_card_driver", NULL);
 	if (val) {
 		if (opts->forced_card_driver)
