Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00017 #include "config.h"
00018 #include <stdarg.h>
00019 #include <stdlib.h>
00020 #include <unistd.h>
00021 #include <string.h>
00022 #include <stdio.h>
00023
00024 #include "debuglog.h"
00025 #include "strlcpycat.h"
00026
00027 #define DEBUG_BUF_SIZE 2048
00028
00029 #ifdef NO_LOG
00030
00031 void log_msg(const int priority, const char *fmt, ...)
00032 {
00033 (void)priority;
00034 (void)fmt;
00035 }
00036
00037 void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
00038 const int len)
00039 {
00040 (void)priority;
00041 (void)msg;
00042 (void)buffer;
00043 (void)len;
00044 }
00045
00046 #else
00047
00049 static char LogLevel = PCSC_LOG_CRITICAL+1;
00050
00051 static signed char LogDoColor = 0;
00053 static void log_init(void)
00054 {
00055 char *e;
00056
00057 #ifdef LIBPCSCLITE
00058 e = getenv("PCSCLITE_DEBUG");
00059 #else
00060 e = getenv("MUSCLECARD_DEBUG");
00061 #endif
00062 if (e)
00063 LogLevel = atoi(e);
00064
00065
00066 if (isatty(fileno(stderr)))
00067 {
00068 const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
00069 char *term;
00070
00071 term = getenv("TERM");
00072 if (term)
00073 {
00074 unsigned int i;
00075
00076
00077 for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
00078 {
00079
00080 if (0 == strcmp(terms[i], term))
00081 {
00082 LogDoColor = 1;
00083 break;
00084 }
00085 }
00086 }
00087 }
00088 }
00089
00090 void log_msg(const int priority, const char *fmt, ...)
00091 {
00092 char DebugBuffer[DEBUG_BUF_SIZE];
00093 va_list argptr;
00094 static int is_initialized = 0;
00095
00096 if (!is_initialized)
00097 {
00098 log_init();
00099 is_initialized = 1;
00100 }
00101
00102 if (priority < LogLevel)
00103 return;
00104
00105 va_start(argptr, fmt);
00106 (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
00107 va_end(argptr);
00108
00109 {
00110 if (LogDoColor)
00111 {
00112 const char *color_pfx = "", *color_sfx = "\33[0m";
00113
00114 switch (priority)
00115 {
00116 case PCSC_LOG_CRITICAL:
00117 color_pfx = "\33[01;31m";
00118 break;
00119
00120 case PCSC_LOG_ERROR:
00121 color_pfx = "\33[35m";
00122 break;
00123
00124 case PCSC_LOG_INFO:
00125 color_pfx = "\33[34m";
00126 break;
00127
00128 case PCSC_LOG_DEBUG:
00129 color_pfx = "";
00130 color_sfx = "";
00131 break;
00132 }
00133 fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
00134 }
00135 else
00136 fprintf(stderr, "%s\n", DebugBuffer);
00137 }
00138 }
00139
00140 void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
00141 const int len)
00142 {
00143 char DebugBuffer[DEBUG_BUF_SIZE];
00144 int i;
00145 char *c;
00146 char *debug_buf_end;
00147
00148 if (priority < LogLevel)
00149 return;
00150
00151 debug_buf_end = DebugBuffer + DEBUG_BUF_SIZE - 5;
00152
00153 (void)strlcpy(DebugBuffer, msg, sizeof(DebugBuffer));
00154 c = DebugBuffer + strlen(DebugBuffer);
00155
00156 for (i = 0; (i < len) && (c < debug_buf_end); ++i)
00157 {
00158 sprintf(c, "%02X ", buffer[i]);
00159 c += strlen(c);
00160 }
00161
00162 fprintf(stderr, "%s\n", DebugBuffer);
00163 }
00164
00165 #endif
00166