1 /* sane - Scanner Access Now Easy.
2 Copyright (C) 1996, 1997 David Mosberger-Tang and Andreas Beck
3 This file is part of the SANE package.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 As a special exception, the authors of SANE give permission for
19 additional uses of the libraries contained in this release of SANE.
20
21 The exception is that, if you link a SANE library with other files
22 to produce an executable, this does not by itself cause the
23 resulting executable to be covered by the GNU General Public
24 License. Your use of that executable is in no way restricted on
25 account of linking the SANE library code into it.
26
27 This exception does not, however, invalidate any other reasons why
28 the executable file might be covered by the GNU General Public
29 License.
30
31 If you submit changes to SANE to the maintainers to be included in
32 a subsequent release, you agree by submitting the changes that
33 those changes may be distributed with this exception intact.
34
35 If you write modifications of your own for SANE, it is your choice
36 whether to permit this exception to apply to your modifications.
37 If you do not wish that, delete this exception notice. */
38
39 #include "../include/sane/config.h"
40
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47 #include <string.h>
48 #include <stdarg.h>
49 #ifdef HAVE_VSYSLOG
50 #include <syslog.h>
51 #endif
52 #ifdef HAVE_OS2_H
53 #include <sys/types.h>
54 #endif
55 #ifdef HAVE_SYS_SOCKET_H
56 #include <sys/socket.h>
57 #endif
58 #include <sys/stat.h>
59 #include <time.h>
60 #include <sys/time.h>
61
62 #ifdef HAVE_OS2_H
63 # define INCL_DOS
64 # include <os2.h>
65 #endif
66
67 #define BACKEND_NAME sanei_debug
68 #include "../include/sane/sanei_debug.h"
69
70 /* If a frontend enables translations, the system toupper()
71 * call will use the LANG env var. We need to use ascii
72 * instead, so the debugging env var name matches the docs.
73 * This is a particular problem in Turkish, where 'i' does
74 * not capitalize to 'I' */
75 static char
toupper_ascii(int c)76 toupper_ascii (int c)
77 {
78 if(c > 0x60 && c < 0x7b)
79 return c - 0x20;
80 return c;
81 }
82
83 void
sanei_init_debug(const char * backend, int * var)84 sanei_init_debug (const char * backend, int * var)
85 {
86 char ch, buf[256] = "SANE_DEBUG_";
87 const char * val;
88 unsigned int i;
89
90 *var = 0;
91
92 for (i = 11; (ch = backend[i - 11]) != 0; ++i)
93 {
94 if (i >= sizeof (buf) - 1)
95 break;
96 buf[i] = toupper_ascii(ch);
97 }
98 buf[i] = '\0';
99
100 val = getenv (buf);
101
102 if (!val)
103 return;
104
105 *var = atoi (val);
106
107 DBG (0, "Setting debug level of %s to %d.\n", backend, *var);
108 }
109
110 #if defined(LOG_DEBUG)
111 static int
is_socket(int fd)112 is_socket (int fd)
113 {
114 struct stat sbuf;
115
116 if (fstat(fd, &sbuf) == -1) return 0;
117
118 #if defined(S_ISSOCK)
119 return S_ISSOCK(sbuf.st_mode);
120 #elif defined (S_IFMT) && defined(S_IFSOCK)
121 return (sbuf.st_mode & S_IFMT) == S_IFSOCK;
122 #else
123 return 0;
124 #endif
125 }
126 #endif
127
128 void
sanei_debug_msg(int level, int max_level, const char *be, const char *fmt, va_list ap)129 sanei_debug_msg
130 (int level, int max_level, const char *be, const char *fmt, va_list ap)
131 {
132 char *msg;
133
134 if (max_level >= level)
135 {
136 #if defined(LOG_DEBUG)
137 if (is_socket(fileno(stderr)))
138 {
139 msg = (char *)malloc (sizeof(char) * (strlen(be) + strlen(fmt) + 4));
140 if (msg == NULL)
141 {
142 syslog (LOG_DEBUG, "[sanei_debug] malloc() failed\n");
143 vsyslog (LOG_DEBUG, fmt, ap);
144 }
145 else
146 {
147 sprintf (msg, "[%s] %s", be, fmt);
148 vsyslog(LOG_DEBUG, msg, ap);
149 free (msg);
150 }
151 }
152 else
153 #endif
154 {
155 struct timeval tv;
156 struct tm *t;
157
158 gettimeofday (&tv, NULL);
159 t = localtime (&tv.tv_sec);
160
161 fprintf (stderr, "[%02d:%02d:%02d.%06ld] [%s] ", t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec, be);
162 vfprintf (stderr, fmt, ap);
163 }
164
165 }
166 }
167
168 #ifdef NDEBUG
169 void
sanei_debug_ndebug(int level, const char *fmt, ...)170 sanei_debug_ndebug (int level, const char *fmt, ...)
171 {
172 /* this function is never called */
173 }
174 #endif
175