53 #include <sys/types.h>
65 void log_msg(
const int priority,
const char *fmt, ...)
71 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
80 void DebugLogSetLogType(
const int dbgtype)
85 void DebugLogSetLevel(
const int level)
90 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
97 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
110 #define DEBUG_BUF_SIZE 2048
112 static char LogMsgType = DEBUGLOG_NO_DEBUG;
113 static char LogCategory = DEBUG_CATEGORY_NOTHING;
120 static void log_line(
const int priority,
const char *DebugBuffer);
122 void log_msg(
const int priority,
const char *fmt, ...)
128 || (DEBUGLOG_NO_DEBUG == LogMsgType))
131 va_start(argptr, fmt);
132 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
135 log_line(priority, DebugBuffer);
138 static void log_line(
const int priority,
const char *DebugBuffer)
140 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
141 syslog(LOG_INFO,
"%s", DebugBuffer);
144 static struct timeval last_time = { 0, 0 };
145 struct timeval new_time = { 0, 0 };
149 gettimeofday(&new_time, NULL);
150 if (0 == last_time.tv_sec)
151 last_time = new_time;
153 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
154 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
158 tmp.tv_usec += 1000000;
160 if (tmp.tv_sec < 100)
161 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
165 last_time = new_time;
169 const char *color_pfx =
"", *color_sfx =
"\33[0m";
170 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
174 case PCSC_LOG_CRITICAL:
175 color_pfx =
"\33[01;31m";
179 color_pfx =
"\33[35m";
183 color_pfx =
"\33[34m";
192 printf(
"%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
193 color_pfx, DebugBuffer, color_sfx);
197 printf(
"%.8d %s\n", delta, DebugBuffer);
203 static void log_xxd_always(
const int priority,
const char *msg,
204 const unsigned char *buffer,
const int len)
206 char DebugBuffer[len*3 + strlen(msg) +1];
211 l = strlcpy(DebugBuffer, msg,
sizeof(DebugBuffer));
214 for (i = 0; (i < len); ++i)
217 snprintf(c, 4,
"%02X ", buffer[i]);
221 log_line(priority, DebugBuffer);
224 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
228 || (DEBUGLOG_NO_DEBUG == LogMsgType))
235 log_xxd_always(priority, msg, buffer, len);
238 void DebugLogSetLogType(
const int dbgtype)
242 case DEBUGLOG_NO_DEBUG:
243 case DEBUGLOG_SYSLOG_DEBUG:
244 case DEBUGLOG_STDOUT_DEBUG:
245 case DEBUGLOG_STDOUT_COLOR_DEBUG:
246 LogMsgType = dbgtype;
249 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
251 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
255 if ((DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
256 || (DEBUGLOG_STDOUT_COLOR_DEBUG == LogMsgType))
260 term = getenv(
"TERM");
263 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
267 for (i = 0; i < COUNT_OF(terms); i++)
270 if (0 == strcmp(terms[i], term))
280 void DebugLogSetLevel(
const int level)
285 case PCSC_LOG_CRITICAL:
291 Log1(PCSC_LOG_INFO,
"debug level=notice");
295 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
300 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=notice",
305 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
307 #define DEBUG_INFO_LENGTH 80
308 char text[DEBUG_INFO_LENGTH];
314 LogCategory &= dbginfo;
316 LogCategory |= dbginfo;
321 if (LogCategory & DEBUG_CATEGORY_APDU)
322 strlcat(text,
" APDU",
sizeof(text));
324 Log2(PCSC_LOG_INFO,
"Debug options:%s", text);
329 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
332 if ((category & DEBUG_CATEGORY_APDU)
333 && (LogCategory & DEBUG_CATEGORY_APDU))
334 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
336 if ((category & DEBUG_CATEGORY_SW)
337 && (LogCategory & DEBUG_CATEGORY_APDU))
338 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
346 void debug_msg(
const char *fmt, ...);
347 void debug_msg(
const char *fmt, ...)
352 if (DEBUGLOG_NO_DEBUG == LogMsgType)
355 va_start(argptr, fmt);
356 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
359 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
360 syslog(LOG_INFO,
"%s", DebugBuffer);
365 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
366 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
368 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