27 #include <sys/types.h>
39 void log_msg(
const int priority,
const char *fmt, ...)
45 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
54 void DebugLogSetLogType(
const int dbgtype)
59 void DebugLogSetLevel(
const int level)
64 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
71 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
84 #define DEBUG_BUF_SIZE 2048
86 static char LogMsgType = DEBUGLOG_NO_DEBUG;
87 static char LogCategory = DEBUG_CATEGORY_NOTHING;
94 static void log_line(
const int priority,
const char *DebugBuffer);
96 void log_msg(
const int priority,
const char *fmt, ...)
102 || (DEBUGLOG_NO_DEBUG == LogMsgType))
105 va_start(argptr, fmt);
106 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
109 log_line(priority, DebugBuffer);
112 static void log_line(
const int priority,
const char *DebugBuffer)
114 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
115 syslog(LOG_INFO,
"%s", DebugBuffer);
118 static struct timeval last_time = { 0, 0 };
119 struct timeval new_time = { 0, 0 };
123 gettimeofday(&new_time, NULL);
124 if (0 == last_time.tv_sec)
125 last_time = new_time;
127 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
128 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
132 tmp.tv_usec += 1000000;
134 if (tmp.tv_sec < 100)
135 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
139 last_time = new_time;
143 const char *color_pfx =
"", *color_sfx =
"\33[0m";
144 const char *time_pfx =
"\33[36m", *time_sfx = color_sfx;
148 case PCSC_LOG_CRITICAL:
149 color_pfx =
"\33[01;31m";
153 color_pfx =
"\33[35m";
157 color_pfx =
"\33[34m";
166 printf(
"%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
167 color_pfx, DebugBuffer, color_sfx);
171 printf(
"%.8d %s\n", delta, DebugBuffer);
177 static void log_xxd_always(
const int priority,
const char *msg,
178 const unsigned char *buffer,
const int len)
180 char DebugBuffer[len*3 + strlen(msg) +1];
185 l = strlcpy(DebugBuffer, msg,
sizeof(DebugBuffer));
188 for (i = 0; (i < len); ++i)
191 snprintf(c, 4,
"%02X ", buffer[i]);
195 log_line(priority, DebugBuffer);
198 void log_xxd(
const int priority,
const char *msg,
const unsigned char *buffer,
202 || (DEBUGLOG_NO_DEBUG == LogMsgType))
209 log_xxd_always(priority, msg, buffer, len);
212 void DebugLogSetLogType(
const int dbgtype)
216 case DEBUGLOG_NO_DEBUG:
217 case DEBUGLOG_SYSLOG_DEBUG:
218 case DEBUGLOG_STDOUT_DEBUG:
219 case DEBUGLOG_STDOUT_COLOR_DEBUG:
220 LogMsgType = dbgtype;
223 Log2(PCSC_LOG_CRITICAL,
"unknown log type (%d), using stdout",
225 LogMsgType = DEBUGLOG_STDOUT_DEBUG;
229 if ((DEBUGLOG_STDOUT_DEBUG == LogMsgType && isatty(fileno(stdout)))
230 || (DEBUGLOG_STDOUT_COLOR_DEBUG == LogMsgType))
232 const char *terms[] = {
"linux",
"xterm",
"xterm-color",
"Eterm",
"rxvt",
"rxvt-unicode",
"xterm-256color" };
235 term = getenv(
"TERM");
241 for (i = 0; i <
sizeof(terms) /
sizeof(terms[0]); i++)
244 if (0 == strcmp(terms[i], term))
254 void DebugLogSetLevel(
const int level)
259 case PCSC_LOG_CRITICAL:
265 Log1(PCSC_LOG_INFO,
"debug level=notice");
269 Log1(PCSC_LOG_DEBUG,
"debug level=debug");
274 Log2(PCSC_LOG_CRITICAL,
"unknown level (%d), using level=notice",
279 INTERNAL
int DebugLogSetCategory(
const int dbginfo)
281 #define DEBUG_INFO_LENGTH 80
282 char text[DEBUG_INFO_LENGTH];
288 LogCategory &= dbginfo;
290 LogCategory |= dbginfo;
295 if (LogCategory & DEBUG_CATEGORY_APDU)
296 strlcat(text,
" APDU",
sizeof(text));
298 Log2(PCSC_LOG_INFO,
"Debug options:%s", text);
303 INTERNAL
void DebugLogCategory(
const int category,
const unsigned char *buffer,
306 if ((category & DEBUG_CATEGORY_APDU)
307 && (LogCategory & DEBUG_CATEGORY_APDU))
308 log_xxd_always(PCSC_LOG_INFO,
"APDU: ", buffer, len);
310 if ((category & DEBUG_CATEGORY_SW)
311 && (LogCategory & DEBUG_CATEGORY_APDU))
312 log_xxd_always(PCSC_LOG_INFO,
"SW: ", buffer, len);
320 void debug_msg(
const char *fmt, ...);
321 void debug_msg(
const char *fmt, ...)
326 if (DEBUGLOG_NO_DEBUG == LogMsgType)
329 va_start(argptr, fmt);
330 vsnprintf(DebugBuffer,
sizeof DebugBuffer, fmt, argptr);
333 if (DEBUGLOG_SYSLOG_DEBUG == LogMsgType)
334 syslog(LOG_INFO,
"%s", DebugBuffer);
339 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len);
340 void debug_xxd(
const char *msg,
const unsigned char *buffer,
const int len)
342 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