]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/debug.c
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...
35 * 'debug_vsnprintf()' - Format a string into a fixed size buffer.
38 int /* O - Number of bytes formatted */
39 debug_vsnprintf(char *buffer
, /* O - Output buffer */
40 size_t bufsize
, /* O - Size of output buffer */
41 const char *format
, /* I - printf-style format string */
42 va_list ap
) /* I - Pointer to additional arguments */
44 char *bufptr
, /* Pointer to position in buffer */
45 *bufend
, /* Pointer to end of buffer */
46 sign
, /* Sign of format width */
47 size
, /* Size character (h, l, L) */
48 type
; /* Format type character */
49 int width
, /* Width of field */
50 prec
; /* Number of characters of precision */
51 char tformat
[100], /* Temporary format string for sprintf() */
52 *tptr
, /* Pointer into temporary format */
53 temp
[1024]; /* Buffer for formatted numbers */
54 char *s
; /* Pointer to string */
55 int bytes
; /* Total number of bytes needed */
59 * Loop through the format string, formatting as needed...
63 bufend
= buffer
+ bufsize
- 1;
75 if (bufptr
&& bufptr
< bufend
) *bufptr
++ = *format
;
80 else if (strchr(" -+#\'", *format
))
91 * Get width from argument...
95 width
= va_arg(ap
, int);
97 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
104 while (isdigit(*format
& 255))
106 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
109 width
= width
* 10 + *format
++ - '0';
115 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
123 * Get precision from argument...
127 prec
= va_arg(ap
, int);
129 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
130 tptr
+= strlen(tptr
);
136 while (isdigit(*format
& 255))
138 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
141 prec
= prec
* 10 + *format
++ - '0';
148 if (*format
== 'l' && format
[1] == 'l')
152 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
160 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
162 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
171 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
179 case 'E' : /* Floating point formats */
184 if ((width
+ 2) > sizeof(temp
))
187 sprintf(temp
, tformat
, va_arg(ap
, double));
189 bytes
+= (int)strlen(temp
);
193 if ((bufptr
+ strlen(temp
)) > bufend
)
195 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
200 strcpy(bufptr
, temp
);
201 bufptr
+= strlen(temp
);
206 case 'B' : /* Integer formats */
214 if ((width
+ 2) > sizeof(temp
))
217 #ifdef HAVE_LONG_LONG
219 sprintf(temp
, tformat
, va_arg(ap
, long long));
221 #endif /* HAVE_LONG_LONG */
223 sprintf(temp
, tformat
, va_arg(ap
, long));
225 sprintf(temp
, tformat
, va_arg(ap
, int));
227 bytes
+= (int)strlen(temp
);
231 if ((bufptr
+ strlen(temp
)) > bufend
)
233 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
238 strcpy(bufptr
, temp
);
239 bufptr
+= strlen(temp
);
244 case 'p' : /* Pointer value */
245 if ((width
+ 2) > sizeof(temp
))
248 sprintf(temp
, tformat
, va_arg(ap
, void *));
250 bytes
+= (int)strlen(temp
);
254 if ((bufptr
+ strlen(temp
)) > bufend
)
256 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
261 strcpy(bufptr
, temp
);
262 bufptr
+= strlen(temp
);
267 case 'c' : /* Character or character array */
273 *bufptr
++ = va_arg(ap
, int);
276 if ((bufptr
+ width
) > bufend
)
277 width
= (int)(bufend
- bufptr
);
279 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
285 case 's' : /* String */
286 if ((s
= va_arg(ap
, char *)) == NULL
)
290 * Copy the C string, replacing control chars and \ with
291 * C character escapes...
294 for (bufend
--; *s
&& bufptr
< bufend
; s
++)
326 else if ((*s
& 255) < ' ')
330 *bufptr
++ = '0' + *s
/ 8;
331 *bufptr
++ = '0' + (*s
& 7);
340 case 'n' : /* Output number of chars so far */
341 *(va_arg(ap
, int *)) = bytes
;
349 if (bufptr
&& bufptr
< bufend
)
357 * Nul-terminate the string and return the number of characters needed.
367 * '_cups_debug_printf()' - Write a formatted line to the log.
371 _cups_debug_printf(const char *format
, /* I - Printf-style format string */
372 ...) /* I - Additional arguments as needed */
374 va_list ap
; /* Pointer to arguments */
375 struct timeval curtime
; /* Current time */
376 char buffer
[2048]; /* Output buffer */
377 size_t bytes
; /* Number of bytes in buffer */
378 const char *cups_debug_log
;/* CUPS_DEBUG_LOG environment variable */
379 _cups_globals_t
*cg
= _cupsGlobals();
384 * See if we need to do any logging...
391 if ((cups_debug_log
= getenv("CUPS_DEBUG_LOG")) == NULL
)
393 else if (!strcmp(cups_debug_log
, "-"))
396 cg
->debug_fd
= open(cups_debug_log
, O_WRONLY
| O_APPEND
| O_CREAT
, 0644);
399 if (cg
->debug_fd
< 0)
403 * Format the message...
406 gettimeofday(&curtime
, NULL
);
407 snprintf(buffer
, sizeof(buffer
), "%02d:%02d:%02d.%03d ",
408 (int)((curtime
.tv_sec
/ 3600) % 24),
409 (int)((curtime
.tv_sec
/ 60) % 60),
410 (int)(curtime
.tv_sec
% 60), (int)(curtime
.tv_usec
/ 1000));
412 va_start(ap
, format
);
413 debug_vsnprintf(buffer
+ 13, sizeof(buffer
) - 14, format
, ap
);
416 bytes
= strlen(buffer
);
417 if (buffer
[bytes
- 1] != '\n')
419 buffer
[bytes
] = '\n';
421 buffer
[bytes
] = '\0';
428 write(cg
->debug_fd
, buffer
, bytes
);
433 * '_cups_debug_puts()' - Write a single line to the log.
437 _cups_debug_puts(const char *s
) /* I - String to output */
439 _cups_debug_printf("%s\n", s
);
443 #elif defined(__APPLE__)
444 /* Mac OS X needs these stubbed since we reference them in the libcups.exp file */
445 void _cups_debug_printf(const char *format
, ...) {}
446 void _cups_debug_puts(const char *s
) {}