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 * 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...
38 int _cups_debug_fd
= -1;
39 /* Debug log file descriptor */
47 static int debug_init
= 0; /* Did we initialize debugging? */
48 # ifdef HAVE_PTHREAD_H
49 static pthread_mutex_t debug_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 /* Mutex to control initialization */
51 # endif /* HAVE_PTHREAD_H */
55 * 'debug_vsnprintf()' - Format a string into a fixed size buffer.
58 int /* O - Number of bytes formatted */
59 debug_vsnprintf(char *buffer
, /* O - Output buffer */
60 size_t bufsize
, /* O - Size of output buffer */
61 const char *format
, /* I - printf-style format string */
62 va_list ap
) /* I - Pointer to additional arguments */
64 char *bufptr
, /* Pointer to position in buffer */
65 *bufend
, /* Pointer to end of buffer */
66 size
, /* Size character (h, l, L) */
67 type
; /* Format type character */
68 int width
, /* Width of field */
69 prec
; /* Number of characters of precision */
70 char tformat
[100], /* Temporary format string for sprintf() */
71 *tptr
, /* Pointer into temporary format */
72 temp
[1024]; /* Buffer for formatted numbers */
73 char *s
; /* Pointer to string */
74 int bytes
; /* Total number of bytes needed */
77 if (!buffer
|| bufsize
< 2 || !format
)
81 * Loop through the format string, formatting as needed...
85 bufend
= buffer
+ bufsize
- 1;
103 else if (strchr(" -+#\'", *format
))
109 * Get width from argument...
113 width
= va_arg(ap
, int);
115 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
116 tptr
+= strlen(tptr
);
122 while (isdigit(*format
& 255))
124 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
127 width
= width
* 10 + *format
++ - '0';
133 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
141 * Get precision from argument...
145 prec
= va_arg(ap
, int);
147 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
148 tptr
+= strlen(tptr
);
154 while (isdigit(*format
& 255))
156 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
159 prec
= prec
* 10 + *format
++ - '0';
164 if (*format
== 'l' && format
[1] == 'l')
168 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
176 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
178 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
189 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
197 case 'E' : /* Floating point formats */
202 if ((width
+ 2) > sizeof(temp
))
205 sprintf(temp
, tformat
, va_arg(ap
, double));
207 bytes
+= (int)strlen(temp
);
211 if ((bufptr
+ strlen(temp
)) > bufend
)
213 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
218 strcpy(bufptr
, temp
);
219 bufptr
+= strlen(temp
);
224 case 'B' : /* Integer formats */
232 if ((width
+ 2) > sizeof(temp
))
235 #ifdef HAVE_LONG_LONG
237 sprintf(temp
, tformat
, va_arg(ap
, long long));
239 #endif /* HAVE_LONG_LONG */
241 sprintf(temp
, tformat
, va_arg(ap
, long));
243 sprintf(temp
, tformat
, va_arg(ap
, int));
245 bytes
+= (int)strlen(temp
);
249 if ((bufptr
+ strlen(temp
)) > bufend
)
251 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
256 strcpy(bufptr
, temp
);
257 bufptr
+= strlen(temp
);
262 case 'p' : /* Pointer value */
263 if ((width
+ 2) > sizeof(temp
))
266 sprintf(temp
, tformat
, va_arg(ap
, void *));
268 bytes
+= (int)strlen(temp
);
272 if ((bufptr
+ strlen(temp
)) > bufend
)
274 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
279 strcpy(bufptr
, temp
);
280 bufptr
+= strlen(temp
);
285 case 'c' : /* Character or character array */
291 *bufptr
++ = va_arg(ap
, int);
294 if ((bufptr
+ width
) > bufend
)
295 width
= (int)(bufend
- bufptr
);
297 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
303 case 's' : /* String */
304 if ((s
= va_arg(ap
, char *)) == NULL
)
308 * Copy the C string, replacing control chars and \ with
309 * C character escapes...
312 for (bufend
--; *s
&& bufptr
< bufend
; s
++)
344 else if ((*s
& 255) < ' ')
348 *bufptr
++ = '0' + *s
/ 8;
349 *bufptr
++ = '0' + (*s
& 7);
358 case 'n' : /* Output number of chars so far */
359 *(va_arg(ap
, int *)) = bytes
;
375 * Nul-terminate the string and return the number of characters needed.
385 * '_cups_debug_printf()' - Write a formatted line to the log.
389 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
390 ...) /* I - Additional arguments as needed */
392 va_list ap
; /* Pointer to arguments */
393 struct timeval curtime
; /* Current time */
394 char buffer
[2048]; /* Output buffer */
395 size_t bytes
; /* Number of bytes in buffer */
396 const char *cups_debug_log
;/* CUPS_DEBUG_LOG environment variable */
400 * See if we need to do any logging...
406 * Get a lock on the debug initializer, then re-check in case another
407 * thread already did it...
410 pthread_mutex_lock(&debug_mutex
);
416 if ((cups_debug_log
= getenv("CUPS_DEBUG_LOG")) == NULL
)
418 else if (!strcmp(cups_debug_log
, "-"))
422 snprintf(buffer
, sizeof(buffer
), cups_debug_log
, getpid());
423 _cups_debug_fd
= open(buffer
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
427 pthread_mutex_unlock(&debug_mutex
);
430 if (_cups_debug_fd
< 0)
434 * Format the message...
437 gettimeofday(&curtime
, NULL
);
438 snprintf(buffer
, sizeof(buffer
), "%02d:%02d:%02d.%03d ",
439 (int)((curtime
.tv_sec
/ 3600) % 24),
440 (int)((curtime
.tv_sec
/ 60) % 60),
441 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
443 va_start(ap
, format
);
444 debug_vsnprintf(buffer
+ 13, sizeof(buffer
) - 14, format
, ap
);
447 bytes
= strlen(buffer
);
448 if (buffer
[bytes
- 1] != '\n')
450 buffer
[bytes
] = '\n';
452 buffer
[bytes
] = '\0';
459 write(_cups_debug_fd
, buffer
, bytes
);
464 * '_cups_debug_puts()' - Write a single line to the log.
468 _cups_debug_puts(const char *s
) /* I - String to output */
470 _cups_debug_printf("%s\n", s
);
474 #elif defined(__APPLE__)
475 /* Mac OS X needs these stubbed since we reference them in the libcups.exp file */
476 void _cups_debug_printf(const char *format
, ...) {}
477 void _cups_debug_puts(const char *s
) {}