4 * Debugging functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2008 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 * _cups_debug_printf() - Write a formatted line to the log.
19 * _cups_debug_puts() - Write a single line to the log.
23 * Include necessary headers...
37 int _cups_debug_fd
= -1;
38 /* Debug log file descriptor */
46 static int debug_init
= 0; /* Did we initialize debugging? */
47 # ifdef HAVE_PTHREAD_H
48 static pthread_mutex_t debug_mutex
= PTHREAD_MUTEX_INITIALIZER
;
49 /* Mutex to control initialization */
50 # endif /* HAVE_PTHREAD_H */
54 * 'debug_vsnprintf()' - Format a string into a fixed size buffer.
57 int /* O - Number of bytes formatted */
58 debug_vsnprintf(char *buffer
, /* O - Output buffer */
59 size_t bufsize
, /* O - Size of output buffer */
60 const char *format
, /* I - printf-style format string */
61 va_list ap
) /* I - Pointer to additional arguments */
63 char *bufptr
, /* Pointer to position in buffer */
64 *bufend
, /* Pointer to end of buffer */
65 size
, /* Size character (h, l, L) */
66 type
; /* Format type character */
67 int width
, /* Width of field */
68 prec
; /* Number of characters of precision */
69 char tformat
[100], /* Temporary format string for sprintf() */
70 *tptr
, /* Pointer into temporary format */
71 temp
[1024]; /* Buffer for formatted numbers */
72 char *s
; /* Pointer to string */
73 int bytes
; /* Total number of bytes needed */
76 if (!buffer
|| bufsize
< 2 || !format
)
80 * Loop through the format string, formatting as needed...
84 bufend
= buffer
+ bufsize
- 1;
102 else if (strchr(" -+#\'", *format
))
108 * Get width from argument...
112 width
= va_arg(ap
, int);
114 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
115 tptr
+= strlen(tptr
);
121 while (isdigit(*format
& 255))
123 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
126 width
= width
* 10 + *format
++ - '0';
132 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
140 * Get precision from argument...
144 prec
= va_arg(ap
, int);
146 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
147 tptr
+= strlen(tptr
);
153 while (isdigit(*format
& 255))
155 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
158 prec
= prec
* 10 + *format
++ - '0';
163 if (*format
== 'l' && format
[1] == 'l')
167 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
175 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
177 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
188 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
196 case 'E' : /* Floating point formats */
201 if ((width
+ 2) > sizeof(temp
))
204 sprintf(temp
, tformat
, va_arg(ap
, double));
206 bytes
+= (int)strlen(temp
);
210 if ((bufptr
+ strlen(temp
)) > bufend
)
212 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
217 strcpy(bufptr
, temp
);
218 bufptr
+= strlen(temp
);
223 case 'B' : /* Integer formats */
231 if ((width
+ 2) > sizeof(temp
))
234 #ifdef HAVE_LONG_LONG
236 sprintf(temp
, tformat
, va_arg(ap
, long long));
238 #endif /* HAVE_LONG_LONG */
240 sprintf(temp
, tformat
, va_arg(ap
, long));
242 sprintf(temp
, tformat
, va_arg(ap
, int));
244 bytes
+= (int)strlen(temp
);
248 if ((bufptr
+ strlen(temp
)) > bufend
)
250 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
255 strcpy(bufptr
, temp
);
256 bufptr
+= strlen(temp
);
261 case 'p' : /* Pointer value */
262 if ((width
+ 2) > sizeof(temp
))
265 sprintf(temp
, tformat
, va_arg(ap
, void *));
267 bytes
+= (int)strlen(temp
);
271 if ((bufptr
+ strlen(temp
)) > bufend
)
273 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
278 strcpy(bufptr
, temp
);
279 bufptr
+= strlen(temp
);
284 case 'c' : /* Character or character array */
290 *bufptr
++ = va_arg(ap
, int);
293 if ((bufptr
+ width
) > bufend
)
294 width
= (int)(bufend
- bufptr
);
296 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
302 case 's' : /* String */
303 if ((s
= va_arg(ap
, char *)) == NULL
)
307 * Copy the C string, replacing control chars and \ with
308 * C character escapes...
311 for (bufend
--; *s
&& bufptr
< bufend
; s
++)
343 else if ((*s
& 255) < ' ')
347 *bufptr
++ = '0' + *s
/ 8;
348 *bufptr
++ = '0' + (*s
& 7);
357 case 'n' : /* Output number of chars so far */
358 *(va_arg(ap
, int *)) = bytes
;
374 * Nul-terminate the string and return the number of characters needed.
384 * '_cups_debug_printf()' - Write a formatted line to the log.
388 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
389 ...) /* I - Additional arguments as needed */
391 va_list ap
; /* Pointer to arguments */
392 struct timeval curtime
; /* Current time */
393 char buffer
[2048]; /* Output buffer */
394 size_t bytes
; /* Number of bytes in buffer */
395 const char *cups_debug_log
;/* CUPS_DEBUG_LOG environment variable */
399 * See if we need to do any logging...
405 * Get a lock on the debug initializer, then re-check in case another
406 * thread already did it...
409 pthread_mutex_lock(&debug_mutex
);
415 if ((cups_debug_log
= getenv("CUPS_DEBUG_LOG")) == NULL
)
417 else if (!strcmp(cups_debug_log
, "-"))
421 snprintf(buffer
, sizeof(buffer
), cups_debug_log
, getpid());
422 _cups_debug_fd
= open(buffer
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
426 pthread_mutex_unlock(&debug_mutex
);
429 if (_cups_debug_fd
< 0)
433 * Format the message...
436 gettimeofday(&curtime
, NULL
);
437 snprintf(buffer
, sizeof(buffer
), "%02d:%02d:%02d.%03d ",
438 (int)((curtime
.tv_sec
/ 3600) % 24),
439 (int)((curtime
.tv_sec
/ 60) % 60),
440 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
442 va_start(ap
, format
);
443 debug_vsnprintf(buffer
+ 13, sizeof(buffer
) - 14, format
, ap
);
446 bytes
= strlen(buffer
);
447 if (buffer
[bytes
- 1] != '\n')
449 buffer
[bytes
] = '\n';
451 buffer
[bytes
] = '\0';
458 write(_cups_debug_fd
, buffer
, bytes
);
463 * '_cups_debug_puts()' - Write a single line to the log.
467 _cups_debug_puts(const char *s
) /* I - String to output */
469 _cups_debug_printf("%s\n", s
);
473 #elif defined(__APPLE__)
474 /* Mac OS X needs these stubbed since we reference them in the libcups.exp file */
475 void _cups_debug_printf(const char *format
, ...) {}
476 void _cups_debug_puts(const char *s
) {}