pcsc-lite
1.8.3
|
00001 /* 00002 * MUSCLE SmartCard Development ( http://www.linuxnet.com ) 00003 * 00004 * Copyright (C) 1999-2002 00005 * David Corcoran <corcoran@linuxnet.com> 00006 * Copyright (C) 2002-2011 00007 * Ludovic Rousseau <ludovic.rousseau@free.fr> 00008 * 00009 * $Id: debug.c 5898 2011-08-21 13:53:27Z rousseau $ 00010 */ 00011 00017 #include "config.h" 00018 #include <stdarg.h> 00019 #include <stdlib.h> 00020 #include <unistd.h> 00021 #include <string.h> 00022 #include <stdio.h> 00023 00024 #include "debuglog.h" 00025 #include "strlcpycat.h" 00026 00027 #define DEBUG_BUF_SIZE 2048 00028 00029 #ifdef NO_LOG 00030 00031 void log_msg(const int priority, const char *fmt, ...) 00032 { 00033 (void)priority; 00034 (void)fmt; 00035 } 00036 00037 #else 00038 00040 static char LogLevel = PCSC_LOG_CRITICAL+1; 00041 00042 static signed char LogDoColor = 0; 00044 static void log_init(void) 00045 { 00046 char *e; 00047 00048 #ifdef LIBPCSCLITE 00049 e = getenv("PCSCLITE_DEBUG"); 00050 #else 00051 e = getenv("MUSCLECARD_DEBUG"); 00052 #endif 00053 if (e) 00054 LogLevel = atoi(e); 00055 00056 /* log to stderr and stderr is a tty? */ 00057 if (isatty(fileno(stderr))) 00058 { 00059 const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" }; 00060 char *term; 00061 00062 term = getenv("TERM"); 00063 if (term) 00064 { 00065 unsigned int i; 00066 00067 /* for each known color terminal */ 00068 for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++) 00069 { 00070 /* we found a supported term? */ 00071 if (0 == strcmp(terms[i], term)) 00072 { 00073 LogDoColor = 1; 00074 break; 00075 } 00076 } 00077 } 00078 } 00079 } /* log_init */ 00080 00081 void log_msg(const int priority, const char *fmt, ...) 00082 { 00083 char DebugBuffer[DEBUG_BUF_SIZE]; 00084 va_list argptr; 00085 static int is_initialized = 0; 00086 00087 if (!is_initialized) 00088 { 00089 log_init(); 00090 is_initialized = 1; 00091 } 00092 00093 if (priority < LogLevel) /* log priority lower than threshold? */ 00094 return; 00095 00096 va_start(argptr, fmt); 00097 (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr); 00098 va_end(argptr); 00099 00100 { 00101 if (LogDoColor) 00102 { 00103 const char *color_pfx = "", *color_sfx = "\33[0m"; 00104 00105 switch (priority) 00106 { 00107 case PCSC_LOG_CRITICAL: 00108 color_pfx = "\33[01;31m"; /* bright + Red */ 00109 break; 00110 00111 case PCSC_LOG_ERROR: 00112 color_pfx = "\33[35m"; /* Magenta */ 00113 break; 00114 00115 case PCSC_LOG_INFO: 00116 color_pfx = "\33[34m"; /* Blue */ 00117 break; 00118 00119 case PCSC_LOG_DEBUG: 00120 color_pfx = ""; /* normal (black) */ 00121 color_sfx = ""; 00122 break; 00123 } 00124 fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx); 00125 } 00126 else 00127 fprintf(stderr, "%s\n", DebugBuffer); 00128 } 00129 } /* log_msg */ 00130 00131 #endif 00132