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

Generated on Fri Aug 17 19:23:08 2007 for pcsc-lite by  doxygen 1.4.7