]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/statbuf.c
2 * "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $"
4 * Status buffer routines for the Common UNIX Printing System (CUPS)
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * cupsdStatBufDelete() - Destroy a status buffer.
19 * cupsdStatBufNew() - Create a new status buffer.
20 * cupsdStatBufUpdate() - Update the status buffer.
24 * Include necessary headers...
32 * 'cupsdStatBufDelete()' - Destroy a status buffer.
36 cupsdStatBufDelete(cupsd_statbuf_t
*sb
) /* I - Status buffer */
39 * Range check input...
46 * Close the status pipe and free memory used...
56 * 'cupsdStatBufNew()' - Create a new status buffer.
59 cupsd_statbuf_t
* /* O - New status buffer */
60 cupsdStatBufNew(int fd
, /* I - File descriptor of pipe */
61 const char *prefix
, /* I - Printf-style prefix string */
62 ...) /* I - Additional args as needed */
64 cupsd_statbuf_t
*sb
; /* New status buffer */
65 va_list ap
; /* Argument list */
69 * Range check input...
76 * Allocate the status buffer...
79 if ((sb
= calloc(1, sizeof(cupsd_statbuf_t
))) != NULL
)
82 * Assign the file descriptor...
88 * Format the prefix string, if any. This is usually "[Job 123]"
89 * or "[Sub 123]", and so forth.
95 * Printf-style prefix string...
99 vsnprintf(sb
->prefix
, sizeof(sb
->prefix
), prefix
, ap
);
105 * No prefix string...
108 sb
->prefix
[0] = '\0';
117 * 'cupsdStatBufUpdate()' - Update the status buffer.
120 char * /* O - Line from buffer, "", or NULL */
122 cupsd_statbuf_t
*sb
, /* I - Status buffer */
123 int *loglevel
, /* O - Log level */
124 char *line
, /* I - Line buffer */
125 int linelen
) /* I - Size of line buffer */
127 int bytes
; /* Number of bytes read */
128 char *lineptr
, /* Pointer to end of line in buffer */
129 *message
; /* Pointer to message text */
133 * Check if the buffer already contains a full line...
136 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
)
139 * No, read more data...
142 if ((bytes
= read(sb
->fd
, sb
->buffer
+ sb
->bufused
,
143 CUPSD_SB_BUFFER_SIZE
- sb
->bufused
- 1)) > 0)
145 sb
->bufused
+= bytes
;
146 sb
->buffer
[sb
->bufused
] = '\0';
149 * Guard against a line longer than the max buffer size...
152 if ((lineptr
= strchr(sb
->buffer
, '\n')) == NULL
&&
153 sb
->bufused
== (CUPSD_SB_BUFFER_SIZE
- 1))
154 lineptr
= sb
->buffer
+ sb
->bufused
;
156 else if (bytes
< 0 && errno
== EINTR
)
159 * Return an empty line if we are interrupted...
162 *loglevel
= CUPSD_LOG_NONE
;
170 * End-of-file, so use the whole buffer...
173 lineptr
= sb
->buffer
+ sb
->bufused
;
178 * Final check for end-of-file...
181 if (sb
->bufused
== 0 && bytes
== 0)
191 *loglevel
= CUPSD_LOG_NONE
;
198 * Terminate the line and process it...
204 * Figure out the logging level...
207 if (!strncmp(sb
->buffer
, "EMERG:", 6))
209 *loglevel
= CUPSD_LOG_EMERG
;
210 message
= sb
->buffer
+ 6;
212 else if (!strncmp(sb
->buffer
, "ALERT:", 6))
214 *loglevel
= CUPSD_LOG_ALERT
;
215 message
= sb
->buffer
+ 6;
217 else if (!strncmp(sb
->buffer
, "CRIT:", 5))
219 *loglevel
= CUPSD_LOG_CRIT
;
220 message
= sb
->buffer
+ 5;
222 else if (!strncmp(sb
->buffer
, "ERROR:", 6))
224 *loglevel
= CUPSD_LOG_ERROR
;
225 message
= sb
->buffer
+ 6;
227 else if (!strncmp(sb
->buffer
, "WARNING:", 8))
229 *loglevel
= CUPSD_LOG_WARN
;
230 message
= sb
->buffer
+ 8;
232 else if (!strncmp(sb
->buffer
, "NOTICE:", 7))
234 *loglevel
= CUPSD_LOG_NOTICE
;
235 message
= sb
->buffer
+ 7;
237 else if (!strncmp(sb
->buffer
, "INFO:", 5))
239 *loglevel
= CUPSD_LOG_INFO
;
240 message
= sb
->buffer
+ 5;
242 else if (!strncmp(sb
->buffer
, "DEBUG:", 6))
244 *loglevel
= CUPSD_LOG_DEBUG
;
245 message
= sb
->buffer
+ 6;
247 else if (!strncmp(sb
->buffer
, "DEBUG2:", 7))
249 *loglevel
= CUPSD_LOG_DEBUG2
;
250 message
= sb
->buffer
+ 7;
252 else if (!strncmp(sb
->buffer
, "PAGE:", 5))
254 *loglevel
= CUPSD_LOG_PAGE
;
255 message
= sb
->buffer
+ 5;
257 else if (!strncmp(sb
->buffer
, "STATE:", 6))
259 *loglevel
= CUPSD_LOG_STATE
;
260 message
= sb
->buffer
+ 6;
262 else if (!strncmp(sb
->buffer
, "ATTR:", 5))
264 *loglevel
= CUPSD_LOG_ATTR
;
265 message
= sb
->buffer
+ 5;
267 else if (!strncmp(sb
->buffer
, "PPD:", 4))
269 *loglevel
= CUPSD_LOG_PPD
;
270 message
= sb
->buffer
+ 4;
274 *loglevel
= CUPSD_LOG_DEBUG
;
275 message
= sb
->buffer
;
279 * Skip leading whitespace in the message...
282 while (isspace(*message
& 255))
286 * Send it to the log file as needed...
291 if (*loglevel
> CUPSD_LOG_NONE
&&
292 (*loglevel
!= CUPSD_LOG_INFO
|| LogLevel
>= CUPSD_LOG_DEBUG
))
295 * General status message; send it to the error_log file...
298 if (message
[0] == '[')
299 cupsdLogMessage(*loglevel
, "%s", message
);
301 cupsdLogMessage(*loglevel
, "%s %s", sb
->prefix
, message
);
303 else if (*loglevel
< CUPSD_LOG_NONE
&& LogLevel
>= CUPSD_LOG_DEBUG
)
304 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "%s %s", sb
->prefix
, sb
->buffer
);
308 * Copy the message to the line buffer...
311 strlcpy(line
, message
, linelen
);
314 * Copy over the buffer data we've used up...
317 if (lineptr
< sb
->buffer
+ sb
->bufused
)
318 _cups_strcpy(sb
->buffer
, lineptr
);
320 sb
->bufused
-= lineptr
- sb
->buffer
;
330 * End of "$Id: statbuf.c 7674 2008-06-18 23:18:32Z mike $".