#include "globals.h"
#include "debug.h"
#ifdef WIN32
# include <io.h>
#else
# include <sys/time.h>
# include <unistd.h>
#endif
#include <fcntl.h>
#ifndef WIN32
# include <regex.h>
#endif
int _cups_debug_fd = -1;
int _cups_debug_level = 1;
#ifdef DEBUG
# ifndef WIN32
static regex_t *debug_filter = NULL;
# endif
static int debug_init = 0;
# ifdef HAVE_PTHREAD_H
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
# endif
int
debug_vsnprintf(char *buffer,
size_t bufsize,
const char *format,
va_list ap)
{
char *bufptr,
*bufend,
size,
type;
int width,
prec;
char tformat[100],
*tptr,
temp[1024];
char *s;
int bytes;
if (!buffer || bufsize < 2 || !format)
return (-1);
bufptr = buffer;
bufend = buffer + bufsize - 1;
bytes = 0;
while (*format)
{
if (*format == '%')
{
tptr = tformat;
*tptr++ = *format++;
if (*format == '%')
{
if (bufptr < bufend)
*bufptr++ = *format;
bytes ++;
format ++;
continue;
}
else if (strchr(" -+#\'", *format))
*tptr++ = *format++;
if (*format == '*')
{
format ++;
width = va_arg(ap, int);
snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", width);
tptr += strlen(tptr);
}
else
{
width = 0;
while (isdigit(*format & 255))
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
width = width * 10 + *format++ - '0';
}
}
if (*format == '.')
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
format ++;
if (*format == '*')
{
format ++;
prec = va_arg(ap, int);
snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", prec);
tptr += strlen(tptr);
}
else
{
prec = 0;
while (isdigit(*format & 255))
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
prec = prec * 10 + *format++ - '0';
}
}
}
if (*format == 'l' && format[1] == 'l')
{
size = 'L';
if (tptr < (tformat + sizeof(tformat) - 2))
{
*tptr++ = 'l';
*tptr++ = 'l';
}
format += 2;
}
else if (*format == 'h' || *format == 'l' || *format == 'L')
{
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
size = *format++;
}
else
size = 0;
if (!*format)
break;
if (tptr < (tformat + sizeof(tformat) - 1))
*tptr++ = *format;
type = *format++;
*tptr = '\0';
switch (type)
{
case 'E' :
case 'G' :
case 'e' :
case 'f' :
case 'g' :
if ((width + 2) > sizeof(temp))
break;
sprintf(temp, tformat, va_arg(ap, double));
bytes += (int)strlen(temp);
if (bufptr)
{
if ((bufptr + strlen(temp)) > bufend)
{
strncpy(bufptr, temp, (size_t)(bufend - bufptr));
bufptr = bufend;
}
else
{
strcpy(bufptr, temp);
bufptr += strlen(temp);
}
}
break;
case 'B' :
case 'X' :
case 'b' :
case 'd' :
case 'i' :
case 'o' :
case 'u' :
case 'x' :
if ((width + 2) > sizeof(temp))
break;
#ifdef HAVE_LONG_LONG
if (size == 'L')
sprintf(temp, tformat, va_arg(ap, long long));
else
#endif
if (size == 'l')
sprintf(temp, tformat, va_arg(ap, long));
else
sprintf(temp, tformat, va_arg(ap, int));
bytes += (int)strlen(temp);
if (bufptr)
{
if ((bufptr + strlen(temp)) > bufend)
{
strncpy(bufptr, temp, (size_t)(bufend - bufptr));
bufptr = bufend;
}
else
{
strcpy(bufptr, temp);
bufptr += strlen(temp);
}
}
break;
case 'p' :
if ((width + 2) > sizeof(temp))
break;
sprintf(temp, tformat, va_arg(ap, void *));
bytes += (int)strlen(temp);
if (bufptr)
{
if ((bufptr + strlen(temp)) > bufend)
{
strncpy(bufptr, temp, (size_t)(bufend - bufptr));
bufptr = bufend;
}
else
{
strcpy(bufptr, temp);
bufptr += strlen(temp);
}
}
break;
case 'c' :
bytes += width;
if (bufptr)
{
if (width <= 1)
*bufptr++ = va_arg(ap, int);
else
{
if ((bufptr + width) > bufend)
width = (int)(bufend - bufptr);
memcpy(bufptr, va_arg(ap, char *), (size_t)width);
bufptr += width;
}
}
break;
case 's' :
if ((s = va_arg(ap, char *)) == NULL)
s = "(null)";
for (bufend --; *s && bufptr < bufend; s ++)
{
if (*s == '\n')
{
*bufptr++ = '\\';
*bufptr++ = 'n';
}
else if (*s == '\r')
{
*bufptr++ = '\\';
*bufptr++ = 'r';
}
else if (*s == '\t')
{
*bufptr++ = '\\';
*bufptr++ = 't';
}
else if (*s == '\\')
{
*bufptr++ = '\\';
*bufptr++ = '\\';
}
else if (*s == '\'')
{
*bufptr++ = '\\';
*bufptr++ = '\'';
}
else if (*s == '\"')
{
*bufptr++ = '\\';
*bufptr++ = '\"';
}
else if ((*s & 255) < ' ')
{
*bufptr++ = '\\';
*bufptr++ = '0';
*bufptr++ = '0' + *s / 8;
*bufptr++ = '0' + (*s & 7);
}
else
*bufptr++ = *s;
}
bufend ++;
break;
case 'n' :
*(va_arg(ap, int *)) = bytes;
break;
}
}
else
{
bytes ++;
if (bufptr < bufend)
*bufptr++ = *format;
format ++;
}
}
*bufptr = '\0';
return (bytes);
}
void
_cups_debug_printf(const char *format,
...)
{
va_list ap;
struct timeval curtime;
char buffer[2048];
size_t bytes;
int level;
const char *cups_debug_filter,
*cups_debug_level,
*cups_debug_log;
if (!debug_init)
{
pthread_mutex_lock(&debug_mutex);
if (!debug_init)
{
if ((cups_debug_log = getenv("CUPS_DEBUG_LOG")) == NULL)
_cups_debug_fd = -1;
else if (!strcmp(cups_debug_log, "-"))
_cups_debug_fd = 2;
else
{
snprintf(buffer, sizeof(buffer), cups_debug_log, getpid());
if (buffer[0] == '+')
_cups_debug_fd = open(buffer + 1, O_WRONLY | O_APPEND | O_CREAT, 0644);
else
_cups_debug_fd = open(buffer, O_WRONLY | O_TRUNC | O_CREAT, 0644);
}
if ((cups_debug_level = getenv("CUPS_DEBUG_LEVEL")) != NULL)
_cups_debug_level = atoi(cups_debug_level);
if ((cups_debug_filter = getenv("CUPS_DEBUG_FILTER")) != NULL)
{
if ((debug_filter = (regex_t *)calloc(1, sizeof(regex_t))) == NULL)
fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not "
"filtered!\n", stderr);
else if (regcomp(debug_filter, cups_debug_filter, REG_EXTENDED))
{
fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not "
"filtered!\n", stderr);
free(debug_filter);
debug_filter = NULL;
}
}
debug_init = 1;
}
pthread_mutex_unlock(&debug_mutex);
}
if (_cups_debug_fd < 0)
return;
if (isdigit(format[0]))
level = *format++ - '0';
else
level = 0;
if (level > _cups_debug_level)
return;
if (debug_filter)
{
int result;
pthread_mutex_lock(&debug_mutex);
result = regexec(debug_filter, format, 0, NULL, 0);
pthread_mutex_unlock(&debug_mutex);
if (result)
return;
}
gettimeofday(&curtime, NULL);
snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d.%03d ",
(int)((curtime.tv_sec / 3600) % 24),
(int)((curtime.tv_sec / 60) % 60),
(int)(curtime.tv_sec % 60), (int)(curtime.tv_usec / 1000));
va_start(ap, format);
debug_vsnprintf(buffer + 13, sizeof(buffer) - 14, format, ap);
va_end(ap);
bytes = strlen(buffer);
if (buffer[bytes - 1] != '\n')
{
buffer[bytes] = '\n';
bytes ++;
buffer[bytes] = '\0';
}
write(_cups_debug_fd, buffer, bytes);
}
void
_cups_debug_puts(const char *s)
{
char format[4];
format[0] = *s++;
format[1] = '%';
format[2] = 's';
format[3] = '\0';
_cups_debug_printf(format, s);
}
#elif defined(__APPLE__)
void _cups_debug_printf(const char *format, ...) {}
void _cups_debug_puts(const char *s) {}
#endif