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