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-2005
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 
00028 /* default level is a bit verbose to be backward compatible */
00029 static char LogLevel = PCSC_LOG_ERROR;
00030 
00031 static signed char LogDoColor = 0;  /* no color by default */
00032 void log_init(void);
00033 
00034 void log_init(void)
00035 {
00036     char *e;
00037 
00038 #ifdef LIBPCSCLITE
00039     e = getenv("PCSCLITE_DEBUG");
00040 #else
00041     e = getenv("MUSCLECARD_DEBUG");
00042 #endif
00043     if (e)
00044         LogLevel = atoi(e);
00045 
00046     /* no color under Windows */
00047 #ifndef WIN32
00048     /* log to stderr and stderr is a tty? */
00049     if (isatty(fileno(stderr)))
00050     {
00051         const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
00052         char *term;
00053 
00054         term = getenv("TERM");
00055         if (term)
00056         {
00057             unsigned int i;
00058 
00059             /* for each known color terminal */
00060             for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
00061             {
00062                 /* we found a supported term? */
00063                 if (0 == strcmp(terms[i], term))
00064                 {
00065                     LogDoColor = 1;
00066                     break;
00067                 }
00068             }
00069         }
00070     }
00071 #endif
00072 } /* log_init */
00073 
00074 void log_msg(const int priority, const char *fmt, ...)
00075 {
00076     char DebugBuffer[DEBUG_BUF_SIZE];
00077     va_list argptr;
00078     static int is_initialized = 0;
00079 
00080     if (!is_initialized)
00081     {
00082         log_init();
00083         is_initialized = 1;
00084     }
00085 
00086     if (priority < LogLevel) /* log priority lower than threshold? */
00087         return;
00088 
00089     va_start(argptr, fmt);
00090 #ifndef WIN32
00091     vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
00092 #else
00093 #if HAVE_VSNPRINTF
00094     vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
00095 #else
00096     vsprintf(DebugBuffer, fmt, argptr);
00097 #endif
00098 #endif
00099     va_end(argptr);
00100 
00101 #ifndef WIN32
00102     {
00103         if (LogDoColor)
00104         {
00105             const char *color_pfx = "", *color_sfx = "\33[0m";
00106 
00107             switch (priority)
00108             {
00109                 case PCSC_LOG_CRITICAL:
00110                     color_pfx = "\33[01;31m"; /* bright + Red */
00111                     break;
00112 
00113                 case PCSC_LOG_ERROR:
00114                     color_pfx = "\33[35m"; /* Magenta */
00115                     break;
00116 
00117                 case PCSC_LOG_INFO:
00118                     color_pfx = "\33[34m"; /* Blue */
00119                     break;
00120 
00121                 case PCSC_LOG_DEBUG:
00122                     color_pfx = ""; /* normal (black) */
00123                     color_sfx = "";
00124                     break;
00125             }
00126             fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
00127         }
00128         else
00129             fprintf(stderr, "%s\n", DebugBuffer);
00130     }
00131 #else
00132     fprintf(stderr, "%s\n", DebugBuffer);
00133 #endif
00134 } /* log_msg */
00135 
00136 void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
00137     const int len)
00138 {
00139     char DebugBuffer[DEBUG_BUF_SIZE];
00140     int i;
00141     char *c;
00142     char *debug_buf_end;
00143 
00144     if (priority < LogLevel) /* log priority lower than threshold? */
00145         return;
00146 
00147     debug_buf_end = DebugBuffer + DEBUG_BUF_SIZE - 5;
00148 
00149     strlcpy(DebugBuffer, msg, sizeof(DebugBuffer));
00150     c = DebugBuffer + strlen(DebugBuffer);
00151 
00152     for (i = 0; (i < len) && (c < debug_buf_end); ++i)
00153     {
00154         sprintf(c, "%02X ", buffer[i]);
00155         c += strlen(c);
00156     }
00157 
00158     fprintf(stderr, "%s\n", DebugBuffer);
00159 } /* log_xxd */
00160 

Generated on Wed Mar 31 04:48:40 2010 for pcsc-lite by  doxygen 1.4.7