pcsc-lite 2.3.0
debug.c
Go to the documentation of this file.
1/*
2 * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
3 *
4 * Copyright (C) 1999-2002
5 * David Corcoran <corcoran@musclecard.com>
6 * Copyright (C) 2002-2024
7 * Ludovic Rousseau <ludovic.rousseau@free.fr>
8 *
9Redistribution and use in source and binary forms, with or without
10modification, are permitted provided that the following conditions
11are met:
12
131. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
152. 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.
183. 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
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
38#include "config.h"
39#include "misc.h"
40#include <stdarg.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <string.h>
44#include <stdio.h>
45#include <stdbool.h>
46
47/* We shall not export the log_msg() symbol */
48#undef PCSC_API
49#include "debuglog.h"
50#include "sys_generic.h"
51
52#define DEBUG_BUF_SIZE 2048
53
54#ifdef NO_LOG
55
56void log_msg(const int priority, const char *fmt, ...)
57{
58 (void)priority;
59 (void)fmt;
60}
61
62#else
63
65static char LogLevel = PCSC_LOG_CRITICAL+1;
66
67static signed char LogDoColor = 0;
69static void log_init(void)
70{
71 const char *e;
72
73 e = SYS_GetEnv("PCSCLITE_DEBUG");
74 if (e)
75 LogLevel = atoi(e);
76
77 /* log to stderr and stderr is a tty? */
78 if (isatty(fileno(stderr)))
79 {
80 const char *term;
81
82 term = SYS_GetEnv("TERM");
83 if (term)
84 {
85 const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
86 unsigned int i;
87
88 /* for each known color terminal */
89 for (i = 0; i < COUNT_OF(terms); i++)
90 {
91 /* we found a supported term? */
92 if (0 == strcmp(terms[i], term))
93 {
94 LogDoColor = 1;
95 break;
96 }
97 }
98 }
99 }
100} /* log_init */
101
102void log_msg(const int priority, const char *fmt, ...)
103{
104 char DebugBuffer[DEBUG_BUF_SIZE];
105 va_list argptr;
106 static bool is_initialized = false;
107
108 if (!is_initialized)
109 {
110 log_init();
111 is_initialized = true;
112 }
113
114 if (priority < LogLevel) /* log priority lower than threshold? */
115 return;
116
117 va_start(argptr, fmt);
118 (void)vsnprintf(DebugBuffer, DEBUG_BUF_SIZE, fmt, argptr);
119 va_end(argptr);
120
121 {
122 if (LogDoColor)
123 {
124 const char *color_pfx = "", *color_sfx = "\33[0m";
125
126 switch (priority)
127 {
128 case PCSC_LOG_CRITICAL:
129 color_pfx = "\33[01;31m"; /* bright + Red */
130 break;
131
132 case PCSC_LOG_ERROR:
133 color_pfx = "\33[35m"; /* Magenta */
134 break;
135
136 case PCSC_LOG_INFO:
137 color_pfx = "\33[34m"; /* Blue */
138 break;
139
140 case PCSC_LOG_DEBUG:
141 color_pfx = ""; /* normal (black) */
142 color_sfx = "";
143 break;
144 }
145 fprintf(stderr, "%s%s%s\n", color_pfx, DebugBuffer, color_sfx);
146 }
147 else
148 fprintf(stderr, "%s\n", DebugBuffer);
149 }
150} /* log_msg */
151
152#endif
153
static char LogLevel
default level is quiet to avoid polluting fd 2 (possibly NOT stderr)
Definition debug.c:65
static signed char LogDoColor
no color by default
Definition debug.c:67
This handles debugging.
This handles abstract system level calls.
const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)
Definition sys_unix.c:168