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...
39 int _cups_debug_fd
= -1;
40 /* Debug log file descriptor */
41 int _cups_debug_level
= 1;
42 /* Log level (0 to 9) */
50 static regex_t
*debug_filter
= NULL
;
51 /* Filter expression for messages */
52 static int debug_init
= 0; /* Did we initialize debugging? */
53 # ifdef HAVE_PTHREAD_H
54 static pthread_mutex_t debug_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 /* Mutex to control initialization */
56 # endif /* HAVE_PTHREAD_H */
60 * 'debug_vsnprintf()' - Format a string into a fixed size buffer.
63 int /* O - Number of bytes formatted */
64 debug_vsnprintf(char *buffer
, /* O - Output buffer */
65 size_t bufsize
, /* O - Size of output buffer */
66 const char *format
, /* I - printf-style format string */
67 va_list ap
) /* I - Pointer to additional arguments */
69 char *bufptr
, /* Pointer to position in buffer */
70 *bufend
, /* Pointer to end of buffer */
71 size
, /* Size character (h, l, L) */
72 type
; /* Format type character */
73 int width
, /* Width of field */
74 prec
; /* Number of characters of precision */
75 char tformat
[100], /* Temporary format string for sprintf() */
76 *tptr
, /* Pointer into temporary format */
77 temp
[1024]; /* Buffer for formatted numbers */
78 char *s
; /* Pointer to string */
79 int bytes
; /* Total number of bytes needed */
82 if (!buffer
|| bufsize
< 2 || !format
)
86 * Loop through the format string, formatting as needed...
90 bufend
= buffer
+ bufsize
- 1;
108 else if (strchr(" -+#\'", *format
))
114 * Get width from argument...
118 width
= va_arg(ap
, int);
120 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
121 tptr
+= strlen(tptr
);
127 while (isdigit(*format
& 255))
129 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
132 width
= width
* 10 + *format
++ - '0';
138 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
146 * Get precision from argument...
150 prec
= va_arg(ap
, int);
152 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
153 tptr
+= strlen(tptr
);
159 while (isdigit(*format
& 255))
161 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
164 prec
= prec
* 10 + *format
++ - '0';
169 if (*format
== 'l' && format
[1] == 'l')
173 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
181 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
183 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
194 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
202 case 'E' : /* Floating point formats */
207 if ((width
+ 2) > sizeof(temp
))
210 sprintf(temp
, tformat
, va_arg(ap
, double));
212 bytes
+= (int)strlen(temp
);
216 if ((bufptr
+ strlen(temp
)) > bufend
)
218 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
223 strcpy(bufptr
, temp
);
224 bufptr
+= strlen(temp
);
229 case 'B' : /* Integer formats */
237 if ((width
+ 2) > sizeof(temp
))
240 #ifdef HAVE_LONG_LONG
242 sprintf(temp
, tformat
, va_arg(ap
, long long));
244 #endif /* HAVE_LONG_LONG */
246 sprintf(temp
, tformat
, va_arg(ap
, long));
248 sprintf(temp
, tformat
, va_arg(ap
, int));
250 bytes
+= (int)strlen(temp
);
254 if ((bufptr
+ strlen(temp
)) > bufend
)
256 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
261 strcpy(bufptr
, temp
);
262 bufptr
+= strlen(temp
);
267 case 'p' : /* Pointer value */
268 if ((width
+ 2) > sizeof(temp
))
271 sprintf(temp
, tformat
, va_arg(ap
, void *));
273 bytes
+= (int)strlen(temp
);
277 if ((bufptr
+ strlen(temp
)) > bufend
)
279 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
284 strcpy(bufptr
, temp
);
285 bufptr
+= strlen(temp
);
290 case 'c' : /* Character or character array */
296 *bufptr
++ = va_arg(ap
, int);
299 if ((bufptr
+ width
) > bufend
)
300 width
= (int)(bufend
- bufptr
);
302 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
308 case 's' : /* String */
309 if ((s
= va_arg(ap
, char *)) == NULL
)
313 * Copy the C string, replacing control chars and \ with
314 * C character escapes...
317 for (bufend
--; *s
&& bufptr
< bufend
; s
++)
349 else if ((*s
& 255) < ' ')
353 *bufptr
++ = '0' + *s
/ 8;
354 *bufptr
++ = '0' + (*s
& 7);
363 case 'n' : /* Output number of chars so far */
364 *(va_arg(ap
, int *)) = bytes
;
380 * Nul-terminate the string and return the number of characters needed.
390 * '_cups_debug_printf()' - Write a formatted line to the log.
394 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
395 ...) /* I - Additional arguments as needed */
397 va_list ap
; /* Pointer to arguments */
398 struct timeval curtime
; /* Current time */
399 char buffer
[2048]; /* Output buffer */
400 size_t bytes
; /* Number of bytes in buffer */
401 int level
; /* Log level in message */
402 const char *cups_debug_filter
,
403 /* CUPS_DEBUG_FILTER environment variable */
405 /* CUPS_DEBUG_LEVEL environment variable */
406 *cups_debug_log
;/* CUPS_DEBUG_LOG environment variable */
410 * See if we need to do any logging...
416 * Get a lock on the debug initializer, then re-check in case another
417 * thread already did it...
420 pthread_mutex_lock(&debug_mutex
);
424 if ((cups_debug_log
= getenv("CUPS_DEBUG_LOG")) == NULL
)
426 else if (!strcmp(cups_debug_log
, "-"))
430 snprintf(buffer
, sizeof(buffer
), cups_debug_log
, getpid());
432 if (buffer
[0] == '+')
433 _cups_debug_fd
= open(buffer
+ 1, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
435 _cups_debug_fd
= open(buffer
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
438 if ((cups_debug_level
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
439 _cups_debug_level
= atoi(cups_debug_level
);
441 if ((cups_debug_filter
= getenv("CUPS_DEBUG_FILTER")) != NULL
)
443 if ((debug_filter
= (regex_t
*)calloc(1, sizeof(regex_t
))) == NULL
)
444 fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not "
445 "filtered!\n", stderr
);
446 else if (regcomp(debug_filter
, cups_debug_filter
, REG_EXTENDED
))
448 fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not "
449 "filtered!\n", stderr
);
458 pthread_mutex_unlock(&debug_mutex
);
461 if (_cups_debug_fd
< 0)
465 * Filter as needed...
468 if (isdigit(format
[0]))
469 level
= *format
++ - '0';
473 if (level
> _cups_debug_level
)
478 int result
; /* Filter result */
480 pthread_mutex_lock(&debug_mutex
);
481 result
= regexec(debug_filter
, format
, 0, NULL
, 0);
482 pthread_mutex_unlock(&debug_mutex
);
489 * Format the message...
492 gettimeofday(&curtime
, NULL
);
493 snprintf(buffer
, sizeof(buffer
), "%02d:%02d:%02d.%03d ",
494 (int)((curtime
.tv_sec
/ 3600) % 24),
495 (int)((curtime
.tv_sec
/ 60) % 60),
496 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
498 va_start(ap
, format
);
499 debug_vsnprintf(buffer
+ 13, sizeof(buffer
) - 14, format
, ap
);
502 bytes
= strlen(buffer
);
503 if (buffer
[bytes
- 1] != '\n')
505 buffer
[bytes
] = '\n';
507 buffer
[bytes
] = '\0';
514 write(_cups_debug_fd
, buffer
, bytes
);
519 * '_cups_debug_puts()' - Write a single line to the log.
523 _cups_debug_puts(const char *s
) /* I - String to output */
525 char format
[4]; /* C%s */
532 _cups_debug_printf(format
, s
);
536 #elif defined(__APPLE__)
537 /* Mac OS X needs these stubbed since we reference them in the libcups.exp file */
538 void _cups_debug_printf(const char *format
, ...) {}
539 void _cups_debug_puts(const char *s
) {}