50 #include <sys/types.h>
61 void log_msg(
const int priority,
const char *fmt, ...)
67 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
76 void DebugLogSetLogType(
const int dbgtype)
81 void DebugLogSetLevel(
const int level)
86 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
93 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
106 #define DEBUG_BUF_SIZE 2048
108 static char LogMsgType = DEBUGLOG_NO_DEBUG;
109 static char LogCategory = DEBUG_CATEGORY_NOTHING;
116 static void log_line(
const int priority,
const char *DebugBuffer);
118 void log_msg(
const int priority,
const char *fmt, ...)
124 || (DEBUGLOG_NO_DEBUG == LogMsgType))
127 va_start(argptr, fmt);
128 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
131 log_line(priority, DebugBuffer);
134 static void log_line(
const int priority,
const char *DebugBuffer)
136 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
137 syslog(LOG_INFO,
"%s", DebugBuffer);
140 static struct timeval last_time = { 0, 0 };
141 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;
165 const char *color_pfx =
"", *color_sfx =
"\33[0m";
166 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
170 case PCSC_LOG_CRITICAL:
171 color_pfx =
"\33[01;31m";
175 color_pfx =
"\33[35m";
179 color_pfx =
"\33[34m";
188 printf(
"%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
189 color_pfx, DebugBuffer, color_sfx);
193 printf(
"%.8d %s\n", delta, DebugBuffer);
199 static void log_xxd_always(
const int priority,
const char *msg,
200 const unsigned char *buffer,
const int len)
202 char DebugBuffer[len*3 + strlen(msg) +1];
207 strcpy(DebugBuffer, msg);
208 c = DebugBuffer + strlen(DebugBuffer);
210 for (i = 0; (i < len); ++i)
213 snprintf(c, 4,
"%02X ", buffer[i]);
217 log_line(priority, DebugBuffer);
220 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
224 || (DEBUGLOG_NO_DEBUG == LogMsgType))
231 log_xxd_always(priority, msg, buffer, len);
234 void DebugLogSetLogType(
const int dbgtype)
238 case DEBUGLOG_NO_DEBUG:
239 case DEBUGLOG_SYSLOG_DEBUG:
240 case DEBUGLOG_STDOUT_DEBUG:
241 case DEBUGLOG_STDOUT_COLOR_DEBUG:
242 LogMsgType = dbgtype;
245 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
247 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
251 if ((DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
252 || (DEBUGLOG_STDOUT_COLOR_DEBUG == LogMsgType))
256 term = getenv(
"TERM");
259 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
263 for (i = 0; i < COUNT_OF(terms); i++)
266 if (0 == strcmp(terms[i], term))
276 void DebugLogSetLevel(
const int level)
281 case PCSC_LOG_CRITICAL:
287 Log1(PCSC_LOG_INFO,
"debug level=notice");
291 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
296 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=notice",
301 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
307 LogCategory &= dbginfo;
309 LogCategory |= dbginfo;
311 if (LogCategory & DEBUG_CATEGORY_APDU)
312 Log1(PCSC_LOG_INFO,
"Debug options: APDU");
317 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
320 if ((category & DEBUG_CATEGORY_APDU)
321 && (LogCategory & DEBUG_CATEGORY_APDU))
322 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
324 if ((category & DEBUG_CATEGORY_SW)
325 && (LogCategory & DEBUG_CATEGORY_APDU))
326 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
334 void debug_msg(
const char *fmt, ...);
335 void debug_msg(
const char *fmt, ...)
340 if (DEBUGLOG_NO_DEBUG == LogMsgType)
343 va_start(argptr, fmt);
344 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
347 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
348 syslog(LOG_INFO,
"%s", DebugBuffer);
353 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
354 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
356 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