[Pcsclite-cvs-commit] CVS PCSC/src

CVS User rousseau ludovic.rousseau@free.fr
Tue, 22 Feb 2005 07:21:58 -0700


Update of /cvsroot/pcsclite/PCSC/src
In directory haydn:/tmp/cvs-serv13853

Modified Files:
	debuglog.c 
Log Message:
implement log with priority level.

debug_msg() is still provided for backward object code compatibility


--- /cvsroot/pcsclite/PCSC/src/debuglog.c	2004/10/20 20:49:15	1.27
+++ /cvsroot/pcsclite/PCSC/src/debuglog.c	2005/02/22 14:21:58	1.28
@@ -7,7 +7,7 @@
  *  David Corcoran <corcoran@linuxnet.com>
  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
  *
- * $Id: debuglog.c,v 1.27 2004/10/20 20:49:15 rousseau Exp $
+ * $Id: debuglog.c,v 1.28 2005/02/22 14:21:58 rousseau Exp $
  */
 
 #include "config.h"
@@ -35,7 +35,10 @@
 static int debug_msg_type = DEBUGLOG_NO_DEBUG;
 static int debug_category = DEBUG_CATEGORY_NOTHING;
 
-void debug_msg(const char *fmt, ...)
+/* default level is a bit verbose to be backward compatible */
+static int log_level = PCSC_LOG_INFO;
+
+void log_msg(int priority, const char *fmt, ...)
 {
 	char DebugBuffer[DEBUG_BUF_SIZE];
 	va_list argptr;
@@ -43,6 +46,10 @@
 	if (lSuppress != DEBUGLOG_LOG_ENTRIES)
 		return;
 
+	/* log priority lower than threshold? */
+	if (priority < log_level)
+		return;
+
 	va_start(argptr, fmt);
 #ifndef WIN32
 	vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
@@ -82,9 +89,9 @@
 			/* Unknown type. Do nothing. */
 			assert(0);
 	}
-}	/* debug_msg */
+} /* log_msg */
 
-void debug_xxd(const char *msg, const unsigned char *buffer, const int len)
+void log_xxd(int priority, const char *msg, const unsigned char *buffer, const int len)
 {
 	char DebugBuffer[DEBUG_BUF_SIZE];
 	int i;
@@ -94,6 +101,10 @@
 	if (lSuppress != DEBUGLOG_LOG_ENTRIES)
 		return;
 
+	/* log priority lower than threshold? */
+	if (priority <= log_level)
+		return;
+
 	debug_buf_end = DebugBuffer + DEBUG_BUF_SIZE - 5;
 
 	strlcpy(DebugBuffer, msg, sizeof(DebugBuffer));
@@ -132,7 +143,7 @@
 			/* Unknown type - do nothing */
 			assert(0);
 	}
-}	/* debug_xxd */
+} /* log_xxd */
 
 void DebugLogSuppress(const int lSType)
 {
@@ -144,6 +155,33 @@
 	debug_msg_type = dbgtype;
 }
 
+void DebugLogSetLevel(const int level)
+{
+	log_level = level;
+	switch (level)
+	{
+		case PCSC_LOG_CRITICAL:
+			Log1(PCSC_LOG_CRITICAL, "debug level=critical");
+			break;
+
+		case PCSC_LOG_ERROR:
+			Log1(PCSC_LOG_CRITICAL, "debug level=error");
+			break;
+
+		case PCSC_LOG_INFO:
+			Log1(PCSC_LOG_CRITICAL, "debug level=notice");
+			break;
+
+		case PCSC_LOG_DEBUG:
+			Log1(PCSC_LOG_CRITICAL, "debug level=debug");
+			break;
+
+		default:
+			log_level = PCSC_LOG_INFO;
+			Log1(PCSC_LOG_CRITICAL, "unknown level, using level=notice");
+	}
+}
+
 int DebugLogSetCategory(const int dbginfo)
 {
 #define DEBUG_INFO_LENGTH 80
@@ -163,7 +201,7 @@
 	if (debug_category & DEBUG_CATEGORY_APDU)
 		strlcat(text, " APDU", sizeof(text));
 
-	DebugLogB("Debug options:%s", text);
+	Log2(PCSC_LOG_INFO, "Debug options:%s", text);
 
 	return debug_category;
 }
@@ -173,11 +211,11 @@
 {
 	if ((category & DEBUG_CATEGORY_APDU)
 		&& (debug_category & DEBUG_CATEGORY_APDU))
-		debug_xxd("APDU: ", (const unsigned char *)buffer, len);
+		log_xxd(PCSC_LOG_INFO, "APDU: ", (const unsigned char *)buffer, len);
 
 	if ((category & DEBUG_CATEGORY_SW)
 		&& (debug_category & DEBUG_CATEGORY_APDU))
-		debug_xxd("SW: ", (const unsigned char *)buffer, len);
+		log_xxd(PCSC_LOG_INFO, "SW: ", (const unsigned char *)buffer, len);
 }
 
 char* pcsc_stringify_error(long pcscError)
@@ -308,3 +346,60 @@
 	return strError;
 }
 
+/*
+ * old function supported for backward object code compatibility
+ */
+void debug_msg(const char *fmt, ...)
+{
+	char DebugBuffer[DEBUG_BUF_SIZE];
+	va_list argptr;
+
+	if (lSuppress != DEBUGLOG_LOG_ENTRIES)
+		return;
+
+	va_start(argptr, fmt);
+#ifndef WIN32
+	vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
+#else
+#if HAVE_VSNPRINTF
+	vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
+#else
+	vsprintf(DebugBuffer, fmt, argptr);
+#endif
+#endif
+	va_end(argptr);
+
+	switch(debug_msg_type) {
+		case DEBUGLOG_NO_DEBUG:
+		/*
+		 * Do nothing, it hasn't been set 
+		 */
+		break;
+
+		case DEBUGLOG_SYSLOG_DEBUG:
+#ifndef WIN32
+			syslog(LOG_INFO, "%s", DebugBuffer);
+#else
+			fprintf(stderr, "%s\n", DebugBuffer);
+#endif
+			break;
+
+		case DEBUGLOG_STDERR_DEBUG:
+			fprintf(stderr, "%s\n", DebugBuffer);
+			break;
+
+		case DEBUGLOG_STDOUT_DEBUG:
+			fprintf(stdout, "%s\n", DebugBuffer);
+			break;
+
+		default:
+			/* Unknown type. Do nothing. */
+			assert(0);
+	}
+} /* debug_msg */
+
+void debug_xxd(const char *msg, const unsigned char *buffer, const int len)
+{
+	log_xxd(PCSC_LOG_ERROR, msg, buffer, len);
+} /* debug_xxd */
+