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