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