4 * Debugging functions for CUPS.
6 * Copyright 2008-2012 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.
21 * _cups_debug_set() - Enable or disable debug logging.
25 * Include necessary headers...
28 #include "cups-private.h"
29 #include "thread-private.h"
31 # include <sys/timeb.h>
34 # define getpid (int)GetCurrentProcessId
35 int /* O - 0 on success, -1 on failure */
36 _cups_gettimeofday(struct timeval
*tv
, /* I - Timeval struct */
37 void *tz
) /* I - Timezone */
39 struct _timeb timebuffer
; /* Time buffer struct */
41 tv
->tv_sec
= (long)timebuffer
.time
;
42 tv
->tv_usec
= timebuffer
.millitm
* 1000;
46 # include <sys/time.h>
57 int _cups_debug_fd
= -1;
58 /* Debug log file descriptor */
59 int _cups_debug_level
= 1;
60 /* Log level (0 to 9) */
68 static regex_t
*debug_filter
= NULL
;
69 /* Filter expression for messages */
70 static int debug_init
= 0; /* Did we initialize debugging? */
71 static _cups_mutex_t debug_init_mutex
= _CUPS_MUTEX_INITIALIZER
,
72 /* Mutex to control initialization */
73 debug_log_mutex
= _CUPS_MUTEX_INITIALIZER
;
74 /* Mutex to serialize log entries */
78 * 'debug_thread_id()' - Return an integer representing the current thread.
81 static int /* O - Local thread ID */
84 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
87 return (cg
->thread_id
);
92 * 'debug_vsnprintf()' - Format a string into a fixed size buffer.
95 static int /* O - Number of bytes formatted */
96 debug_vsnprintf(char *buffer
, /* O - Output buffer */
97 size_t bufsize
, /* O - Size of output buffer */
98 const char *format
, /* I - printf-style format string */
99 va_list ap
) /* I - Pointer to additional arguments */
101 char *bufptr
, /* Pointer to position in buffer */
102 *bufend
, /* Pointer to end of buffer */
103 size
, /* Size character (h, l, L) */
104 type
; /* Format type character */
105 int width
, /* Width of field */
106 prec
; /* Number of characters of precision */
107 char tformat
[100], /* Temporary format string for sprintf() */
108 *tptr
, /* Pointer into temporary format */
109 temp
[1024]; /* Buffer for formatted numbers */
110 char *s
; /* Pointer to string */
111 int bytes
; /* Total number of bytes needed */
114 if (!buffer
|| bufsize
< 2 || !format
)
118 * Loop through the format string, formatting as needed...
122 bufend
= buffer
+ bufsize
- 1;
140 else if (strchr(" -+#\'", *format
))
146 * Get width from argument...
150 width
= va_arg(ap
, int);
152 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
153 tptr
+= strlen(tptr
);
159 while (isdigit(*format
& 255))
161 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
164 width
= width
* 10 + *format
++ - '0';
170 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
178 * Get precision from argument...
182 prec
= va_arg(ap
, int);
184 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
185 tptr
+= strlen(tptr
);
191 while (isdigit(*format
& 255))
193 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
196 prec
= prec
* 10 + *format
++ - '0';
201 if (*format
== 'l' && format
[1] == 'l')
205 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
213 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
215 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
226 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
234 case 'E' : /* Floating point formats */
239 if ((width
+ 2) > sizeof(temp
))
242 sprintf(temp
, tformat
, va_arg(ap
, double));
244 bytes
+= (int)strlen(temp
);
248 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
249 bufptr
+= strlen(bufptr
);
253 case 'B' : /* Integer formats */
261 if ((width
+ 2) > sizeof(temp
))
264 # ifdef HAVE_LONG_LONG
266 sprintf(temp
, tformat
, va_arg(ap
, long long));
268 # endif /* HAVE_LONG_LONG */
270 sprintf(temp
, tformat
, va_arg(ap
, long));
272 sprintf(temp
, tformat
, va_arg(ap
, int));
274 bytes
+= (int)strlen(temp
);
278 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
279 bufptr
+= strlen(bufptr
);
283 case 'p' : /* Pointer value */
284 if ((width
+ 2) > sizeof(temp
))
287 sprintf(temp
, tformat
, va_arg(ap
, void *));
289 bytes
+= (int)strlen(temp
);
293 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
294 bufptr
+= strlen(bufptr
);
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
++)
363 else if ((*s
& 255) < ' ')
365 if ((bufptr
+ 2) >= bufend
)
370 *bufptr
++ = '0' + *s
/ 8;
371 *bufptr
++ = '0' + (*s
& 7);
384 case 'n' : /* Output number of chars so far */
385 *(va_arg(ap
, int *)) = bytes
;
401 * Nul-terminate the string and return the number of characters needed.
411 * '_cups_debug_printf()' - Write a formatted line to the log.
415 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
416 ...) /* I - Additional arguments as needed */
418 va_list ap
; /* Pointer to arguments */
419 struct timeval curtime
; /* Current time */
420 char buffer
[2048]; /* Output buffer */
421 size_t bytes
; /* Number of bytes in buffer */
422 int level
; /* Log level in message */
426 * See if we need to do any logging...
430 _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"),
431 getenv("CUPS_DEBUG_FILTER"), 0);
433 if (_cups_debug_fd
< 0)
437 * Filter as needed...
440 if (isdigit(format
[0]))
441 level
= *format
++ - '0';
445 if (level
> _cups_debug_level
)
450 int result
; /* Filter result */
452 _cupsMutexLock(&debug_init_mutex
);
453 result
= regexec(debug_filter
, format
, 0, NULL
, 0);
454 _cupsMutexUnlock(&debug_init_mutex
);
461 * Format the message...
464 gettimeofday(&curtime
, NULL
);
465 snprintf(buffer
, sizeof(buffer
), "T%03d %02d:%02d:%02d.%03d ",
466 debug_thread_id(), (int)((curtime
.tv_sec
/ 3600) % 24),
467 (int)((curtime
.tv_sec
/ 60) % 60),
468 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
470 va_start(ap
, format
);
471 bytes
= debug_vsnprintf(buffer
+ 19, sizeof(buffer
) - 20, format
, ap
) + 19;
474 if (bytes
>= (sizeof(buffer
) - 1))
476 buffer
[sizeof(buffer
) - 2] = '\n';
477 bytes
= sizeof(buffer
) - 1;
479 else if (buffer
[bytes
- 1] != '\n')
481 buffer
[bytes
++] = '\n';
482 buffer
[bytes
] = '\0';
489 _cupsMutexLock(&debug_log_mutex
);
490 write(_cups_debug_fd
, buffer
, bytes
);
491 _cupsMutexUnlock(&debug_log_mutex
);
496 * '_cups_debug_puts()' - Write a single line to the log.
500 _cups_debug_puts(const char *s
) /* I - String to output */
502 struct timeval curtime
; /* Current time */
503 char buffer
[2048]; /* Output buffer */
504 size_t bytes
; /* Number of bytes in buffer */
505 int level
; /* Log level in message */
509 * See if we need to do any logging...
513 _cups_debug_set(getenv("CUPS_DEBUG_LOG"), getenv("CUPS_DEBUG_LEVEL"),
514 getenv("CUPS_DEBUG_FILTER"), 0);
516 if (_cups_debug_fd
< 0)
520 * Filter as needed...
528 if (level
> _cups_debug_level
)
533 int result
; /* Filter result */
535 _cupsMutexLock(&debug_init_mutex
);
536 result
= regexec(debug_filter
, s
, 0, NULL
, 0);
537 _cupsMutexUnlock(&debug_init_mutex
);
544 * Format the message...
547 gettimeofday(&curtime
, NULL
);
548 bytes
= snprintf(buffer
, sizeof(buffer
), "T%03d %02d:%02d:%02d.%03d %s",
549 debug_thread_id(), (int)((curtime
.tv_sec
/ 3600) % 24),
550 (int)((curtime
.tv_sec
/ 60) % 60),
551 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000),
554 if (bytes
>= (sizeof(buffer
) - 1))
556 buffer
[sizeof(buffer
) - 2] = '\n';
557 bytes
= sizeof(buffer
) - 1;
559 else if (buffer
[bytes
- 1] != '\n')
561 buffer
[bytes
++] = '\n';
562 buffer
[bytes
] = '\0';
569 _cupsMutexLock(&debug_log_mutex
);
570 write(_cups_debug_fd
, buffer
, bytes
);
571 _cupsMutexUnlock(&debug_log_mutex
);
576 * '_cups_debug_set()' - Enable or disable debug logging.
580 _cups_debug_set(const char *logfile
, /* I - Log file or NULL */
581 const char *level
, /* I - Log level or NULL */
582 const char *filter
, /* I - Filter string or NULL */
583 int force
) /* I - Force initialization */
585 _cupsMutexLock(&debug_init_mutex
);
587 if (!debug_init
|| force
)
590 * Restore debug settings to defaults...
593 if (_cups_debug_fd
!= -1)
595 close(_cups_debug_fd
);
601 regfree((regex_t
*)debug_filter
);
605 _cups_debug_level
= 1;
608 * Open logs, set log levels, etc.
613 else if (!strcmp(logfile
, "-"))
617 char buffer
[1024]; /* Filename buffer */
619 snprintf(buffer
, sizeof(buffer
), logfile
, getpid());
621 if (buffer
[0] == '+')
622 _cups_debug_fd
= open(buffer
+ 1, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
624 _cups_debug_fd
= open(buffer
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0644);
628 _cups_debug_level
= atoi(level
);
632 if ((debug_filter
= (regex_t
*)calloc(1, sizeof(regex_t
))) == NULL
)
633 fputs("Unable to allocate memory for CUPS_DEBUG_FILTER - results not "
634 "filtered!\n", stderr
);
635 else if (regcomp(debug_filter
, filter
, REG_EXTENDED
))
637 fputs("Bad regular expression in CUPS_DEBUG_FILTER - results not "
638 "filtered!\n", stderr
);
647 _cupsMutexUnlock(&debug_init_mutex
);