48 #include <sys/types.h>
60 void log_msg(
const int priority,
const char *fmt, ...)
66 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
75 void DebugLogSetLogType(
const int dbgtype)
80 void DebugLogSetLevel(
const int level)
85 INTERNAL
void DebugLogSetCategory(
const int dbginfo)
90 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
103 #define DEBUG_BUF_SIZE 2048
105 static char LogMsgType = DEBUGLOG_NO_DEBUG;
106 static char LogCategory = DEBUG_CATEGORY_NOTHING;
113 static pthread_mutex_t LastTimeMutex = PTHREAD_MUTEX_INITIALIZER;
115 static void log_line(
const int priority,
const char *DebugBuffer,
121 void log_msg_rv(
const int priority,
unsigned int rv,
const char *fmt, ...)
127 || (DEBUGLOG_NO_DEBUG == LogMsgType))
130 va_start(argptr, fmt);
131 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
134 log_line(priority, DebugBuffer, rv);
137 void log_msg(
const int priority,
const char *fmt, ...)
142 static int syslog_opened = 0;
145 || (DEBUGLOG_NO_DEBUG == LogMsgType))
148 va_start(argptr, fmt);
149 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
152 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType && !syslog_opened) {
153 openlog(NULL, LOG_PID, LOG_DAEMON);
157 log_line(priority, DebugBuffer, -1);
163 const char * rv2text(
unsigned int rv)
165 const char *rv_text = NULL;
166 static __thread
char strError[30];
170 rv_text = "rv=" #x; \
173 if (rv != (
unsigned int)-1)
202 (void)snprintf(strError,
sizeof(strError)-1,
203 "Unknown error: 0x%08X", rv);
211 static void log_line(
const int priority,
const char *DebugBuffer,
214 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
215 syslog(LOG_DEBUG,
"%s", DebugBuffer);
218 static struct timeval last_time = { 0, 0 };
219 struct timeval new_time = { 0, 0 };
223 const char *rv_text = NULL;
225 (void)pthread_mutex_lock(&LastTimeMutex);
226 gettimeofday(&new_time, NULL);
227 if (0 == last_time.tv_sec)
228 last_time = new_time;
230 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
231 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
235 tmp.tv_usec += 1000000;
237 if (tmp.tv_sec < 100)
238 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
242 last_time = new_time;
243 (void)pthread_mutex_unlock(&LastTimeMutex);
245 thread_id = pthread_self();
247 rv_text = rv2text(rv);
251 const char *color_pfx =
"", *color_sfx =
"\33[0m";
252 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
256 case PCSC_LOG_CRITICAL:
257 color_pfx =
"\33[01;31m";
261 color_pfx =
"\33[35m";
265 color_pfx =
"\33[34m";
275 #define THREAD_FORMAT "%p"
277 #define THREAD_FORMAT "%lu"
281 const char * rv_pfx =
"", * rv_sfx =
"";
288 printf(
"%s%.8d%s [" THREAD_FORMAT
"] %s%s%s, %s%s%s\n",
289 time_pfx, delta, time_sfx, thread_id,
290 color_pfx, DebugBuffer, color_sfx,
291 rv_pfx, rv_text, rv_sfx);
294 printf(
"%s%.8d%s [" THREAD_FORMAT
"] %s%s%s\n",
295 time_pfx, delta, time_sfx, thread_id,
296 color_pfx, DebugBuffer, color_sfx);
301 printf(
"%.8d %s, %s\n", delta, DebugBuffer, rv_text);
303 printf(
"%.8d %s\n", delta, DebugBuffer);
309 static void log_xxd_always(
const int priority,
const char *msg,
310 const unsigned char *buffer,
const int len)
312 char DebugBuffer[len*3 + strlen(msg) +1];
317 strcpy(DebugBuffer, msg);
318 c = DebugBuffer + strlen(DebugBuffer);
320 for (i = 0; (i < len); ++i)
323 snprintf(c, 4,
"%02X ", buffer[i]);
327 log_line(priority, DebugBuffer, -1);
330 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
334 || (DEBUGLOG_NO_DEBUG == LogMsgType))
341 log_xxd_always(priority, msg, buffer, len);
344 void DebugLogSetLogType(
const int dbgtype)
348 case DEBUGLOG_NO_DEBUG:
349 case DEBUGLOG_SYSLOG_DEBUG:
350 case DEBUGLOG_STDOUT_DEBUG:
351 case DEBUGLOG_STDOUT_COLOR_DEBUG:
352 LogMsgType = dbgtype;
355 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
357 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
361 if ((DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
362 || (DEBUGLOG_STDOUT_COLOR_DEBUG == LogMsgType))
366 term = getenv(
"TERM");
369 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
373 for (i = 0; i < COUNT_OF(terms); i++)
376 if (0 == strcmp(terms[i], term))
386 void DebugLogSetLevel(
const int level)
391 case PCSC_LOG_CRITICAL:
397 Log1(PCSC_LOG_INFO,
"debug level=info");
401 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
406 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=info",
411 INTERNAL
void DebugLogSetCategory(
const int dbginfo)
417 LogCategory &= dbginfo;
419 LogCategory |= dbginfo;
421 if (LogCategory & DEBUG_CATEGORY_APDU)
422 Log1(PCSC_LOG_INFO,
"Debug options: APDU");
425 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
428 if ((category & DEBUG_CATEGORY_APDU)
429 && (LogCategory & DEBUG_CATEGORY_APDU))
430 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
432 if ((category & DEBUG_CATEGORY_SW)
433 && (LogCategory & DEBUG_CATEGORY_APDU))
434 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
442 void debug_msg(
const char *fmt, ...);
443 void debug_msg(
const char *fmt, ...)
448 if (DEBUGLOG_NO_DEBUG == LogMsgType)
451 va_start(argptr, fmt);
452 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
455 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
456 syslog(LOG_INFO,
"%s", DebugBuffer);
461 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
462 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
464 log_xxd(PCSC_LOG_ERROR, msg, buffer, len);
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_E_TIMEOUT
The user-specified timeout value has expired.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
#define SCARD_E_INVALID_PARAMETER
One or more of the supplied parameters could not be properly interpreted.
#define SCARD_E_NO_READERS_AVAILABLE
Cannot find a smart card reader.
This handles abstract system level calls.
#define SCARD_E_NO_SMARTCARD
The operation requires a Smart Card, but no Smart Card is currently in the device.
#define SCARD_W_UNRESPONSIVE_CARD
The smart card is not responding to a reset.
#define SCARD_E_NOT_TRANSACTED
An attempt was made to end a non-existent transaction.
#define DEBUG_BUF_SIZE
Max string size dumping a maximum of 2 lines of 80 characters.
#define SCARD_E_PROTO_MISMATCH
The requested protocols are incompatible with the protocol currently in use with the smart card...
static char LogLevel
default level
#define SCARD_E_NO_MEMORY
Not enough memory available to complete this command.
#define SCARD_E_READER_UNAVAILABLE
The specified reader is not currently available for use.
#define SCARD_W_RESET_CARD
The smart card has been reset, so any shared state information is invalid.
This keeps a list of defines for pcsc-lite.
#define SCARD_F_COMM_ERROR
An internal communications error has been detected.
#define SCARD_W_REMOVED_CARD
The smart card has been removed, so further communication is not possible.
#define SCARD_E_INSUFFICIENT_BUFFER
The data buffer to receive returned data is too small for the returned data.
#define SCARD_E_CANCELLED
The action was cancelled by an SCardCancel request.
#define SCARD_E_SHARING_VIOLATION
The smart card cannot be accessed because of other connections outstanding.
#define SCARD_E_UNKNOWN_READER
The specified reader name is not recognized.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
#define SCARD_E_UNSUPPORTED_FEATURE
This smart card does not support the requested feature.
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
#define SCARD_W_UNPOWERED_CARD
Power has been removed from the smart card, so that further communication is not possible.
static signed char LogDoColor
no color by default