• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static int
is_socket(int fd)111 is_socket (int fd)
112 {
113   struct stat sbuf;
114 
115   if (fstat(fd, &sbuf) == -1) return 0;
116 
117 #if defined(S_ISSOCK)
118   return S_ISSOCK(sbuf.st_mode);
119 #elif defined (S_IFMT) && defined(S_IFSOCK)
120   return (sbuf.st_mode & S_IFMT) == S_IFSOCK;
121 #else
122   return 0;
123 #endif
124 }
125 
126 void
sanei_debug_msg(int level,int max_level,const char * be,const char * fmt,va_list ap)127 sanei_debug_msg
128   (int level, int max_level, const char *be, const char *fmt, va_list ap)
129 {
130   char *msg;
131 
132   if (max_level >= level)
133     {
134 #if defined(LOG_DEBUG)
135       if (is_socket(fileno(stderr)))
136 	{
137 	  msg = (char *)malloc (sizeof(char) * (strlen(be) + strlen(fmt) + 4));
138 	  if (msg == NULL)
139 	    {
140 	      syslog (LOG_DEBUG, "[sanei_debug] malloc() failed\n");
141 	      vsyslog (LOG_DEBUG, fmt, ap);
142 	    }
143 	  else
144 	    {
145 	      sprintf (msg, "[%s] %s", be, fmt);
146               vsyslog(LOG_DEBUG, msg, ap);
147 	      free (msg);
148 	    }
149 	}
150       else
151 #endif
152 	{
153           struct timeval tv;
154           struct tm *t;
155 
156           gettimeofday (&tv, NULL);
157           t = localtime (&tv.tv_sec);
158 
159           fprintf (stderr, "[%02d:%02d:%02d.%06ld] [%s] ", t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec, be);
160           vfprintf (stderr, fmt, ap);
161 	}
162 
163     }
164 }
165 
166 #ifdef NDEBUG
167 void
sanei_debug_ndebug(int level,const char * fmt,...)168 sanei_debug_ndebug (int level, const char *fmt, ...)
169 {
170   /* this function is never called */
171 }
172 #endif
173