1 /*
2 * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
3 *
4 * Copyright (C) 1999-2002
5 * David Corcoran <corcoran@musclecard.com>
6 * Copyright (C) 2002-2011
7 * Ludovic Rousseau <ludovic.rousseau@free.fr>
8 *
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 3. The name of the author may not be used to endorse or promote products
19 derived from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /**
34 * @file
35 * @brief This handles debugging for libpcsclite.
36 */
37
38 #include "config.h"
39 #include "misc.h"
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <stdio.h>
45
46 /* We shall not export the log_msg() sumbol */
47 #undef PCSC_API
48 #include "debuglog.h"
49
50 #define DEBUG_BUF_SIZE 2048
51
52 #ifdef NO_LOG
53
log_msg(const int priority,const char * fmt,...)54 void log_msg(const int priority, const char *fmt, ...)
55 {
56 (void)priority;
57 (void)fmt;
58 }
59
60 #else
61
62 /** default level is quiet to avoid polluting fd 2 (possibly NOT stderr) */
63 static char LogLevel = PCSC_LOG_CRITICAL+1;
64
65 static signed char LogDoColor = 0; /**< no color by default */
66
log_init(void)67 static void log_init(void)
68 {
69 char *e;
70
71 e = getenv("PCSCLITE_DEBUG");
72 if (e)
73 LogLevel = atoi(e);
74
75 /* log to stderr and stderr is a tty? */
76 if (isatty(fileno(stderr)))
77 {
78 char *term;
79
80 term = getenv("TERM");
81 if (term)
82 {
83 const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
84 unsigned int i;
85
86 /* for each known color terminal */
87 for (i = 0; i < COUNT_OF(terms); i++)
88 {
89 /* we found a supported term? */
90 if (0 == strcmp(terms[i], term))
91 {
92 LogDoColor = 1;
93 break;
94 }
95 }
96 }
97 }
98 } /* log_init */
99
log_msg(const int priority,const char * fmt,...)100 void log_msg(const int priority, const char *fmt, ...)
101 {
102 char DebugBuffer[DEBUG_BUF_SIZE];
103 va_list argptr;
104 static int is_initialized = 0;
105
106 if (!is_initialized)
107 {
108 log_init();
109 is_initialized = 1;
110 }
111
112 if (priority < LogLevel) /* log priority lower than threshold? */
113 return;
114
115 va_start(argptr, fmt);
116 (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
117 va_end(argptr);
118
119 {
120 if (LogDoColor)
121 {
122 const char *color_pfx = "", *color_sfx = "\33[0m";
123
124 switch (priority)
125 {
126 case PCSC_LOG_CRITICAL:
127 color_pfx = "\33[01;31m"; /* bright + Red */
128 break;
129
130 case PCSC_LOG_ERROR:
131 color_pfx = "\33[35m"; /* Magenta */
132 break;
133
134 case PCSC_LOG_INFO:
135 color_pfx = "\33[34m"; /* Blue */
136 break;
137
138 case PCSC_LOG_DEBUG:
139 color_pfx = ""; /* normal (black) */
140 color_sfx = "";
141 break;
142 }
143 fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
144 }
145 else
146 fprintf(stderr, "%s\n", DebugBuffer);
147 }
148 } /* log_msg */
149
150 #endif
151
152