pcsc-lite  2.5.0
debug.c
Go to the documentation of this file.
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-2024
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 
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 #include <stdbool.h>
46 
47 /* We shall not export the log_msg() symbol */
48 #undef PCSC_API
49 #include "debuglog.h"
50 #include "sys_generic.h"
51 
52 #define DEBUG_BUF_SIZE 2048
53 
54 #ifdef NO_LOG
55 
56 void log_msg(const int priority, const char *fmt, ...)
57 {
58  (void)priority;
59  (void)fmt;
60 }
61 
62 #else
63 
65 static char LogLevel = PCSC_LOG_CRITICAL+1;
66 
67 static signed char LogDoColor = 0;
69 static void log_init(void)
70 {
71  const char *e;
72 
73  e = SYS_GetEnv("PCSCLITE_DEBUG");
74  if (e)
75  LogLevel = atoi(e);
76 
77  /* log to stderr and stderr is a tty? */
78  if (isatty(fileno(stderr)))
79  {
80  const char *term;
81 
82  term = SYS_GetEnv("TERM");
83  if (term)
84  {
85  const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
86  unsigned int i;
87 
88  /* for each known color terminal */
89  for (i = 0; i < COUNT_OF(terms); i++)
90  {
91  /* we found a supported term? */
92  if (0 == strcmp(terms[i], term))
93  {
94  LogDoColor = 1;
95  break;
96  }
97  }
98  }
99  }
100 } /* log_init */
101 
102 void log_msg(const int priority, const char *fmt, ...)
103 {
104  char DebugBuffer[DEBUG_BUF_SIZE];
105  va_list argptr;
106  static bool is_initialized = false;
107 
108  if (!is_initialized)
109  {
110  log_init();
111  is_initialized = true;
112  }
113 
114  if (priority < LogLevel) /* log priority lower than threshold? */
115  return;
116 
117  va_start(argptr, fmt);
118  (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
119  va_end(argptr);
120 
121  {
122  if (LogDoColor)
123  {
124  const char *color_pfx = "", *color_sfx = "\33[0m";
125 
126  switch (priority)
127  {
128  case PCSC_LOG_CRITICAL:
129  color_pfx = "\33[01;31m"; /* bright + Red */
130  break;
131 
132  case PCSC_LOG_ERROR:
133  color_pfx = "\33[35m"; /* Magenta */
134  break;
135 
136  case PCSC_LOG_INFO:
137  color_pfx = "\33[34m"; /* Blue */
138  break;
139 
140  case PCSC_LOG_DEBUG:
141  color_pfx = ""; /* normal (black) */
142  color_sfx = "";
143  break;
144  }
145  fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
146  }
147  else
148  fprintf(stderr, "%s\n", DebugBuffer);
149  }
150 } /* log_msg */
151 
152 void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
153  const int len)
154 {
155  char DebugBuffer[len*3 + strlen(msg) +1];
156  int i;
157  char *c;
158 
159  /* DebugBuffer is always big enough for msg */
160  strcpy(DebugBuffer, msg);
161  c = DebugBuffer + strlen(DebugBuffer);
162 
163  for (i = 0; (i < len); ++i)
164  {
165  /* 2 hex characters, 1 space, 1 NUL : total 4 characters */
166  snprintf(c, 4, "%02X ", buffer[i]);
167  c += 3;
168  }
169 
170  log_msg(priority, "%s", DebugBuffer);
171 } /* log_xxd */
172 
173 #endif
174 
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
Definition: debug.c:65
This handles abstract system level calls.
const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)
Definition: sys_unix.c:168
#define DEBUG_BUF_SIZE
Max string size dumping a maximum of 2 lines of 80 characters.
Definition: debuglog.c:102
static signed char LogDoColor
no color by default
Definition: debug.c:67
This handles debugging.