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 */
48 static regex_t
*debug_filter
= NULL
;
49 /* Filter expression for messages */
50 static int debug_init
= 1; /* Did we initialize debugging? */
51 # ifdef HAVE_PTHREAD_H
52 static int debug_level
= 0;/* Log level (0 to 9) */
53 static pthread_mutex_t debug_mutex
= PTHREAD_MUTEX_INITIALIZER
;
54 /* Mutex to control initialization */
55 # endif /* HAVE_PTHREAD_H */
59 * 'debug_vsnprintf()' - Format a string into a fixed size buffer.
62 int /* O - Number of bytes formatted */
63 debug_vsnprintf(char *buffer
, /* O - Output buffer */
64 size_t bufsize
, /* O - Size of output buffer */
65 const char *format
, /* I - printf-style format string */
66 va_list ap
) /* I - Pointer to additional arguments */
68 char *bufptr
, /* Pointer to position in buffer */
69 *bufend
, /* Pointer to end of buffer */
70 size
, /* Size character (h, l, L) */
71 type
; /* Format type character */
72 int width
, /* Width of field */
73 prec
; /* Number of characters of precision */
74 char tformat
[100], /* Temporary format string for sprintf() */
75 *tptr
, /* Pointer into temporary format */
76 temp
[1024]; /* Buffer for formatted numbers */
77 char *s
; /* Pointer to string */
78 int bytes
; /* Total number of bytes needed */
81 if (!buffer
|| bufsize
< 2 || !format
)
85 * Loop through the format string, formatting as needed...
89 bufend
= buffer
+ bufsize
- 1;
107 else if (strchr(" -+#\'", *format
))
113 * Get width from argument...
117 width
= va_arg(ap
, int);
119 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
120 tptr
+= strlen(tptr
);
126 while (isdigit(*format
& 255))
128 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
131 width
= width
* 10 + *format
++ - '0';
137 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
145 * Get precision from argument...
149 prec
= va_arg(ap
, int);
151 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
152 tptr
+= strlen(tptr
);
158 while (isdigit(*format
& 255))
160 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
163 prec
= prec
* 10 + *format
++ - '0';
168 if (*format
== 'l' && format
[1] == 'l')
172 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
180 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
182 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
193 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
201 case 'E' : /* Floating point formats */
206 if ((width
+ 2) > sizeof(temp
))
209 sprintf(temp
, tformat
, va_arg(ap
, double));
211 bytes
+= (int)strlen(temp
);
215 if ((bufptr
+ strlen(temp
)) > bufend
)
217 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
222 strcpy(bufptr
, temp
);
223 bufptr
+= strlen(temp
);
228 case 'B' : /* Integer formats */
236 if ((width
+ 2) > sizeof(temp
))
239 #ifdef HAVE_LONG_LONG
241 sprintf(temp
, tformat
, va_arg(ap
, long long));
243 #endif /* HAVE_LONG_LONG */
245 sprintf(temp
, tformat
, va_arg(ap
, long));
247 sprintf(temp
, tformat
, va_arg(ap
, int));
249 bytes
+= (int)strlen(temp
);
253 if ((bufptr
+ strlen(temp
)) > bufend
)
255 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
260 strcpy(bufptr
, temp
);
261 bufptr
+= strlen(temp
);
266 case 'p' : /* Pointer value */
267 if ((width
+ 2) > sizeof(temp
))
270 sprintf(temp
, tformat
, va_arg(ap
, void *));
272 bytes
+= (int)strlen(temp
);
276 if ((bufptr
+ strlen(temp
)) > bufend
)
278 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
283 strcpy(bufptr
, temp
);
284 bufptr
+= strlen(temp
);
289 case 'c' : /* Character or character array */
295 *bufptr
++ = va_arg(ap
, int);
298 if ((bufptr
+ width
) > bufend
)
299 width
= (int)(bufend
- bufptr
);
301 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
307 case 's' : /* String */
308 if ((s
= va_arg(ap
, char *)) == NULL
)
312 * Copy the C string, replacing control chars and \ with
313 * C character escapes...
316 for (bufend
--; *s
&& bufptr
< bufend
; s
++)
348 else if ((*s
& 255) < ' ')
352 *bufptr
++ = '0' + *s
/ 8;
353 *bufptr
++ = '0' + (*s
& 7);
362 case 'n' : /* Output number of chars so far */
363 *(va_arg(ap
, int *)) = bytes
;
379 * Nul-terminate the string and return the number of characters needed.
389 * '_cups_debug_printf()' - Write a formatted line to the log.
393 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
394 ...) /* I - Additional arguments as needed */
396 va_list ap
; /* Pointer to arguments */
397 struct timeval curtime
; /* Current time */
398 char buffer
[2048]; /* Output buffer */
399 size_t bytes
; /* Number of bytes in buffer */
400 int level
; /* Log level in message */
401 const char *cups_debug_filter
,
402 /* CUPS_DEBUG_FILTER environment variable */
404 /* CUPS_DEBUG_LEVEL environment variable */
405 *cups_debug_log
;/* CUPS_DEBUG_LOG environment variable */
409 * See if we need to do any logging...
415 * Get a lock on the debug initializer, then re-check in case another
416 * thread already did it...
419 pthread_mutex_lock(&debug_mutex
);
425 if ((cups_debug_log
= getenv("CUPS_DEBUG_LOG")) == NULL
)
427 else if (!strcmp(cups_debug_log
, "-"))
431 snprintf(buffer
, sizeof(buffer
), cups_debug_log
, getpid());
432 _cups_debug_fd
= open(buffer
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
435 if ((cups_debug_level
= getenv("CUPS_DEBUG_LEVEL")) != NULL
)
436 debug_level
= atoi(cups_debug_level
);
438 if ((cups_debug_filter
= getenv("CUPS_DEBUG_FILTER")) != NULL
)
440 if ((debug_filter
= (regex_t
*)calloc(1, sizeof(regex_t
))) == NULL
)
441 fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not "
442 "filtered!\n", stderr
);
443 else if (regcomp(debug_filter
, cups_debug_filter
, REG_EXTENDED
))
445 fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not "
446 "filtered!\n", stderr
);
453 pthread_mutex_unlock(&debug_mutex
);
456 if (_cups_debug_fd
< 0)
460 * Filter as needed...
463 if (isdigit(format
[0]))
464 level
= *format
++ - '0';
468 if (level
> debug_level
)
473 int result
; /* Filter result */
475 pthread_mutex_lock(&debug_mutex
);
476 result
= regexec(debug_filter
, format
, 0, NULL
, 0);
477 pthread_mutex_unlock(&debug_mutex
);
484 * Format the message...
487 gettimeofday(&curtime
, NULL
);
488 snprintf(buffer
, sizeof(buffer
), "%02d:%02d:%02d.%03d ",
489 (int)((curtime
.tv_sec
/ 3600) % 24),
490 (int)((curtime
.tv_sec
/ 60) % 60),
491 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
493 va_start(ap
, format
);
494 debug_vsnprintf(buffer
+ 13, sizeof(buffer
) - 14, format
, ap
);
497 bytes
= strlen(buffer
);
498 if (buffer
[bytes
- 1] != '\n')
500 buffer
[bytes
] = '\n';
502 buffer
[bytes
] = '\0';
509 write(_cups_debug_fd
, buffer
, bytes
);
514 * '_cups_debug_puts()' - Write a single line to the log.
518 _cups_debug_puts(const char *s
) /* I - String to output */
520 char format
[4]; /* C%s */
527 _cups_debug_printf(format
, s
);
531 #elif defined(__APPLE__)
532 /* Mac OS X needs these stubbed since we reference them in the libcups.exp file */
533 void _cups_debug_printf(const char *format
, ...) {}
534 void _cups_debug_puts(const char *s
) {}