59void log_msg(
const int priority,
const char *fmt, ...)
65void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
74void DebugLogSetLogType(
const int dbgtype)
79void DebugLogSetLevel(
const int level)
84INTERNAL
void DebugLogSetCategory(
const int dbginfo)
89INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
102#define DEBUG_BUF_SIZE 2048
104static char LogMsgType = DEBUGLOG_NO_DEBUG;
105static char LogCategory = DEBUG_CATEGORY_NOTHING;
112static pthread_mutex_t LastTimeMutex = PTHREAD_MUTEX_INITIALIZER;
114static void log_line(
const int priority,
const char *DebugBuffer,
120void log_msg_rv(
const int priority,
unsigned int rv,
const char *fmt, ...)
122 char DebugBuffer[DEBUG_BUF_SIZE];
126 || (DEBUGLOG_NO_DEBUG == LogMsgType))
129 va_start(argptr, fmt);
130 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
133 log_line(priority, DebugBuffer, rv);
136void log_msg(
const int priority,
const char *fmt, ...)
138 char DebugBuffer[DEBUG_BUF_SIZE];
142 || (DEBUGLOG_NO_DEBUG == LogMsgType))
145 va_start(argptr, fmt);
146 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
149 log_line(priority, DebugBuffer, -1);
155const char * rv2text(
unsigned int rv)
157 const char *rv_text = NULL;
158 static __thread
char strError[30];
162 rv_text = "rv=" #x; \
165 if (rv != (
unsigned int)-1)
195 (void)snprintf(strError,
sizeof(strError)-1,
196 "Unknown error: 0x%08X", rv);
204static void log_line(
const int priority,
const char *DebugBuffer,
207 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
208 syslog(LOG_INFO,
"%s", DebugBuffer);
211 static struct timeval last_time = { 0, 0 };
212 struct timeval new_time = { 0, 0 };
216 const char *rv_text = NULL;
218 (void)pthread_mutex_lock(&LastTimeMutex);
219 gettimeofday(&new_time, NULL);
220 if (0 == last_time.tv_sec)
221 last_time = new_time;
223 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
224 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
228 tmp.tv_usec += 1000000;
230 if (tmp.tv_sec < 100)
231 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
235 last_time = new_time;
236 (void)pthread_mutex_unlock(&LastTimeMutex);
238 thread_id = pthread_self();
240 rv_text = rv2text(rv);
244 const char *color_pfx =
"", *color_sfx =
"\33[0m";
245 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
249 case PCSC_LOG_CRITICAL:
250 color_pfx =
"\33[01;31m";
254 color_pfx =
"\33[35m";
258 color_pfx =
"\33[34m";
268#define THREAD_FORMAT "%lu"
270#define THREAD_FORMAT "%p"
274 const char * rv_pfx =
"", * rv_sfx =
"";
281 printf(
"%s%.8d%s [" THREAD_FORMAT
"] %s%s%s, %s%s%s\n",
282 time_pfx, delta, time_sfx, thread_id,
283 color_pfx, DebugBuffer, color_sfx,
284 rv_pfx, rv_text, rv_sfx);
287 printf(
"%s%.8d%s [" THREAD_FORMAT
"] %s%s%s\n",
288 time_pfx, delta, time_sfx, thread_id,
289 color_pfx, DebugBuffer, color_sfx);
294 printf(
"%.8d %s, %s\n", delta, DebugBuffer, rv_text);
296 printf(
"%.8d %s\n", delta, DebugBuffer);
302static void log_xxd_always(
const int priority,
const char *msg,
303 const unsigned char *buffer,
const int len)
305 char DebugBuffer[len*3 + strlen(msg) +1];
310 strcpy(DebugBuffer, msg);
311 c = DebugBuffer + strlen(DebugBuffer);
313 for (i = 0; (i < len); ++i)
316 snprintf(c, 4,
"%02X ", buffer[i]);
320 log_line(priority, DebugBuffer, -1);
323void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
327 || (DEBUGLOG_NO_DEBUG == LogMsgType))
334 log_xxd_always(priority, msg, buffer, len);
337void DebugLogSetLogType(
const int dbgtype)
341 case DEBUGLOG_NO_DEBUG:
342 case DEBUGLOG_SYSLOG_DEBUG:
343 case DEBUGLOG_STDOUT_DEBUG:
344 LogMsgType = dbgtype;
346 case DEBUGLOG_STDOUT_COLOR_DEBUG:
347 LogMsgType = dbgtype;
351 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
353 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
357 if (DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
364 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
368 for (i = 0; i < COUNT_OF(terms); i++)
371 if (0 == strcmp(terms[i], term))
381void DebugLogSetLevel(
const int level)
386 case PCSC_LOG_CRITICAL:
392 Log1(PCSC_LOG_INFO,
"debug level=info");
396 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
401 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=info",
406INTERNAL
void DebugLogSetCategory(
const int dbginfo)
412 LogCategory &= dbginfo;
414 LogCategory |= dbginfo;
416 if (LogCategory & DEBUG_CATEGORY_APDU)
417 Log1(PCSC_LOG_INFO,
"Debug options: APDU");
420INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
423 if ((category & DEBUG_CATEGORY_APDU)
424 && (LogCategory & DEBUG_CATEGORY_APDU))
425 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
427 if ((category & DEBUG_CATEGORY_SW)
428 && (LogCategory & DEBUG_CATEGORY_APDU))
429 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
437void debug_msg(
const char *fmt, ...);
438void debug_msg(
const char *fmt, ...)
440 char DebugBuffer[DEBUG_BUF_SIZE];
443 if (DEBUGLOG_NO_DEBUG == LogMsgType)
446 va_start(argptr, fmt);
447 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
450 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
451 syslog(LOG_INFO,
"%s", DebugBuffer);
456void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
457void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
459 log_xxd(PCSC_LOG_ERROR, msg, buffer, len);
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
static signed char LogDoColor
no color by default
#define SCARD_E_INVALID_HANDLE
The supplied handle was invalid.
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
#define SCARD_E_UNKNOWN_READER
The specified reader name is not recognized.
#define SCARD_W_RESET_CARD
The smart card has been reset, so any shared state information is invalid.
#define SCARD_E_SERVICE_STOPPED
The Smart card resource manager has shut down.
#define SCARD_W_UNRESPONSIVE_CARD
The smart card is not responding to a reset.
#define SCARD_E_PROTO_MISMATCH
The requested protocols are incompatible with the protocol currently in use with the smart card.
#define SCARD_E_INVALID_PARAMETER
One or more of the supplied parameters could not be properly interpreted.
#define SCARD_E_CANCELLED
The action was cancelled by an SCardCancel request.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_E_NO_MEMORY
Not enough memory available to complete this command.
#define SCARD_E_NO_READERS_AVAILABLE
Cannot find a smart card reader.
#define SCARD_E_SHARING_VIOLATION
The smart card cannot be accessed because of other connections outstanding.
#define SCARD_F_COMM_ERROR
An internal communications error has been detected.
#define SCARD_E_INVALID_VALUE
One or more of the supplied parameters values could not be properly interpreted.
#define SCARD_E_TIMEOUT
The user-specified timeout value has expired.
#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_NO_SMARTCARD
The operation requires a Smart Card, but no Smart Card is currently in the device.
#define SCARD_E_NOT_TRANSACTED
An attempt was made to end a non-existent transaction.
#define SCARD_E_NO_SERVICE
The Smart card resource manager is not running.
#define SCARD_E_READER_UNAVAILABLE
The specified reader is not currently available for use.
#define SCARD_W_UNPOWERED_CARD
Power has been removed from the smart card, so that further communication is not possible.
#define SCARD_E_UNSUPPORTED_FEATURE
This smart card does not support the requested feature.
This keeps a list of defines for pcsc-lite.
This handles abstract system level calls.
const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)