Index: /branches/1.3/coverager.c
===================================================================
--- /branches/1.3/coverager.c	(revision 592)
+++ /branches/1.3/coverager.c	(revision 593)
@@ -27,7 +27,5 @@
 {
 	coverager_t cov = *(coverager_t*) pDest;
-#ifdef DEBUG
-	fprintf(stderr, "destroy %p\n", cov);
-#endif
+	TRACE("destroy %p", cov);
 	zend_hash_destroy(cov);
 	efree(cov);
@@ -39,7 +37,5 @@
 	struct stat st;
 
-#ifdef DEBUG
-	fprintf(stderr, "mkdirs %s %d %s %d\n", root, rootlen, path, pathlen);
-#endif
+	TRACE("mkdirs %s %d %s %d", root, rootlen, path, pathlen);
 	fullpath = do_alloca(rootlen + pathlen + 1);
 	memcpy(fullpath, root, rootlen);
@@ -56,7 +52,5 @@
 			*chr = PHP_DIR_SEPARATOR;
 		}
-#ifdef DEBUG
-		fprintf(stderr, "mkdir %s\n", fullpath);
-#endif
+		TRACE("mkdir %s", fullpath);
 #if PHP_MAJOR_VERSION > 5
 		php_stream_mkdir(fullpath, 0700, REPORT_ERRORS, NULL);
@@ -113,7 +107,5 @@
 
 	if (newfile) {
-#ifdef DEBUG
-		fprintf(stderr, "new file\n");
-#endif
+		TRACE("%s", "new file");
 	}
 	else if (outstat.st_size) {
@@ -123,7 +115,5 @@
 			goto bailout;
 		}
-#ifdef DEBUG
-		fprintf(stderr, "oldsize %d\n", (int) len);
-#endif
+		TRACE("oldsize %d", (int) len);
 		do {
 			p = (long *) contents;
@@ -133,7 +123,5 @@
 			}
 			if (*p++ != PCOV_HEADER_MAGIC) {
-#ifdef DEBUG
-				fprintf(stderr, "wrong magic in file %s\n", outfilename);
-#endif
+				TRACE("wrong magic in file %s", outfilename);
 				break;
 			}
@@ -290,7 +278,5 @@
 			strcpy(outfilename + dumpdir_len + size - 1, ".pcov");
 
-#ifdef DEBUG
-			fprintf(stderr, "outfilename %s\n", outfilename);
-#endif
+			TRACE("outfilename %s", outfilename);
 			xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC);
 			zend_hash_move_forward_ex(XG(coverages), &pos);
@@ -354,7 +340,5 @@
 
 	if (zend_hash_find(XG(coverages), filename, len, (void **) &pcov) == SUCCESS) {
-#ifdef DEBUG
-		fprintf(stderr, "got coverage %s %p\n", filename, *pcov);
-#endif
+		TRACE("got coverage %s %p", filename, *pcov);
 		return *pcov;
 	}
@@ -363,7 +347,5 @@
 		zend_hash_init(cov, 0, NULL, NULL, 0);
 		zend_hash_add(XG(coverages), filename, len, (void **) &cov, sizeof(cov), NULL);
-#ifdef DEBUG
-		fprintf(stderr, "new coverage %s %p\n", filename, cov);
-#endif
+		TRACE("new coverage %s %p", filename, cov);
 		return cov;
 	}
@@ -534,7 +516,5 @@
 	}
 	if (*p++ != PCOV_HEADER_MAGIC) {
-#ifdef DEBUG
-		fprintf(stderr, "wrong magic in xcache_coverager_decode");
-#endif
+		TRACE("%s", "wrong magic in xcache_coverager_decode");
 		return;
 	}
Index: /branches/1.3/mem.c
===================================================================
--- /branches/1.3/mem.c	(revision 592)
+++ /branches/1.3/mem.c	(revision 593)
@@ -13,9 +13,10 @@
 #include "xc_shm.h"
 #include "align.h"
+#include "utils.h"
 
 #ifdef TEST
-#	define ALLOC_DEBUG
-#endif
-#ifdef ALLOC_DEBUG
+#	define DEBUG
+#endif
+#ifdef DEBUG
 #	define ALLOC_DEBUG_BLOCK_CHECK
 #endif
@@ -91,17 +92,13 @@
 	realsize = ALIGN(realsize);
 
-#ifdef ALLOC_DEBUG
-	fprintf(stderr, "avail: %d (%dKB). Allocate size: %d realsize: %d (%dKB)"
+	TRACE("avail: %d (%dKB). Allocate size: %d realsize: %d (%dKB)"
 			, mem->avail, mem->avail / 1024
 			, size
 			, realsize, realsize / 1024
 			);
-#endif
 	do {
 		p = NULL;
 		if (mem->avail < realsize) {
-#ifdef ALLOC_DEBUG
-			fprintf(stderr, " oom\n");
-#endif
+			TRACE("%s", " oom");
 			break;
 		}
@@ -132,7 +129,5 @@
 
 		if (b == NULL) {
-#ifdef ALLOC_DEBUG
-			fprintf(stderr, " no fit chunk\n");
-#endif
+			TRACE("%s", " no fit chunk");
 			break;
 		}
@@ -149,7 +144,5 @@
 		if (cur->size == realsize) {
 			prev->next = cur->next;
-#ifdef ALLOC_DEBUG
-			fprintf(stderr, " perfect fit. Got: %p\n", p);
-#endif
+			TRACE(" perfect fit. Got: %p", p);
 			break;
 		}
@@ -169,6 +162,5 @@
 		 */
 
-#ifdef ALLOC_DEBUG
-		fprintf(stderr, " -> avail: %d (%dKB). new next: %p offset: %d %dKB. Got: %p\n"
+		TRACE(" -> avail: %d (%dKB). new next: %p offset: %d %dKB. Got: %p"
 				, mem->avail, mem->avail / 1024
 				, newb
@@ -176,5 +168,4 @@
 				, p
 				);
-#endif
 		prev->next = newb;
 		/* prev|cur|newb|next
@@ -193,8 +184,5 @@
 
 	cur = (xc_block_t *) (CHAR_PTR(p) - BLOCK_HEADER_SIZE());
-#ifdef ALLOC_DEBUG
-	fprintf(stderr, "freeing: %p", p);
-	fprintf(stderr, ", size=%d", cur->size);
-#endif
+	TRACE("freeing: %p, size=%d", p, cur->size);
 	xc_block_check(cur);
 	assert((char*)mem < (char*)cur && (char*)cur < (char*)mem + mem->size);
@@ -211,7 +199,5 @@
 	size = cur->size;
 
-#ifdef ALLOC_DEBUG
-	fprintf(stderr, ", avail %d (%dKB)", mem->avail, mem->avail / 1024);
-#endif
+	TRACE(" avail %d (%dKB)", mem->avail, mem->avail / 1024);
 	mem->avail += size;
 
@@ -221,7 +207,5 @@
 		b->next = cur->next;
 		cur = b;
-#ifdef ALLOC_DEBUG
-		fprintf(stderr, ", combine prev");
-#endif
+		TRACE("%s", " combine prev");
 	}
 
@@ -231,11 +215,7 @@
 		cur->size += b->size;
 		cur->next = b->next;
-#ifdef ALLOC_DEBUG
-		fprintf(stderr, ", combine next");
-#endif
-	}
-#ifdef ALLOC_DEBUG
-	fprintf(stderr, " -> avail %d (%dKB)\n", mem->avail, mem->avail / 1024);
-#endif
+		TRACE("%s", " combine next");
+	}
+	TRACE(" -> avail %d (%dKB)", mem->avail, mem->avail / 1024);
 	return size;
 }
Index: /branches/1.3/mmap.c
===================================================================
--- /branches/1.3/mmap.c	(revision 592)
+++ /branches/1.3/mmap.c	(revision 593)
@@ -1,4 +1,2 @@
-
-#undef ALLOC_DEBUG
 
 #include <stdio.h>
@@ -34,4 +32,5 @@
 #define XC_SHM_IMPL _xc_mmap_shm_t
 #include "xc_shm.h"
+#include "utils.h"
 
 #ifndef max
@@ -55,11 +54,4 @@
 };
 
-#undef NDEBUG
-#ifdef ALLOC_DEBUG
-#	define inline
-#else
-#	define NDEBUG
-#endif
-#include <assert.h>
 /* }}} */
 #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
Index: /branches/1.3/processor/main.m4
===================================================================
--- /branches/1.3/processor/main.m4	(revision 592)
+++ /branches/1.3/processor/main.m4	(revision 593)
@@ -39,5 +39,5 @@
 	IFSTORE(`
 		IFASSERT(`{
-			if (!xc_stack_size(&processor->allocsizes)) {
+			if (!xc_stack_count(&processor->allocsizes)) {
 				fprintf(stderr, "mismatch `$@' at line %d\n", __LINE__);
 			}
Index: /branches/1.3/stack.c
===================================================================
--- /branches/1.3/stack.c	(revision 592)
+++ /branches/1.3/stack.c	(revision 593)
@@ -4,8 +4,8 @@
 typedef xc_stack_t* S;
 
-void xc_stack_init(S stack)
+void xc_stack_init_ex(S stack, int initsize)
 {
 	stack->cnt = 0;
-	stack->size = 8;
+	stack->size = initsize;
 	stack->data = malloc(sizeof(void *) * stack->size);
 }
@@ -43,5 +43,5 @@
 }
 
-int xc_stack_size(S stack)
+int xc_stack_count(S stack)
 {
 	assert(stack != NULL);
Index: /branches/1.3/stack.h
===================================================================
--- /branches/1.3/stack.h	(revision 592)
+++ /branches/1.3/stack.h	(revision 593)
@@ -7,5 +7,6 @@
 
 #define S xc_stack_t*
-void xc_stack_init(S stack);
+void xc_stack_init_ex(S stack, int initsize);
+#define xc_stack_init(stack) xc_stack_init_ex(stack, 8)
 void xc_stack_destroy(S stack);
 void xc_stack_push(S stack, void *item);
@@ -13,5 +14,5 @@
 void *xc_stack_top(S stack);
 void *xc_stack_get(S stack, int n);
-int xc_stack_size(S stack);
+int xc_stack_count(S stack);
 void xc_stack_reverse(S stack);
 #undef S
Index: /branches/1.3/utils.c
===================================================================
--- /branches/1.3/utils.c	(revision 592)
+++ /branches/1.3/utils.c	(revision 593)
@@ -333,7 +333,5 @@
 	zend_op *opline;
 
-#ifdef DEBUG
-	fprintf(stderr, "binding %d\n", oplineno);
-#endif
+	TRACE("binding %d", oplineno);
 	assert(oplineno >= 0);
 
@@ -354,7 +352,5 @@
 
 			parent_name = &(opline - 1)->op2.u.constant;
-#	ifdef DEBUG
-			fprintf(stderr, "binding with parent %s\n", Z_STRVAL_P(parent_name));
-#	endif
+			TRACE("binding with parent %s", Z_STRVAL_P(parent_name));
 			if (zend_lookup_class(Z_STRVAL_P(parent_name), Z_STRLEN_P(parent_name), &pce TSRMLS_CC) == FAILURE) {
 				return FAILURE;
@@ -371,7 +367,5 @@
 			zend_op *fetch_class_opline = opline - 1;
 
-#	ifdef DEBUG
-			fprintf(stderr, "%s %p\n", Z_STRVAL(fetch_class_opline->op2.u.constant), Z_STRVAL(fetch_class_opline->op2.u.constant));
-#	endif
+			TRACE("%s %p", Z_STRVAL(fetch_class_opline->op2.u.constant), Z_STRVAL(fetch_class_opline->op2.u.constant));
 			OP_ZVAL_DTOR(fetch_class_opline->op2);
 			fetch_class_opline->opcode = ZEND_NOP;
@@ -736,2 +730,18 @@
 }
 /* }}} */
+int xc_vtrace(const char *fmt, va_list args) /* {{{ */
+{
+	vfprintf(stderr, fmt, args);
+	return 0;
+}
+/* }}} */
+int xc_trace(const char *fmt, ...) /* {{{ */
+{
+	va_list args;
+
+	va_start(args, fmt);
+	xc_vtrace(fmt, args);
+	va_end(args);
+	return 0;
+}
+/* }}} */
Index: /branches/1.3/utils.h
===================================================================
--- /branches/1.3/utils.h	(revision 592)
+++ /branches/1.3/utils.h	(revision 593)
@@ -1,3 +1,21 @@
 #include "php.h"
+#include "xcache.h"
+
+#ifdef DEBUG
+#	define IFDEBUG(x) (x)
+#	define TRACE(fmt, ...) \
+	xc_trace("%s:%d: " fmt "\r\n", __FILE__, __LINE__, __VA_ARGS__)
+int xc_trace(const char *fmt, ...) ZEND_ATTRIBUTE_PTR_FORMAT(printf, 1, 2);
+#   undef NDEBUG
+#   undef inline
+#   define inline
+#else
+#	define TRACE(fmt, ...) do { } while (0)
+#	define IFDEBUG(x) do { } while (0)
+#   ifndef NDEBUG
+#       define NDEBUG
+#   endif
+#endif
+#include <assert.h>
 
 typedef struct {
Index: /branches/1.3/xc_malloc.c
===================================================================
--- /branches/1.3/xc_malloc.c	(revision 592)
+++ /branches/1.3/xc_malloc.c	(revision 593)
@@ -11,4 +11,5 @@
 #include "php.h"
 #include "align.h"
+#include "utils.h"
 
 struct _xc_malloc_mem_t {
@@ -116,12 +117,4 @@
 	xc_shmsize_t       memoffset;
 };
-
-#undef NDEBUG
-#ifdef ALLOC_DEBUG
-#	define inline
-#else
-#	define NDEBUG
-#endif
-#include <assert.h>
 /* }}} */
 
Index: /branches/1.3/xc_shm.h
===================================================================
--- /branches/1.3/xc_shm.h	(revision 592)
+++ /branches/1.3/xc_shm.h	(revision 593)
@@ -1,2 +1,4 @@
+#ifndef XC_SHM_H
+#define XC_SHM_H
 typedef struct _xc_shm_handlers_t xc_shm_handlers_t;
 
@@ -66,2 +68,3 @@
 xc_shm_t *xc_shm_init(const char *type, xc_shmsize_t size, int readonly_protection, const void *arg1, const void *arg2);
 void xc_shm_destroy(xc_shm_t *shm);
+#endif
Index: /branches/1.3/xcache.c
===================================================================
--- /branches/1.3/xcache.c	(revision 592)
+++ /branches/1.3/xcache.c	(revision 593)
@@ -30,18 +30,7 @@
 #include "xcache_globals.h"
 #include "processor.h"
-#include "utils.h"
 #include "const_string.h"
 #include "opcode_spec.h"
-
-#ifdef DEBUG
-#	undef NDEBUG
-#	undef inline
-#	define inline
-#else
-#	ifndef NDEBUG
-#		define NDEBUG
-#	endif
-#endif
-#include <assert.h>
+#include "utils.h"
 
 #define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > pentry->ctime + (pentry)->ttl)
@@ -245,7 +234,5 @@
 static void xc_entry_hold_php_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
 {
-#ifdef DEBUG
-	fprintf(stderr, "hold %s\n", xce->name.str.val);
-#endif
+	TRACE("hold %s", xce->name.str.val);
 	xce->refcount ++;
 	xc_stack_push(&XG(php_holds)[xce->cache->cacheid], (void *)xce);
@@ -292,7 +279,5 @@
 static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_dmz) /* {{{ */
 {
-#ifdef DEBUG
-	fprintf(stderr, "ttl %lu, %lu %lu\n", (zend_ulong) XG(request_time), (zend_ulong) entry->atime, xc_php_ttl);
-#endif
+	TRACE("ttl %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) entry->atime, xc_php_ttl);
 	if (XG(request_time) > entry->atime + xc_php_ttl) {
 		return 1;
@@ -311,7 +296,5 @@
 static void xc_gc_expires_one(xc_cache_t *cache, zend_ulong gc_interval, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */
 {
-#ifdef DEBUG
-	fprintf(stderr, "interval %lu, %lu %lu\n", (zend_ulong) XG(request_time), (zend_ulong) cache->last_gc_expires, gc_interval);
-#endif
+	TRACE("interval %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) cache->last_gc_expires, gc_interval);
 	if (XG(request_time) - cache->last_gc_expires >= gc_interval) {
 		ENTER_LOCK(cache) {
@@ -630,15 +613,11 @@
 	for (i = 0; i < cachecount; i ++) {
 		s = &holds[i];
-#ifdef DEBUG
-		fprintf(stderr, "holded %d\n", xc_stack_size(s));
-#endif
-		if (xc_stack_size(s)) {
+		TRACE("holded %d", xc_stack_count(s));
+		if (xc_stack_count(s)) {
 			cache = ((xc_entry_t *)xc_stack_top(s))->cache;
 			ENTER_LOCK(cache) {
-				while (xc_stack_size(s)) {
+				while (xc_stack_count(s)) {
 					xce = (xc_entry_t*) xc_stack_pop(s);
-#ifdef DEBUG
-					fprintf(stderr, "unhold %s\n", xce->name.str.val);
-#endif
+					TRACE("unhold %s", xce->name.str.val);
 					xce->refcount --;
 					assert(xce->refcount >= 0);
@@ -829,7 +808,5 @@
 		assert(0);
 	}
-#ifdef DEBUG
-	fprintf(stderr, "got ZEND_DECLARE_INHERITED_CLASS: %s\n", class_name + 1);
-#endif
+	TRACE("got ZEND_DECLARE_INHERITED_CLASS: %s", class_name + 1);
 	/* let's see which class */
 	for (i = 0; i < php->classinfo_cnt; i ++) {
@@ -915,7 +892,5 @@
 		/* found */
 		if (stored_xce) {
-#ifdef DEBUG
-			fprintf(stderr, "found %s, catch it\n", stored_xce->name.str.val);
-#endif
+			TRACE("found %s, catch it", stored_xce->name.str.val);
 			xc_entry_hold_php_dmz(stored_xce TSRMLS_CC);
 			cache->hits ++;
@@ -944,7 +919,5 @@
 
 	/* {{{ compile */
-#ifdef DEBUG
-	fprintf(stderr, "compiling %s\n", filename);
-#endif
+	TRACE("compiling %s", filename);
 
 	/* make compile inside sandbox */
@@ -1125,7 +1098,5 @@
 	} LEAVE_LOCK_EX(cache);
 	/* }}} */
-#ifdef DEBUG
-	fprintf(stderr, "stored\n");
-#endif
+	TRACE("%s", "stored");
 
 #define X_FREE(var) \
@@ -1186,7 +1157,5 @@
 	CG(compiled_filename) = stored_xce->name.str.val;
 	CG(zend_lineno)       = 0;
-#ifdef DEBUG
-	fprintf(stderr, "restoring %s\n", stored_xce->name.str.val);
-#endif
+	TRACE("restoring %s", stored_xce->name.str.val);
 	xc_processor_restore_xc_entry_t(&xce, stored_xce, xc_readonly_protection TSRMLS_CC);
 #ifdef SHOW_DPRINT
@@ -1221,7 +1190,5 @@
 	CG(in_compilation)    = 0;
 	CG(compiled_filename) = NULL;
-#ifdef DEBUG
-	fprintf(stderr, "restored  %s\n", stored_xce->name.str.val);
-#endif
+	TRACE("restored  %s", stored_xce->name.str.val);
 	return op_array;
 }
@@ -2006,12 +1973,8 @@
 		stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC);
 		if (stored_xce) {
-#ifdef DEBUG
-			fprintf(stderr, "incdec: gotxce %s\n", xce.name.str.val);
-#endif
+			TRACE("incdec: gotxce %s", xce.name.str.val);
 			/* timeout */
 			if (VAR_ENTRY_EXPIRED(stored_xce)) {
-#ifdef DEBUG
-				fprintf(stderr, "incdec: expired\n");
-#endif
+				TRACE("%s", "incdec: expired");
 				xc_entry_remove_dmz(stored_xce TSRMLS_CC);
 				stored_xce = NULL;
@@ -2024,7 +1987,5 @@
 					stored_xce->ctime = XG(request_time);
 					stored_xce->ttl   = xce.ttl;
-#ifdef DEBUG
-					fprintf(stderr, "incdec: islong\n");
-#endif
+					TRACE("%s", "incdec: islong");
 					value = Z_LVAL_P(stored_var->value);
 					value += (inc == 1 ? count : - count);
@@ -2036,7 +1997,5 @@
 				}
 				else {
-#ifdef DEBUG
-					fprintf(stderr, "incdec: notlong\n");
-#endif
+					TRACE("%s", "incdec: notlong");
 					xc_processor_restore_zval(&oldzval, stored_xce->data.var->value, stored_xce->have_references TSRMLS_CC);
 					convert_to_long(&oldzval);
@@ -2046,9 +2005,7 @@
 			}
 		}
-#ifdef DEBUG
 		else {
-			fprintf(stderr, "incdec: %s not found\n", xce.name.str.val);
-		}
-#endif
+			TRACE("incdec: %s not found", xce.name.str.val);
+		}
 
 		value += (inc == 1 ? count : - count);
@@ -2495,7 +2452,5 @@
 
     zend_llist_prepend_element(&zend_extensions, &extension);
-#ifdef DEBUG
-	fprintf(stderr, "registered\n");
-#endif
+	TRACE("%s", "registered");
 }
 
