pcsc-lite  1.8.10
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 6771 2013-10-17 13:02:41Z rousseau $
10  */
11 
17 #include "config.h"
18 #include "misc.h"
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdio.h>
24 
25 /* We shall not export the log_msg() sumbol */
26 #undef PCSC_API
27 #include "debuglog.h"
28 #include "strlcpycat.h"
29 
30 #define DEBUG_BUF_SIZE 2048
31 
32 #ifdef NO_LOG
33 
34 void log_msg(const int priority, const char *fmt, ...)
35 {
36  (void)priority;
37  (void)fmt;
38 }
39 
40 #else
41 
43 static char LogLevel = PCSC_LOG_CRITICAL+1;
44 
45 static signed char LogDoColor = 0;
47 static void log_init(void)
48 {
49  char *e;
50 
51 #ifdef LIBPCSCLITE
52  e = getenv("PCSCLITE_DEBUG");
53 #else
54  e = getenv("MUSCLECARD_DEBUG");
55 #endif
56  if (e)
57  LogLevel = atoi(e);
58 
59  /* log to stderr and stderr is a tty? */
60  if (isatty(fileno(stderr)))
61  {
62  char *term;
63 
64  term = getenv("TERM");
65  if (term)
66  {
67  const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
68  unsigned int i;
69 
70  /* for each known color terminal */
71  for (i = 0; i < COUNT_OF(terms); i++)
72  {
73  /* we found a supported term? */
74  if (0 == strcmp(terms[i], term))
75  {
76  LogDoColor = 1;
77  break;
78  }
79  }
80  }
81  }
82 } /* log_init */
83 
84 void log_msg(const int priority, const char *fmt, ...)
85 {
86  char DebugBuffer[DEBUG_BUF_SIZE];
87  va_list argptr;
88  static int is_initialized = 0;
89 
90  if (!is_initialized)
91  {
92  log_init();
93  is_initialized = 1;
94  }
95 
96  if (priority < LogLevel) /* log priority lower than threshold? */
97  return;
98 
99  va_start(argptr, fmt);
100  (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
101  va_end(argptr);
102 
103  {
104  if (LogDoColor)
105  {
106  const char *color_pfx = "", *color_sfx = "\33[0m";
107 
108  switch (priority)
109  {
110  case PCSC_LOG_CRITICAL:
111  color_pfx = "\33[01;31m"; /* bright + Red */
112  break;
113 
114  case PCSC_LOG_ERROR:
115  color_pfx = "\33[35m"; /* Magenta */
116  break;
117 
118  case PCSC_LOG_INFO:
119  color_pfx = "\33[34m"; /* Blue */
120  break;
121 
122  case PCSC_LOG_DEBUG:
123  color_pfx = ""; /* normal (black) */
124  color_sfx = "";
125  break;
126  }
127  fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
128  }
129  else
130  fprintf(stderr, "%s\n", DebugBuffer);
131  }
132 } /* log_msg */
133 
134 #endif
135 
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
Definition: debug.c:43
#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:45
This handles debugging.