50 #include <sys/types.h>
62 void log_msg(
const int priority,
const char *fmt, ...)
68 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
77 void DebugLogSetLogType(
const int dbgtype)
82 void DebugLogSetLevel(
const int level)
87 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
94 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
107 #define DEBUG_BUF_SIZE 2048
109 static char LogMsgType = DEBUGLOG_NO_DEBUG;
110 static char LogCategory = DEBUG_CATEGORY_NOTHING;
117 static void log_line(
const int priority,
const char *DebugBuffer);
119 void log_msg(
const int priority,
const char *fmt, ...)
125 || (DEBUGLOG_NO_DEBUG == LogMsgType))
128 va_start(argptr, fmt);
129 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
132 log_line(priority, DebugBuffer);
135 static void log_line(
const int priority,
const char *DebugBuffer)
137 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
138 syslog(LOG_INFO,
"%s", DebugBuffer);
141 static struct timeval last_time = { 0, 0 };
142 struct timeval new_time = { 0, 0 };
146 gettimeofday(&new_time, NULL);
147 if (0 == last_time.tv_sec)
148 last_time = new_time;
150 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
151 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
155 tmp.tv_usec += 1000000;
157 if (tmp.tv_sec < 100)
158 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
162 last_time = new_time;
166 const char *color_pfx =
"", *color_sfx =
"\33[0m";
167 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
171 case PCSC_LOG_CRITICAL:
172 color_pfx =
"\33[01;31m";
176 color_pfx =
"\33[35m";
180 color_pfx =
"\33[34m";
189 printf(
"%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
190 color_pfx, DebugBuffer, color_sfx);
194 printf(
"%.8d %s\n", delta, DebugBuffer);
200 static void log_xxd_always(
const int priority,
const char *msg,
201 const unsigned char *buffer,
const int len)
203 char DebugBuffer[len*3 + strlen(msg) +1];
208 l = strlcpy(DebugBuffer, msg,
sizeof(DebugBuffer));
211 for (i = 0; (i < len); ++i)
214 snprintf(c, 4,
"%02X ", buffer[i]);
218 log_line(priority, DebugBuffer);
221 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
225 || (DEBUGLOG_NO_DEBUG == LogMsgType))
232 log_xxd_always(priority, msg, buffer, len);
235 void DebugLogSetLogType(
const int dbgtype)
239 case DEBUGLOG_NO_DEBUG:
240 case DEBUGLOG_SYSLOG_DEBUG:
241 case DEBUGLOG_STDOUT_DEBUG:
242 case DEBUGLOG_STDOUT_COLOR_DEBUG:
243 LogMsgType = dbgtype;
246 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
248 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
252 if ((DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
253 || (DEBUGLOG_STDOUT_COLOR_DEBUG == LogMsgType))
257 term = getenv(
"TERM");
260 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
264 for (i = 0; i < COUNT_OF(terms); i++)
267 if (0 == strcmp(terms[i], term))
277 void DebugLogSetLevel(
const int level)
282 case PCSC_LOG_CRITICAL:
288 Log1(PCSC_LOG_INFO,
"debug level=notice");
292 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
297 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=notice",
302 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
304 #define DEBUG_INFO_LENGTH 80
305 char text[DEBUG_INFO_LENGTH];
311 LogCategory &= dbginfo;
313 LogCategory |= dbginfo;
318 if (LogCategory & DEBUG_CATEGORY_APDU)
319 strlcat(text,
" APDU",
sizeof(text));
321 Log2(PCSC_LOG_INFO,
"Debug options:%s", text);
326 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
329 if ((category & DEBUG_CATEGORY_APDU)
330 && (LogCategory & DEBUG_CATEGORY_APDU))
331 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
333 if ((category & DEBUG_CATEGORY_SW)
334 && (LogCategory & DEBUG_CATEGORY_APDU))
335 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
343 void debug_msg(
const char *fmt, ...);
344 void debug_msg(
const char *fmt, ...)
349 if (DEBUGLOG_NO_DEBUG == LogMsgType)
352 va_start(argptr, fmt);
353 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
356 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
357 syslog(LOG_INFO,
"%s", DebugBuffer);
362 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
363 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
365 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
prototypes of strlcpy()/strlcat() imported from OpenBSD
This keeps a list of defines for pcsc-lite.
static signed char LogDoColor
no color by default