pcsc-lite  1.8.8
debug.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
3  *
4  * Copyright (C) 1999-2002
5  * David Corcoran <corcoran@linuxnet.com>
6  * Copyright (C) 2002-2011
7  * Ludovic Rousseau <ludovic.rousseau@free.fr>
8  *
9  * $Id: debug.c 5898 2011-08-21 13:53:27Z rousseau $
10  */
11 
17 #include "config.h"
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <stdio.h>
23 
24 #include "debuglog.h"
25 #include "strlcpycat.h"
26 
27 #define DEBUG_BUF_SIZE 2048
28 
29 #ifdef NO_LOG
30 
31 void log_msg(const int priority, const char *fmt, ...)
32 {
33  (void)priority;
34  (void)fmt;
35 }
36 
37 #else
38 
40 static char LogLevel = PCSC_LOG_CRITICAL+1;
41 
42 static signed char LogDoColor = 0;
44 static void log_init(void)
45 {
46  char *e;
47 
48 #ifdef LIBPCSCLITE
49  e = getenv("PCSCLITE_DEBUG");
50 #else
51  e = getenv("MUSCLECARD_DEBUG");
52 #endif
53  if (e)
54  LogLevel = atoi(e);
55 
56  /* log to stderr and stderr is a tty? */
57  if (isatty(fileno(stderr)))
58  {
59  const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
60  char *term;
61 
62  term = getenv("TERM");
63  if (term)
64  {
65  unsigned int i;
66 
67  /* for each known color terminal */
68  for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
69  {
70  /* we found a supported term? */
71  if (0 == strcmp(terms[i], term))
72  {
73  LogDoColor = 1;
74  break;
75  }
76  }
77  }
78  }
79 } /* log_init */
80 
81 void log_msg(const int priority, const char *fmt, ...)
82 {
83  char DebugBuffer[DEBUG_BUF_SIZE];
84  va_list argptr;
85  static int is_initialized = 0;
86 
87  if (!is_initialized)
88  {
89  log_init();
90  is_initialized = 1;
91  }
92 
93  if (priority < LogLevel) /* log priority lower than threshold? */
94  return;
95 
96  va_start(argptr, fmt);
97  (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
98  va_end(argptr);
99 
100  {
101  if (LogDoColor)
102  {
103  const char *color_pfx = "", *color_sfx = "\33[0m";
104 
105  switch (priority)
106  {
107  case PCSC_LOG_CRITICAL:
108  color_pfx = "\33[01;31m"; /* bright + Red */
109  break;
110 
111  case PCSC_LOG_ERROR:
112  color_pfx = "\33[35m"; /* Magenta */
113  break;
114 
115  case PCSC_LOG_INFO:
116  color_pfx = "\33[34m"; /* Blue */
117  break;
118 
119  case PCSC_LOG_DEBUG:
120  color_pfx = ""; /* normal (black) */
121  color_sfx = "";
122  break;
123  }
124  fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
125  }
126  else
127  fprintf(stderr, "%s\n", DebugBuffer);
128  }
129 } /* log_msg */
130 
131 #endif
132 
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
Definition: debug.c:40
#define DEBUG_BUF_SIZE
Max string size dumping a maxmium of 2 lines of 80 characters.
Definition: debuglog.c:84
prototypes of strlcpy()/strlcat() imported from OpenBSD
static signed char LogDoColor
no color by default
Definition: debug.c:42
This handles debugging.