pcsc-lite  1.8.14
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  * $Id$
33  */
34 
40 #include "config.h"
41 #include "misc.h"
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <stdio.h>
47 
48 /* We shall not export the log_msg() sumbol */
49 #undef PCSC_API
50 #include "debuglog.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  char *e;
72 
73 #ifdef LIBPCSCLITE
74  e = getenv("PCSCLITE_DEBUG");
75 #else
76  e = getenv("MUSCLECARD_DEBUG");
77 #endif
78  if (e)
79  LogLevel = atoi(e);
80 
81  /* log to stderr and stderr is a tty? */
82  if (isatty(fileno(stderr)))
83  {
84  char *term;
85 
86  term = getenv("TERM");
87  if (term)
88  {
89  const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
90  unsigned int i;
91 
92  /* for each known color terminal */
93  for (i = 0; i < COUNT_OF(terms); i++)
94  {
95  /* we found a supported term? */
96  if (0 == strcmp(terms[i], term))
97  {
98  LogDoColor = 1;
99  break;
100  }
101  }
102  }
103  }
104 } /* log_init */
105 
106 void log_msg(const int priority, const char *fmt, ...)
107 {
108  char DebugBuffer[DEBUG_BUF_SIZE];
109  va_list argptr;
110  static int is_initialized = 0;
111 
112  if (!is_initialized)
113  {
114  log_init();
115  is_initialized = 1;
116  }
117 
118  if (priority < LogLevel) /* log priority lower than threshold? */
119  return;
120 
121  va_start(argptr, fmt);
122  (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
123  va_end(argptr);
124 
125  {
126  if (LogDoColor)
127  {
128  const char *color_pfx = "", *color_sfx = "\33[0m";
129 
130  switch (priority)
131  {
132  case PCSC_LOG_CRITICAL:
133  color_pfx = "\33[01;31m"; /* bright + Red */
134  break;
135 
136  case PCSC_LOG_ERROR:
137  color_pfx = "\33[35m"; /* Magenta */
138  break;
139 
140  case PCSC_LOG_INFO:
141  color_pfx = "\33[34m"; /* Blue */
142  break;
143 
144  case PCSC_LOG_DEBUG:
145  color_pfx = ""; /* normal (black) */
146  color_sfx = "";
147  break;
148  }
149  fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
150  }
151  else
152  fprintf(stderr, "%s\n", DebugBuffer);
153  }
154 } /* log_msg */
155 
156 #endif
157 
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
Definition: debug.c:65
#define DEBUG_BUF_SIZE
Max string size dumping a maxmium of 2 lines of 80 characters.
Definition: debuglog.c:106
static signed char LogDoColor
no color by default
Definition: debug.c:67
This handles debugging.