Index: /trunk/include/cf_debug.h
===================================================================
--- /trunk/include/cf_debug.h	(revision 24)
+++ /trunk/include/cf_debug.h	(revision 25)
@@ -19,10 +19,19 @@
 
 #ifdef _DEBUG
+# define CF_DEBUG_CREATE_CTX()										\
+	CF_Debug_CreateCtx ()
+
+# define CF_DEBUG_DESTROY_CTX(__ctx)								\
+	CF_Debug_DestroyCtx (__ctx)
+
 # define CF_UPDATE_CTX(__ctx)										\
 	CF_Debug_UpdateCtx (__ctx,__FILE__,__func__,__LINE__)
+
 # define CF_DEBUG_PRINT(__fp,__fmt,...)								\
 	CF_Debug_Print (__fp,__FILE__,__func__,__LINE__,__fmt,##__VA_ARGS__)
+
 # define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...)				\
 	CF_Debug_PrintBin (__fp,__FILE__,__func__,__LINE__,__bin,__len,__fmt,##__VA_ARGS__)
+
 # define CF_DEBUG_TRACE(__ctx,__fmt,...)							\
 	do {															\
@@ -30,4 +39,5 @@
 	CF_Debug_Trace (__ctx,__fmt,##__VA_ARGS__);						\
 	} while (0)
+
 # define CF_DEBUG_TRACE_BIN(__ctx,__bin,__len,__fmt,...)			\
 	do {															\
@@ -35,14 +45,20 @@
 	CF_Debug_TraceBin (__ctx,__bin,__len,__fmt,##__VA_ARGS__);		\
 	} while (0)
+
 # define CF_DEBUG_CALLSTACK_PUSH(__ctx)								\
 	CF_Debug_CallStackPush (__ctx,__FILE__,__func__,__LINE__)
+
 # define CF_DEBUG_CALLSTACK_POP										\
 	CF_Debug_CallStackPop
 #else // #ifdef _DEBUG
+# define CF_DEBUG_CREATE_CTX()							NULL
+# define CF_DEBUG_DESTROY_CTX(__ctx)
 # define CF_UPDATE_CTX(__ctx)
+# define CF_DEBUG_PRINT(__fp,__fmt,...)
+# define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...)
 # define CF_DEBUG_TRACE(__ctx,__fmt,...)
 # define CF_DEBUG_TRACE_BIN(__ctx,__bin,__len,__fmt,...)
 # define CF_DEBUG_CALLSTACK_PUSH(__ctx)
-# define CF_DEBUG_CALLSTACK_POP()
+# define CF_DEBUG_CALLSTACK_POP(__ctx,__callstack)		1
 #endif // #ifdef _DEBUG
 
Index: /trunk/src/cf_log.c
===================================================================
--- /trunk/src/cf_log.c	(revision 24)
+++ /trunk/src/cf_log.c	(revision 25)
@@ -24,13 +24,12 @@
 #endif // #ifdef _WIN32
 
-#define CHECK_INVALID_CTX(__ctx)				\
-	if (__ctx == NULL)							\
-		return CF_ERROR_LOG_INVALID_CTX
-
-#define LOCK_LOG_CTX(__ctx)						\
-	CF_Mutex_Lock (&__ctx->mutex)
-
-#define UNLOCK_LOG_CTX(__ctx)					\
-	CF_Mutex_Unlock (&__ctx->mutex)
+#define CHECK_INVALID_CTX(__ctx)		(__ctx == NULL)
+#define LOCK_LOG_CTX(__ctx)				CF_Mutex_Lock (&__ctx->mutex)
+#define UNLOCK_LOG_CTX(__ctx)			CF_Mutex_Unlock (&__ctx->mutex)
+#define CHECK_INITIALIZED()				(gLogEnvironment.ctxPool == NULL||	\
+										 gLogEnvironment.ctxSize <= 0	)
+#define CHECK_INVALID_MAPID(__mapid)	(gLogEnvironment.ctxSize <= __mapid)
+#define CHECK_MAPPED_ID(__mapid)		(gLogEnvironment.ctxPool[__mapid] != NULL)	\
+
 
 #define CF_LOG_BUFFER_DEFAULT_SIZE		128 * 1024
@@ -271,5 +270,6 @@
 	CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
 
-	CHECK_INVALID_CTX (ctx);
+	if (CHECK_INVALID_CTX (ctx))
+		return CF_ERROR_LOG_INVALID_CTX;
 
 	memset (context->path, 0x00, sizeof (context->path));
@@ -301,5 +301,6 @@
 	CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
 
-	CHECK_INVALID_CTX (ctx);
+	if (CHECK_INVALID_CTX (ctx))
+		return CF_ERROR_LOG_INVALID_CTX;
 
 	if (CF_Mutex_Create (&context->mutex) < 0)
@@ -314,5 +315,6 @@
 	CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
 
-	CHECK_INVALID_CTX (ctx);
+	if (CHECK_INVALID_CTX (ctx))
+		return CF_ERROR_LOG_INVALID_CTX;
 
 	if (CF_Mutex_Destory (&context->mutex) < 0)
@@ -332,5 +334,6 @@
 	size_t		length = 0;
 
-	CHECK_INVALID_CTX (ctx);
+	if (CHECK_INVALID_CTX (ctx))
+		return CF_ERROR_LOG_INVALID_CTX;
 
 	LOCK_LOG_CTX (context);
@@ -375,5 +378,6 @@
 	CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
 
-	CHECK_INVALID_CTX (ctx);
+	if (CHECK_INVALID_CTX (ctx))
+		return CF_ERROR_LOG_INVALID_CTX;
 
 	LOCK_LOG_CTX (context);
@@ -388,22 +392,43 @@
 				 const CF_Log_Ctx	ctx)
 {
-	if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0)
+	int result = 0;
+
+	TRY
+	{
+		if (CHECK_INITIALIZED())
+		{
+			result = CF_ERROR_LOG_NOT_INITIALIZE;
+			TRY_BREAK;
+		}
+		if (CHECK_INVALID_MAPID(mapid))
+		{
+			result = CF_ERROR_LOG_INVALID_MAPID;
+			TRY_BREAK;
+		}
+		if (CHECK_MAPPED_ID(mapid))
+		{
+			result = CF_ERROR_LOG_ALREADY_MAPPED_ID;
+			TRY_BREAK;
+		}
+	}
+	CATCH_IF (result < 0)
+	{
+		CF_Log_DestroyCtx (ctx);
+		return result;
+	}
+
+	gLogEnvironment.ctxPool[mapid] = ctx;
+
+	return CF_OK;
+}
+
+int
+CF_Log_UnmapCtxID (const int mapid)
+{
+	if (CHECK_INITIALIZED())
 		return CF_ERROR_LOG_NOT_INITIALIZE;
-
-	if (gLogEnvironment.ctxPool[mapid] != NULL)
-		return CF_ERROR_LOG_ALREADY_MAPPED_ID;
-
-	gLogEnvironment.ctxPool[mapid] = ctx;
-
-	return CF_OK;
-}
-
-int
-CF_Log_UnmapCtxID (const int mapid)
-{
-	if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0)
-		return CF_ERROR_LOG_NOT_INITIALIZE;
-
-	if (gLogEnvironment.ctxPool[mapid] == NULL)
+	if (CHECK_INVALID_MAPID(mapid))
+		return CF_ERROR_LOG_INVALID_MAPID;
+	if (!CHECK_MAPPED_ID(mapid))
 		return CF_ERROR_LOG_NOT_MAPPED_ID;
 
@@ -419,6 +444,10 @@
 CF_Log_GetMappedCtx (const int mapid)
 {
-	if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0)
+	if (CHECK_INITIALIZED())
 		return NULL;
+	if (CHECK_INVALID_MAPID(mapid))
+		return NULL;
+	if (!CHECK_MAPPED_ID(mapid))
+		return NULL;
 
 	return gLogEnvironment.ctxPool[mapid];
Index: /trunk/test/test.c
===================================================================
--- /trunk/test/test.c	(revision 24)
+++ /trunk/test/test.c	(revision 25)
@@ -3,9 +3,14 @@
  */
 #include "cf_file.h"
-#include "cf_debug.h"
 #include "cf_log.h"
 #include "cf_socket.h"
 #include "cf_thread.h"
 
+/*
+ * before including "cf_debug.h",
+ * if _DEBUG is defined, the debugging code will be act.
+ */
+#include "cf_debug.h"
+
 #include <stdlib.h>
 #include <string.h>
@@ -30,5 +35,5 @@
 
 	/* initialize */
-	CF_Log_Initialize (10);
+	CF_Log_Initialize (1);
 
 	/* context {{{ */
