pcsc-lite  1.8.11
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 Changes to this license can be made only by the copyright author with
22 explicit written consent.
23 
24 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $Id: debug.c 6851 2014-02-14 15:43:32Z rousseau $
36  */
37 
43 #include "config.h"
44 #include "misc.h"
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <stdio.h>
50 
51 /* We shall not export the log_msg() sumbol */
52 #undef PCSC_API
53 #include "debuglog.h"
54 #include "strlcpycat.h"
55 
56 #define DEBUG_BUF_SIZE 2048
57 
58 #ifdef NO_LOG
59 
60 void log_msg(const int priority, const char *fmt, ...)
61 {
62  (void)priority;
63  (void)fmt;
64 }
65 
66 #else
67 
69 static char LogLevel = PCSC_LOG_CRITICAL+1;
70 
71 static signed char LogDoColor = 0;
73 static void log_init(void)
74 {
75  char *e;
76 
77 #ifdef LIBPCSCLITE
78  e = getenv("PCSCLITE_DEBUG");
79 #else
80  e = getenv("MUSCLECARD_DEBUG");
81 #endif
82  if (e)
83  LogLevel = atoi(e);
84 
85  /* log to stderr and stderr is a tty? */
86  if (isatty(fileno(stderr)))
87  {
88  char *term;
89 
90  term = getenv("TERM");
91  if (term)
92  {
93  const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
94  unsigned int i;
95 
96  /* for each known color terminal */
97  for (i = 0; i < COUNT_OF(terms); i++)
98  {
99  /* we found a supported term? */
100  if (0 == strcmp(terms[i], term))
101  {
102  LogDoColor = 1;
103  break;
104  }
105  }
106  }
107  }
108 } /* log_init */
109 
110 void log_msg(const int priority, const char *fmt, ...)
111 {
112  char DebugBuffer[DEBUG_BUF_SIZE];
113  va_list argptr;
114  static int is_initialized = 0;
115 
116  if (!is_initialized)
117  {
118  log_init();
119  is_initialized = 1;
120  }
121 
122  if (priority < LogLevel) /* log priority lower than threshold? */
123  return;
124 
125  va_start(argptr, fmt);
126  (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
127  va_end(argptr);
128 
129  {
130  if (LogDoColor)
131  {
132  const char *color_pfx = "", *color_sfx = "\33[0m";
133 
134  switch (priority)
135  {
136  case PCSC_LOG_CRITICAL:
137  color_pfx = "\33[01;31m"; /* bright + Red */
138  break;
139 
140  case PCSC_LOG_ERROR:
141  color_pfx = "\33[35m"; /* Magenta */
142  break;
143 
144  case PCSC_LOG_INFO:
145  color_pfx = "\33[34m"; /* Blue */
146  break;
147 
148  case PCSC_LOG_DEBUG:
149  color_pfx = ""; /* normal (black) */
150  color_sfx = "";
151  break;
152  }
153  fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
154  }
155  else
156  fprintf(stderr, "%s\n", DebugBuffer);
157  }
158 } /* log_msg */
159 
160 #endif
161 
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
Definition: debug.c:69
#define DEBUG_BUF_SIZE
Max string size dumping a maxmium of 2 lines of 80 characters.
Definition: debuglog.c:110
prototypes of strlcpy()/strlcat() imported from OpenBSD
static signed char LogDoColor
no color by default
Definition: debug.c:71
This handles debugging.