debug.c

Go to the documentation of this file.
00001 /*
00002  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00003  *
00004  * Copyright (C) 1999-2002
00005  *  David Corcoran <corcoran@linuxnet.com>
00006  * Copyright (C) 1999-2008
00007  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
00008  *
00009  * $Id: debuglog.c 1953 2006-03-21 13:46:28Z rousseau $
00010  */
00011 
00017 #include "config.h"
00018 #include <stdarg.h>
00019 #include <stdlib.h>
00020 #include <unistd.h>
00021 #include <string.h>
00022 
00023 #include "debug.h"
00024 #include "strlcpycat.h"
00025 
00026 #define DEBUG_BUF_SIZE 2048
00027 
00029 static char LogLevel = PCSC_LOG_CRITICAL+1;
00030 
00031 static signed char LogDoColor = 0;  
00033 static void log_init(void)
00034 {
00035     char *e;
00036 
00037 #ifdef LIBPCSCLITE
00038     e = getenv("PCSCLITE_DEBUG");
00039 #else
00040     e = getenv("MUSCLECARD_DEBUG");
00041 #endif
00042     if (e)
00043         LogLevel = atoi(e);
00044 
00045     /* no color under Windows */
00046 #ifndef WIN32
00047     /* log to stderr and stderr is a tty? */
00048     if (isatty(fileno(stderr)))
00049     {
00050         const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
00051         char *term;
00052 
00053         term = getenv("TERM");
00054         if (term)
00055         {
00056             unsigned int i;
00057 
00058             /* for each known color terminal */
00059             for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
00060             {
00061                 /* we found a supported term? */
00062                 if (0 == strcmp(terms[i], term))
00063                 {
00064                     LogDoColor = 1;
00065                     break;
00066                 }
00067             }
00068         }
00069     }
00070 #endif
00071 } /* log_init */
00072 
00073 void log_msg(const int priority, const char *fmt, ...)
00074 {
00075     char DebugBuffer[DEBUG_BUF_SIZE];
00076     va_list argptr;
00077     static int is_initialized = 0;
00078 
00079     if (!is_initialized)
00080     {
00081         log_init();
00082         is_initialized = 1;
00083     }
00084 
00085     if (priority < LogLevel) /* log priority lower than threshold? */
00086         return;
00087 
00088     va_start(argptr, fmt);
00089 #ifndef WIN32
00090     (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
00091 #else
00092 #if HAVE_VSNPRINTF
00093     vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
00094 #else
00095     vsprintf(DebugBuffer, fmt, argptr);
00096 #endif
00097 #endif
00098     va_end(argptr);
00099 
00100 #ifndef WIN32
00101     {
00102         if (LogDoColor)
00103         {
00104             const char *color_pfx = "", *color_sfx = "\33[0m";
00105 
00106             switch (priority)
00107             {
00108                 case PCSC_LOG_CRITICAL:
00109                     color_pfx = "\33[01;31m"; /* bright + Red */
00110                     break;
00111 
00112                 case PCSC_LOG_ERROR:
00113                     color_pfx = "\33[35m"; /* Magenta */
00114                     break;
00115 
00116                 case PCSC_LOG_INFO:
00117                     color_pfx = "\33[34m"; /* Blue */
00118                     break;
00119 
00120                 case PCSC_LOG_DEBUG:
00121                     color_pfx = ""; /* normal (black) */
00122                     color_sfx = "";
00123                     break;
00124             }
00125             fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
00126         }
00127         else
00128             fprintf(stderr, "%s\n", DebugBuffer);
00129     }
00130 #else
00131     fprintf(stderr, "%s\n", DebugBuffer);
00132 #endif
00133 } /* log_msg */
00134 
00135 void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
00136     const int len)
00137 {
00138     char DebugBuffer[DEBUG_BUF_SIZE];
00139     int i;
00140     char *c;
00141     char *debug_buf_end;
00142 
00143     if (priority < LogLevel) /* log priority lower than threshold? */
00144         return;
00145 
00146     debug_buf_end = DebugBuffer + DEBUG_BUF_SIZE - 5;
00147 
00148     (void)strlcpy(DebugBuffer, msg, sizeof(DebugBuffer));
00149     c = DebugBuffer + strlen(DebugBuffer);
00150 
00151     for (i = 0; (i < len) && (c < debug_buf_end); ++i)
00152     {
00153         sprintf(c, "%02X ", buffer[i]);
00154         c += strlen(c);
00155     }
00156 
00157     fprintf(stderr, "%s\n", DebugBuffer);
00158 } /* log_xxd */
00159 

Generated on Mon Aug 17 01:00:08 2009 for pcsc-lite by  doxygen 1.5.9