48 #include <sys/types.h> 59 void log_msg(
const int priority,
const char *fmt, ...)
65 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
74 void DebugLogSetLogType(
const int dbgtype)
79 void DebugLogSetLevel(
const int level)
84 INTERNAL
void DebugLogSetCategory(
const int dbginfo)
91 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
104 #define DEBUG_BUF_SIZE 2048 106 static char LogMsgType = DEBUGLOG_NO_DEBUG;
107 static char LogCategory = DEBUG_CATEGORY_NOTHING;
114 static void log_line(
const int priority,
const char *DebugBuffer);
116 void log_msg(
const int priority,
const char *fmt, ...)
122 || (DEBUGLOG_NO_DEBUG == LogMsgType))
125 va_start(argptr, fmt);
126 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
129 log_line(priority, DebugBuffer);
132 static void log_line(
const int priority,
const char *DebugBuffer)
134 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
135 syslog(LOG_INFO,
"%s", DebugBuffer);
138 static struct timeval last_time = { 0, 0 };
139 struct timeval new_time = { 0, 0 };
143 gettimeofday(&new_time, NULL);
144 if (0 == last_time.tv_sec)
145 last_time = new_time;
147 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
148 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
152 tmp.tv_usec += 1000000;
154 if (tmp.tv_sec < 100)
155 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
159 last_time = new_time;
163 const char *color_pfx =
"", *color_sfx =
"\33[0m";
164 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
168 case PCSC_LOG_CRITICAL:
169 color_pfx =
"\33[01;31m";
173 color_pfx =
"\33[35m";
177 color_pfx =
"\33[34m";
186 printf(
"%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
187 color_pfx, DebugBuffer, color_sfx);
191 printf(
"%.8d %s\n", delta, DebugBuffer);
197 static void log_xxd_always(
const int priority,
const char *msg,
198 const unsigned char *buffer,
const int len)
200 char DebugBuffer[len*3 + strlen(msg) +1];
205 strcpy(DebugBuffer, msg);
206 c = DebugBuffer + strlen(DebugBuffer);
208 for (i = 0; (i < len); ++i)
211 snprintf(c, 4,
"%02X ", buffer[i]);
215 log_line(priority, DebugBuffer);
218 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
222 || (DEBUGLOG_NO_DEBUG == LogMsgType))
229 log_xxd_always(priority, msg, buffer, len);
232 void DebugLogSetLogType(
const int dbgtype)
236 case DEBUGLOG_NO_DEBUG:
237 case DEBUGLOG_SYSLOG_DEBUG:
238 case DEBUGLOG_STDOUT_DEBUG:
239 case DEBUGLOG_STDOUT_COLOR_DEBUG:
240 LogMsgType = dbgtype;
243 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
245 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
249 if ((DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
250 || (DEBUGLOG_STDOUT_COLOR_DEBUG == LogMsgType))
254 term = getenv(
"TERM");
257 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
261 for (i = 0; i < COUNT_OF(terms); i++)
264 if (0 == strcmp(terms[i], term))
274 void DebugLogSetLevel(
const int level)
279 case PCSC_LOG_CRITICAL:
285 Log1(PCSC_LOG_INFO,
"debug level=info");
289 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
294 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=info",
299 INTERNAL
void DebugLogSetCategory(
const int dbginfo)
305 LogCategory &= dbginfo;
307 LogCategory |= dbginfo;
309 if (LogCategory & DEBUG_CATEGORY_APDU)
310 Log1(PCSC_LOG_INFO,
"Debug options: APDU");
313 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
316 if ((category & DEBUG_CATEGORY_APDU)
317 && (LogCategory & DEBUG_CATEGORY_APDU))
318 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
320 if ((category & DEBUG_CATEGORY_SW)
321 && (LogCategory & DEBUG_CATEGORY_APDU))
322 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
330 void debug_msg(
const char *fmt, ...);
331 void debug_msg(
const char *fmt, ...)
336 if (DEBUGLOG_NO_DEBUG == LogMsgType)
339 va_start(argptr, fmt);
340 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
343 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
344 syslog(LOG_INFO,
"%s", DebugBuffer);
349 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
350 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
352 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