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)
92 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
105 #define DEBUG_BUF_SIZE 2048 107 static char LogMsgType = DEBUGLOG_NO_DEBUG;
108 static char LogCategory = DEBUG_CATEGORY_NOTHING;
115 static void log_line(
const int priority,
const char *DebugBuffer);
117 void log_msg(
const int priority,
const char *fmt, ...)
123 || (DEBUGLOG_NO_DEBUG == LogMsgType))
126 va_start(argptr, fmt);
127 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
130 log_line(priority, DebugBuffer);
133 static void log_line(
const int priority,
const char *DebugBuffer)
135 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
136 syslog(LOG_INFO,
"%s", DebugBuffer);
139 static struct timeval last_time = { 0, 0 };
140 struct timeval new_time = { 0, 0 };
145 gettimeofday(&new_time, NULL);
146 if (0 == last_time.tv_sec)
147 last_time = new_time;
149 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
150 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
154 tmp.tv_usec += 1000000;
156 if (tmp.tv_sec < 100)
157 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
161 last_time = new_time;
163 thread_id = pthread_self();
167 const char *color_pfx =
"", *color_sfx =
"\33[0m";
168 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
172 case PCSC_LOG_CRITICAL:
173 color_pfx =
"\33[01;31m";
177 color_pfx =
"\33[35m";
181 color_pfx =
"\33[34m";
191 #define THREAD_FORMAT "%p" 193 #define THREAD_FORMAT "%lu" 195 printf(
"%s%.8d%s [" THREAD_FORMAT
"] %s%s%s\n",
196 time_pfx, delta, time_sfx, thread_id,
197 color_pfx, DebugBuffer, color_sfx);
201 printf(
"%.8d %s\n", delta, DebugBuffer);
207 static void log_xxd_always(
const int priority,
const char *msg,
208 const unsigned char *buffer,
const int len)
210 char DebugBuffer[len*3 + strlen(msg) +1];
215 strcpy(DebugBuffer, msg);
216 c = DebugBuffer + strlen(DebugBuffer);
218 for (i = 0; (i < len); ++i)
221 snprintf(c, 4,
"%02X ", buffer[i]);
225 log_line(priority, DebugBuffer);
228 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
232 || (DEBUGLOG_NO_DEBUG == LogMsgType))
239 log_xxd_always(priority, msg, buffer, len);
242 void DebugLogSetLogType(
const int dbgtype)
246 case DEBUGLOG_NO_DEBUG:
247 case DEBUGLOG_SYSLOG_DEBUG:
248 case DEBUGLOG_STDOUT_DEBUG:
249 case DEBUGLOG_STDOUT_COLOR_DEBUG:
250 LogMsgType = dbgtype;
253 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
255 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
259 if ((DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
260 || (DEBUGLOG_STDOUT_COLOR_DEBUG == LogMsgType))
264 term = getenv(
"TERM");
267 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
271 for (i = 0; i < COUNT_OF(terms); i++)
274 if (0 == strcmp(terms[i], term))
284 void DebugLogSetLevel(
const int level)
289 case PCSC_LOG_CRITICAL:
295 Log1(PCSC_LOG_INFO,
"debug level=info");
299 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
304 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=info",
309 INTERNAL
void DebugLogSetCategory(
const int dbginfo)
315 LogCategory &= dbginfo;
317 LogCategory |= dbginfo;
319 if (LogCategory & DEBUG_CATEGORY_APDU)
320 Log1(PCSC_LOG_INFO,
"Debug options: APDU");
323 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
326 if ((category & DEBUG_CATEGORY_APDU)
327 && (LogCategory & DEBUG_CATEGORY_APDU))
328 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
330 if ((category & DEBUG_CATEGORY_SW)
331 && (LogCategory & DEBUG_CATEGORY_APDU))
332 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
340 void debug_msg(
const char *fmt, ...);
341 void debug_msg(
const char *fmt, ...)
346 if (DEBUGLOG_NO_DEBUG == LogMsgType)
349 va_start(argptr, fmt);
350 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
353 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
354 syslog(LOG_INFO,
"%s", DebugBuffer);
359 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
360 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
362 log_xxd(PCSC_LOG_ERROR, msg, buffer, len);
This handles abstract system level calls.
#define DEBUG_BUF_SIZE
Max string size dumping a maxmium of 2 lines of 80 characters.
static char LogLevel
default level
This keeps a list of defines for pcsc-lite.
static signed char LogDoColor
no color by default