2 * Debugging functions for CUPS.
4 * Copyright 2008-2015 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 * Include necessary headers...
13 #include "cups-private.h"
14 #include "thread-private.h"
16 # include <sys/timeb.h>
19 # define getpid (int)GetCurrentProcessId
20 int /* O - 0 on success, -1 on failure */
21 _cups_gettimeofday(struct timeval
*tv
, /* I - Timeval struct */
22 void *tz
) /* I - Timezone */
24 struct _timeb timebuffer
; /* Time buffer struct */
26 tv
->tv_sec
= (long)timebuffer
.time
;
27 tv
->tv_usec
= timebuffer
.millitm
* 1000;
31 # include <sys/time.h>
42 int _cups_debug_fd
= -1;
43 /* Debug log file descriptor */
44 int _cups_debug_level
= 1;
45 /* Log level (0 to 9) */
53 static regex_t
*debug_filter
= NULL
;
54 /* Filter expression for messages */
55 static int debug_init
= 0; /* Did we initialize debugging? */
56 static _cups_mutex_t debug_init_mutex
= _CUPS_MUTEX_INITIALIZER
,
57 /* Mutex to control initialization */
58 debug_log_mutex
= _CUPS_MUTEX_INITIALIZER
;
59 /* Mutex to serialize log entries */
63 * 'debug_thread_id()' - Return an integer representing the current thread.
66 static int /* O - Local thread ID */
69 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
72 return (cg
->thread_id
);
77 * '_cups_debug_printf()' - Write a formatted line to the log.
81 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
82 ...) /* I - Additional arguments as needed */
84 va_list ap
; /* Pointer to arguments */
85 struct timeval curtime
; /* Current time */
86 char buffer
[2048]; /* Output buffer */
87 ssize_t bytes
; /* Number of bytes in buffer */
88 int level
; /* Log level in message */
92 * See if we need to do any logging...
96 _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"),
97 getenv("CUPS_DEBUG_FILTER"), 0);
99 if (_cups_debug_fd
< 0)
103 * Filter as needed...
106 if (isdigit(format
[0]))
107 level
= *format
++ - '0';
111 if (level
> _cups_debug_level
)
116 int result
; /* Filter result */
118 _cupsMutexLock(&debug_init_mutex
);
119 result
= regexec(debug_filter
, format
, 0, NULL
, 0);
120 _cupsMutexUnlock(&debug_init_mutex
);
127 * Format the message...
130 gettimeofday(&curtime
, NULL
);
131 snprintf(buffer
, sizeof(buffer
), "T%03d %02d:%02d:%02d.%03d ",
132 debug_thread_id(), (int)((curtime
.tv_sec
/ 3600) % 24),
133 (int)((curtime
.tv_sec
/ 60) % 60),
134 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
136 va_start(ap
, format
);
137 bytes
= _cups_safe_vsnprintf(buffer
+ 19, sizeof(buffer
) - 20, format
, ap
) + 19;
140 if ((size_t)bytes
>= (sizeof(buffer
) - 1))
142 buffer
[sizeof(buffer
) - 2] = '\n';
143 bytes
= sizeof(buffer
) - 1;
145 else if (buffer
[bytes
- 1] != '\n')
147 buffer
[bytes
++] = '\n';
148 buffer
[bytes
] = '\0';
155 _cupsMutexLock(&debug_log_mutex
);
156 write(_cups_debug_fd
, buffer
, (size_t)bytes
);
157 _cupsMutexUnlock(&debug_log_mutex
);
162 * '_cups_debug_puts()' - Write a single line to the log.
166 _cups_debug_puts(const char *s
) /* I - String to output */
168 struct timeval curtime
; /* Current time */
169 char buffer
[2048]; /* Output buffer */
170 ssize_t bytes
; /* Number of bytes in buffer */
171 int level
; /* Log level in message */
175 * See if we need to do any logging...
179 _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"),
180 getenv("CUPS_DEBUG_FILTER"), 0);
182 if (_cups_debug_fd
< 0)
186 * Filter as needed...
194 if (level
> _cups_debug_level
)
199 int result
; /* Filter result */
201 _cupsMutexLock(&debug_init_mutex
);
202 result
= regexec(debug_filter
, s
, 0, NULL
, 0);
203 _cupsMutexUnlock(&debug_init_mutex
);
210 * Format the message...
213 gettimeofday(&curtime
, NULL
);
214 bytes
= snprintf(buffer
, sizeof(buffer
), "T%03d %02d:%02d:%02d.%03d %s",
215 debug_thread_id(), (int)((curtime
.tv_sec
/ 3600) % 24),
216 (int)((curtime
.tv_sec
/ 60) % 60),
217 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000),
220 if ((size_t)bytes
>= (sizeof(buffer
) - 1))
222 buffer
[sizeof(buffer
) - 2] = '\n';
223 bytes
= sizeof(buffer
) - 1;
225 else if (buffer
[bytes
- 1] != '\n')
227 buffer
[bytes
++] = '\n';
228 buffer
[bytes
] = '\0';
235 _cupsMutexLock(&debug_log_mutex
);
236 write(_cups_debug_fd
, buffer
, (size_t)bytes
);
237 _cupsMutexUnlock(&debug_log_mutex
);
242 * '_cups_debug_set()' - Enable or disable debug logging.
246 _cups_debug_set(const char *logfile
, /* I - Log file or NULL */
247 const char *level
, /* I - Log level or NULL */
248 const char *filter
, /* I - Filter string or NULL */
249 int force
) /* I - Force initialization */
251 _cupsMutexLock(&debug_init_mutex
);
253 if (!debug_init
|| force
)
256 * Restore debug settings to defaults...
259 if (_cups_debug_fd
!= -1)
261 close(_cups_debug_fd
);
267 regfree((regex_t
*)debug_filter
);
271 _cups_debug_level
= 1;
274 * Open logs, set log levels, etc.
279 else if (!strcmp(logfile
, "-"))
283 char buffer
[1024]; /* Filename buffer */
285 snprintf(buffer
, sizeof(buffer
), logfile
, getpid());
287 if (buffer
[0] == '+')
288 _cups_debug_fd
= open(buffer
+ 1, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
290 _cups_debug_fd
= open(buffer
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
294 _cups_debug_level
= atoi(level
);
298 if ((debug_filter
= (regex_t
*)calloc(1, sizeof(regex_t
))) == NULL
)
299 fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not "
300 "filtered!\n", stderr
);
301 else if (regcomp(debug_filter
, filter
, REG_EXTENDED
))
303 fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not "
304 "filtered!\n", stderr
);
313 _cupsMutexUnlock(&debug_init_mutex
);
319 * '_cups_safe_vsnprintf()' - Format a string into a fixed size buffer,
320 * quoting special characters.
323 ssize_t
/* O - Number of bytes formatted */
324 _cups_safe_vsnprintf(
325 char *buffer
, /* O - Output buffer */
326 size_t bufsize
, /* O - Size of output buffer */
327 const char *format
, /* I - printf-style format string */
328 va_list ap
) /* I - Pointer to additional arguments */
330 char *bufptr
, /* Pointer to position in buffer */
331 *bufend
, /* Pointer to end of buffer */
332 size
, /* Size character (h, l, L) */
333 type
; /* Format type character */
334 int width
, /* Width of field */
335 prec
; /* Number of characters of precision */
336 char tformat
[100], /* Temporary format string for snprintf() */
337 *tptr
, /* Pointer into temporary format */
338 temp
[1024]; /* Buffer for formatted numbers */
339 char *s
; /* Pointer to string */
340 ssize_t bytes
; /* Total number of bytes needed */
343 if (!buffer
|| bufsize
< 2 || !format
)
347 * Loop through the format string, formatting as needed...
351 bufend
= buffer
+ bufsize
- 1;
369 else if (strchr(" -+#\'", *format
))
375 * Get width from argument...
379 width
= va_arg(ap
, int);
381 snprintf(tptr
, sizeof(tformat
) - (size_t)(tptr
- tformat
), "%d", width
);
382 tptr
+= strlen(tptr
);
388 while (isdigit(*format
& 255))
390 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
393 width
= width
* 10 + *format
++ - '0';
399 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
407 * Get precision from argument...
411 prec
= va_arg(ap
, int);
413 snprintf(tptr
, sizeof(tformat
) - (size_t)(tptr
- tformat
), "%d", prec
);
414 tptr
+= strlen(tptr
);
420 while (isdigit(*format
& 255))
422 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
425 prec
= prec
* 10 + *format
++ - '0';
430 if (*format
== 'l' && format
[1] == 'l')
434 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
442 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
444 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
455 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
463 case 'E' : /* Floating point formats */
468 if ((size_t)(width
+ 2) > sizeof(temp
))
471 snprintf(temp
, sizeof(temp
), tformat
, va_arg(ap
, double));
473 bytes
+= (int)strlen(temp
);
477 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
478 bufptr
+= strlen(bufptr
);
482 case 'B' : /* Integer formats */
490 if ((size_t)(width
+ 2) > sizeof(temp
))
493 # ifdef HAVE_LONG_LONG
495 snprintf(temp
, sizeof(temp
), tformat
, va_arg(ap
, long long));
497 # endif /* HAVE_LONG_LONG */
499 snprintf(temp
, sizeof(temp
), tformat
, va_arg(ap
, long));
501 snprintf(temp
, sizeof(temp
), tformat
, va_arg(ap
, int));
503 bytes
+= (int)strlen(temp
);
507 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
508 bufptr
+= strlen(bufptr
);
512 case 'p' : /* Pointer value */
513 if ((size_t)(width
+ 2) > sizeof(temp
))
516 snprintf(temp
, sizeof(temp
), tformat
, va_arg(ap
, void *));
518 bytes
+= (int)strlen(temp
);
522 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
523 bufptr
+= strlen(bufptr
);
527 case 'c' : /* Character or character array */
533 *bufptr
++ = (char)va_arg(ap
, int);
536 if ((bufptr
+ width
) > bufend
)
537 width
= (int)(bufend
- bufptr
);
539 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
545 case 's' : /* String */
546 if ((s
= va_arg(ap
, char *)) == NULL
)
550 * Copy the C string, replacing control chars and \ with
551 * C character escapes...
554 for (bufend
--; *s
&& bufptr
< bufend
; s
++)
592 else if ((*s
& 255) < ' ')
594 if ((bufptr
+ 2) >= bufend
)
599 *bufptr
++ = '0' + *s
/ 8;
600 *bufptr
++ = '0' + (*s
& 7);
613 case 'n' : /* Output number of chars so far */
614 *(va_arg(ap
, int *)) = (int)bytes
;
630 * Nul-terminate the string and return the number of characters needed.