pcsc-lite  2.0.3
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-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 
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 
51 #define DEBUG_BUF_SIZE 2048
52 
53 #ifdef NO_LOG
54 
55 void log_msg(const int priority, const char *fmt, ...)
56 {
57  (void)priority;
58  (void)fmt;
59 }
60 
61 #else
62 
64 static char LogLevel = PCSC_LOG_CRITICAL+1;
65 
66 static signed char LogDoColor = 0;
68 static void log_init(void)
69 {
70  char *e;
71 
72  e = getenv("PCSCLITE_DEBUG");
73  if (e)
74  LogLevel = atoi(e);
75 
76  /* log to stderr and stderr is a tty? */
77  if (isatty(fileno(stderr)))
78  {
79  char *term;
80 
81  term = getenv("TERM");
82  if (term)
83  {
84  const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
85  unsigned int i;
86 
87  /* for each known color terminal */
88  for (i = 0; i < COUNT_OF(terms); i++)
89  {
90  /* we found a supported term? */
91  if (0 == strcmp(terms[i], term))
92  {
93  LogDoColor = 1;
94  break;
95  }
96  }
97  }
98  }
99 } /* log_init */
100 
101 void log_msg(const int priority, const char *fmt, ...)
102 {
103  char DebugBuffer[DEBUG_BUF_SIZE];
104  va_list argptr;
105  static bool is_initialized = false;
106 
107  if (!is_initialized)
108  {
109  log_init();
110  is_initialized = true;
111  }
112 
113  if (priority < LogLevel) /* log priority lower than threshold? */
114  return;
115 
116  va_start(argptr, fmt);
117  (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
118  va_end(argptr);
119 
120  {
121  if (LogDoColor)
122  {
123  const char *color_pfx = "", *color_sfx = "\33[0m";
124 
125  switch (priority)
126  {
127  case PCSC_LOG_CRITICAL:
128  color_pfx = "\33[01;31m"; /* bright + Red */
129  break;
130 
131  case PCSC_LOG_ERROR:
132  color_pfx = "\33[35m"; /* Magenta */
133  break;
134 
135  case PCSC_LOG_INFO:
136  color_pfx = "\33[34m"; /* Blue */
137  break;
138 
139  case PCSC_LOG_DEBUG:
140  color_pfx = ""; /* normal (black) */
141  color_sfx = "";
142  break;
143  }
144  fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
145  }
146  else
147  fprintf(stderr, "%s\n", DebugBuffer);
148  }
149 } /* log_msg */
150 
151 #endif
152 
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
Definition: debug.c:64
#define DEBUG_BUF_SIZE
Max string size dumping a maximum of 2 lines of 80 characters.
Definition: debuglog.c:103
static signed char LogDoColor
no color by default
Definition: debug.c:66
This handles debugging.