#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "debug.h"
#define DEBUG_BUF_SIZE 2048
static char LogLevel = PCSC_LOG_CRITICAL+1;
static signed char LogDoColor = 0;
static void log_init(void)
{
char *e;
#ifdef LIBPCSCLITE
e = getenv("PCSCLITE_DEBUG");
#else
e = getenv("MUSCLECARD_DEBUG");
#endif
if (e)
LogLevel = atoi(e);
if (isatty(fileno(stderr)))
{
const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
char *term;
term = getenv("TERM");
if (term)
{
unsigned int i;
for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
{
if (0 == strcmp(terms[i], term))
{
LogDoColor = 1;
break;
}
}
}
}
}
void log_msg(const int priority, const char *fmt, ...)
{
char DebugBuffer[DEBUG_BUF_SIZE];
va_list argptr;
static int is_initialized = 0;
if (!is_initialized)
{
log_init();
is_initialized = 1;
}
if (priority < LogLevel)
return;
va_start(argptr, fmt);
(void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
va_end(argptr);
{
if (LogDoColor)
{
const char *color_pfx = "", *color_sfx = "\33[0m";
switch (priority)
{
case PCSC_LOG_CRITICAL:
color_pfx = "\33[01;31m";
break;
case PCSC_LOG_ERROR:
color_pfx = "\33[35m";
break;
case PCSC_LOG_INFO:
color_pfx = "\33[34m";
break;
case PCSC_LOG_DEBUG:
color_pfx = "";
color_sfx = "";
break;
}
fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
}
else
fprintf(stderr, "%s\n", DebugBuffer);
}
}
void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
const int len)
{
char DebugBuffer[DEBUG_BUF_SIZE];
int i;
char *c;
char *debug_buf_end;
if (priority < LogLevel)
return;
debug_buf_end = DebugBuffer + DEBUG_BUF_SIZE - 5;
(void)strlcpy(DebugBuffer, msg, sizeof(DebugBuffer));
c = DebugBuffer + strlen(DebugBuffer);
for (i = 0; (i < len) && (c < debug_buf_end); ++i)
{
sprintf(c, "%02X ", buffer[i]);
c += strlen(c);
}
fprintf(stderr, "%s\n", DebugBuffer);
}
int DebugLogSetCategory(const int dbginfo)
{
return 0;
}
void DebugLogCategory(const int category, const unsigned char *buffer,
const int len)
{
}
void DebugLogSetLevel(const int level)
{
}
void DebugLogSetLogType(const int dbgtype)
{
}