4 * Debugging functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2008-2009 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * debug_vsnprintf() - Format a string into a fixed size buffer.
19 * _cups_debug_printf() - Write a formatted line to the log.
20 * _cups_debug_puts() - Write a single line to the log.
24 * Include necessary headers...
32 # include <sys/time.h>
45 int _cups_debug_fd
= -1;
46 /* Debug log file descriptor */
47 int _cups_debug_level
= 1;
48 /* Log level (0 to 9) */
57 static regex_t
*debug_filter
= NULL
;
58 /* Filter expression for messages */
60 static int debug_init
= 0; /* Did we initialize debugging? */
61 # ifdef HAVE_PTHREAD_H
62 static pthread_mutex_t debug_mutex
= PTHREAD_MUTEX_INITIALIZER
;
63 /* Mutex to control initialization */
64 # endif /* HAVE_PTHREAD_H */
68 * 'debug_vsnprintf()' - Format a string into a fixed size buffer.
71 static int /* O - Number of bytes formatted */
72 debug_vsnprintf(char *buffer
, /* O - Output buffer */
73 size_t bufsize
, /* O - Size of output buffer */
74 const char *format
, /* I - printf-style format string */
75 va_list ap
) /* I - Pointer to additional arguments */
77 char *bufptr
, /* Pointer to position in buffer */
78 *bufend
, /* Pointer to end of buffer */
79 size
, /* Size character (h, l, L) */
80 type
; /* Format type character */
81 int width
, /* Width of field */
82 prec
; /* Number of characters of precision */
83 char tformat
[100], /* Temporary format string for sprintf() */
84 *tptr
, /* Pointer into temporary format */
85 temp
[1024]; /* Buffer for formatted numbers */
86 char *s
; /* Pointer to string */
87 int bytes
; /* Total number of bytes needed */
90 if (!buffer
|| bufsize
< 2 || !format
)
94 * Loop through the format string, formatting as needed...
98 bufend
= buffer
+ bufsize
- 1;
116 else if (strchr(" -+#\'", *format
))
122 * Get width from argument...
126 width
= va_arg(ap
, int);
128 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
129 tptr
+= strlen(tptr
);
135 while (isdigit(*format
& 255))
137 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
140 width
= width
* 10 + *format
++ - '0';
146 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
154 * Get precision from argument...
158 prec
= va_arg(ap
, int);
160 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
161 tptr
+= strlen(tptr
);
167 while (isdigit(*format
& 255))
169 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
172 prec
= prec
* 10 + *format
++ - '0';
177 if (*format
== 'l' && format
[1] == 'l')
181 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
189 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
191 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
202 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
210 case 'E' : /* Floating point formats */
215 if ((width
+ 2) > sizeof(temp
))
218 sprintf(temp
, tformat
, va_arg(ap
, double));
220 bytes
+= (int)strlen(temp
);
224 if ((bufptr
+ strlen(temp
)) > bufend
)
226 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
231 strcpy(bufptr
, temp
);
232 bufptr
+= strlen(temp
);
237 case 'B' : /* Integer formats */
245 if ((width
+ 2) > sizeof(temp
))
248 #ifdef HAVE_LONG_LONG
250 sprintf(temp
, tformat
, va_arg(ap
, long long));
252 #endif /* HAVE_LONG_LONG */
254 sprintf(temp
, tformat
, va_arg(ap
, long));
256 sprintf(temp
, tformat
, va_arg(ap
, int));
258 bytes
+= (int)strlen(temp
);
262 if ((bufptr
+ strlen(temp
)) > bufend
)
264 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
269 strcpy(bufptr
, temp
);
270 bufptr
+= strlen(temp
);
275 case 'p' : /* Pointer value */
276 if ((width
+ 2) > sizeof(temp
))
279 sprintf(temp
, tformat
, va_arg(ap
, void *));
281 bytes
+= (int)strlen(temp
);
285 if ((bufptr
+ strlen(temp
)) > bufend
)
287 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
292 strcpy(bufptr
, temp
);
293 bufptr
+= strlen(temp
);
298 case 'c' : /* Character or character array */
304 *bufptr
++ = va_arg(ap
, int);
307 if ((bufptr
+ width
) > bufend
)
308 width
= (int)(bufend
- bufptr
);
310 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
316 case 's' : /* String */
317 if ((s
= va_arg(ap
, char *)) == NULL
)
321 * Copy the C string, replacing control chars and \ with
322 * C character escapes...
325 for (bufend
--; *s
&& bufptr
< bufend
; s
++)
357 else if ((*s
& 255) < ' ')
361 *bufptr
++ = '0' + *s
/ 8;
362 *bufptr
++ = '0' + (*s
& 7);
371 case 'n' : /* Output number of chars so far */
372 *(va_arg(ap
, int *)) = bytes
;
388 * Nul-terminate the string and return the number of characters needed.
398 * '_cups_debug_printf()' - Write a formatted line to the log.
402 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
403 ...) /* I - Additional arguments as needed */
405 va_list ap
; /* Pointer to arguments */
406 struct timeval curtime
; /* Current time */
407 char buffer
[2048]; /* Output buffer */
408 size_t bytes
; /* Number of bytes in buffer */
409 int level
; /* Log level in message */
410 const char *cups_debug_filter
,
411 /* CUPS_DEBUG_FILTER environment variable */
413 /* CUPS_DEBUG_LEVEL environment variable */
414 *cups_debug_log
;/* CUPS_DEBUG_LOG environment variable */
418 * See if we need to do any logging...
424 * Get a lock on the debug initializer, then re-check in case another
425 * thread already did it...
428 pthread_mutex_lock(&debug_mutex
);
432 if ((cups_debug_log
= getenv("CUPS_DEBUG_LOG")) == NULL
)
434 else if (!strcmp(cups_debug_log
, "-"))
438 snprintf(buffer
, sizeof(buffer
), cups_debug_log
, getpid());
440 if (buffer
[0] == '+')
441 _cups_debug_fd
= open(buffer
+ 1, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
443 _cups_debug_fd
= open(buffer
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
446 if ((cups_debug_level
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
447 _cups_debug_level
= atoi(cups_debug_level
);
449 if ((cups_debug_filter
= getenv("CUPS_DEBUG_FILTER")) != NULL
)
451 if ((debug_filter
= (regex_t
*)calloc(1, sizeof(regex_t
))) == NULL
)
452 fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not "
453 "filtered!\n", stderr
);
454 else if (regcomp(debug_filter
, cups_debug_filter
, REG_EXTENDED
))
456 fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not "
457 "filtered!\n", stderr
);
466 pthread_mutex_unlock(&debug_mutex
);
469 if (_cups_debug_fd
< 0)
473 * Filter as needed...
476 if (isdigit(format
[0]))
477 level
= *format
++ - '0';
481 if (level
> _cups_debug_level
)
486 int result
; /* Filter result */
488 pthread_mutex_lock(&debug_mutex
);
489 result
= regexec(debug_filter
, format
, 0, NULL
, 0);
490 pthread_mutex_unlock(&debug_mutex
);
497 * Format the message...
500 gettimeofday(&curtime
, NULL
);
501 snprintf(buffer
, sizeof(buffer
), "%02d:%02d:%02d.%03d ",
502 (int)((curtime
.tv_sec
/ 3600) % 24),
503 (int)((curtime
.tv_sec
/ 60) % 60),
504 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
506 va_start(ap
, format
);
507 debug_vsnprintf(buffer
+ 13, sizeof(buffer
) - 14, format
, ap
);
510 bytes
= strlen(buffer
);
511 if (buffer
[bytes
- 1] != '\n')
513 buffer
[bytes
] = '\n';
515 buffer
[bytes
] = '\0';
522 write(_cups_debug_fd
, buffer
, bytes
);
527 * '_cups_debug_puts()' - Write a single line to the log.
531 _cups_debug_puts(const char *s
) /* I - String to output */
533 char format
[4]; /* C%s */
540 _cups_debug_printf(format
, s
);
544 #elif defined(__APPLE__)
545 /* Mac OS X needs these stubbed since we reference them in the libcups.exp file */
546 void _cups_debug_printf(const char *format
, ...) {}
547 void _cups_debug_puts(const char *s
) {}