]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/log.c
Fix logging when "-t" is used (always to stderr).
[thirdparty/cups.git] / scheduler / log.c
CommitLineData
ef416fc2 1/*
996acce8 2 * Log file routines for the CUPS scheduler.
ef416fc2 3 *
75c86da7 4 * Copyright 2007-2016 by Apple Inc.
996acce8 5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 6 *
996acce8
MS
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
57b7b66b 11 * missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 12 */
13
14/*
15 * Include necessary headers...
16 */
17
18#include "cupsd.h"
19#include <stdarg.h>
0dc4a6bf
MS
20#ifdef HAVE_ASL_H
21# include <asl.h>
22#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
23# define SD_JOURNAL_SUPPRESS_LOCATION
24# include <systemd/sd-journal.h>
25#endif /* HAVE_ASL_H */
b423cd4c 26#include <syslog.h>
ef416fc2 27
28
0dc4a6bf
MS
29/*
30 * Constants for log keys from PWG 5110.3 (PWG Common Log Format)...
31 */
32
33#define PWG_DeviceUUID "DUU"
34#define PWG_Event "E"
35#define PWG_LogNaturalLanguage "NL"
36#define PWG_Status "S"
37#define PWG_ServiceURI "URI"
38#define PWG_UserHost "UH"
39#define PWG_UserName "UN"
40#define PWG_UserURI "UU"
41#define PWG_ServiceIsAcceptingJobs "IAJ"
42#define PWG_ServiceState "ST"
43#define PWG_ServiceStateReasons "SR"
44#define PWG_ServiceUUID "SUU"
45#define PWG_JobID "JID"
46#define PWG_JobUUID "JUU"
47#define PWG_JobImagesCompleted "JIM"
48#define PWG_JobImpressionsCompleted "JIC"
49#define PWG_JobDestinationURI "JD"
50#define PWG_JobState "JS"
51#define PWG_JobStateReasons "JR"
52#define PWG_JobAccountingID "JA"
53#define PWG_JobAcountingUserName "JAUN"
54#define PWG_JobAccountingUserURI "JAUU"
55
56
75bd9771
MS
57/*
58 * Local globals...
59 */
60
28a463e0
MS
61static _cups_mutex_t log_mutex = _CUPS_MUTEX_INITIALIZER;
62 /* Mutex for logging */
7e86f2f6 63static size_t log_linesize = 0; /* Size of line for output file */
75bd9771
MS
64static char *log_line = NULL; /* Line for output file */
65
0dc4a6bf
MS
66#ifdef HAVE_ASL_H
67static const int log_levels[] = /* ASL levels... */
68 {
69 ASL_LEVEL_EMERG,
70 ASL_LEVEL_EMERG,
71 ASL_LEVEL_ALERT,
72 ASL_LEVEL_CRIT,
73 ASL_LEVEL_ERR,
74 ASL_LEVEL_WARNING,
75 ASL_LEVEL_NOTICE,
76 ASL_LEVEL_INFO,
77 ASL_LEVEL_DEBUG,
78 ASL_LEVEL_DEBUG
79 };
80#elif defined(HAVE_VSYSLOG) || defined(HAVE_SYSTEMD_SD_JOURNAL_H)
81static const int log_levels[] = /* SYSLOG levels... */
cb7f98ee
MS
82 {
83 0,
84 LOG_EMERG,
85 LOG_ALERT,
86 LOG_CRIT,
87 LOG_ERR,
88 LOG_WARNING,
89 LOG_NOTICE,
90 LOG_INFO,
91 LOG_DEBUG,
92 LOG_DEBUG
93 };
0dc4a6bf 94#endif /* HAVE_ASL_H */
cb7f98ee 95
75bd9771 96
ef416fc2 97/*
98 * Local functions...
99 */
100
005dd1eb 101static int format_log_line(const char *message, va_list ap);
ef416fc2 102
103
22c9029b
MS
104/*
105 * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it.
106 */
107
108int /* O - 1 if log file open */
109cupsdCheckLogFile(cups_file_t **lf, /* IO - Log file */
110 const char *logname) /* I - Log filename */
111{
112 char backname[1024], /* Backup log filename */
113 filename[1024], /* Formatted log filename */
114 *ptr; /* Pointer into filename */
115 const char *logptr; /* Pointer into log filename */
116
117
118 /*
119 * See if we have a log file to check...
120 */
121
122 if (!lf || !logname || !logname[0])
123 return (1);
124
a1797929
MS
125 /*
126 * Handle logging to stderr...
127 */
128
129 if (!strcmp(logname, "stderr"))
130 {
131 *lf = LogStderr;
132 return (1);
133 }
134
22c9029b
MS
135 /*
136 * Format the filename as needed...
137 */
138
139 if (!*lf ||
140 (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
141 MaxLogSize > 0))
142 {
143 /*
144 * Handle format strings...
145 */
146
147 filename[sizeof(filename) - 1] = '\0';
148
149 if (logname[0] != '/')
150 {
151 strlcpy(filename, ServerRoot, sizeof(filename));
152 strlcat(filename, "/", sizeof(filename));
153 }
154 else
155 filename[0] = '\0';
156
157 for (logptr = logname, ptr = filename + strlen(filename);
158 *logptr && ptr < (filename + sizeof(filename) - 1);
159 logptr ++)
160 if (*logptr == '%')
161 {
162 /*
163 * Format spec...
164 */
165
166 logptr ++;
167 if (*logptr == 's')
168 {
169 /*
170 * Insert the server name...
171 */
172
7e86f2f6 173 strlcpy(ptr, ServerName, sizeof(filename) - (size_t)(ptr - filename));
22c9029b
MS
174 ptr += strlen(ptr);
175 }
176 else
177 {
178 /*
179 * Otherwise just insert the character...
180 */
181
182 *ptr++ = *logptr;
183 }
184 }
185 else
186 *ptr++ = *logptr;
187
188 *ptr = '\0';
189 }
190
191 /*
192 * See if the log file is open...
193 */
194
195 if (!*lf)
196 {
197 /*
198 * Nope, open the log file...
199 */
200
201 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
202 {
203 /*
204 * If the file is in CUPS_LOGDIR then try to create a missing directory...
205 */
206
207 if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
208 {
209 /*
210 * Try updating the permissions of the containing log directory, using
211 * the log file permissions as a basis...
212 */
213
7e86f2f6 214 mode_t log_dir_perm = (mode_t)(0300 | LogFilePerm);
22c9029b
MS
215 /* LogFilePerm + owner write/search */
216 if (log_dir_perm & 0040)
217 log_dir_perm |= 0010; /* Add group search */
218 if (log_dir_perm & 0004)
219 log_dir_perm |= 0001; /* Add other search */
220
7e86f2f6 221 cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group, 1, -1);
22c9029b
MS
222
223 *lf = cupsFileOpen(filename, "a");
224 }
225
226 if (*lf == NULL)
227 {
250bb8a9 228#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
0dc4a6bf 229 sd_journal_print(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
0dc4a6bf
MS
230#else
231 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
250bb8a9 232#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
22c9029b
MS
233
234 if (FatalErrors & CUPSD_FATAL_LOG)
235 cupsdEndProcess(getpid(), 0);
236
237 return (0);
238 }
239 }
240
241 if (strncmp(filename, "/dev/", 5))
242 {
243 /*
244 * Change ownership and permissions of non-device logs...
245 */
246
247 fchown(cupsFileNumber(*lf), RunUser, Group);
248 fchmod(cupsFileNumber(*lf), LogFilePerm);
249 }
250 }
251
252 /*
253 * Do we need to rotate the log?
254 */
255
256 if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
257 MaxLogSize > 0)
258 {
259 /*
260 * Rotate log file...
261 */
262
263 cupsFileClose(*lf);
264
5a9febac 265 strlcpy(backname, filename, sizeof(backname));
22c9029b
MS
266 strlcat(backname, ".O", sizeof(backname));
267
268 unlink(backname);
269 rename(filename, backname);
270
271 if ((*lf = cupsFileOpen(filename, "a")) == NULL)
272 {
250bb8a9
MS
273#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
274 sd_journal_print(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
0dc4a6bf
MS
275
276#else
250bb8a9
MS
277 syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
278#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
22c9029b
MS
279
280 if (FatalErrors & CUPSD_FATAL_LOG)
281 cupsdEndProcess(getpid(), 0);
282
283 return (0);
284 }
285
286 /*
287 * Change ownership and permissions of non-device logs...
288 */
289
290 fchown(cupsFileNumber(*lf), RunUser, Group);
291 fchmod(cupsFileNumber(*lf), LogFilePerm);
292 }
293
294 return (1);
295}
296
297
ef416fc2 298/*
299 * 'cupsdGetDateTime()' - Returns a pointer to a date/time string.
300 */
301
302char * /* O - Date/time string */
dfd5680b
MS
303cupsdGetDateTime(struct timeval *t, /* I - Time value or NULL for current */
304 cupsd_time_t format) /* I - Format to use */
ef416fc2 305{
dfd5680b
MS
306 struct timeval curtime; /* Current time value */
307 struct tm *date; /* Date/time value */
308 static struct timeval last_time = { 0, 0 };
309 /* Last time we formatted */
310 static char s[1024]; /* Date/time string */
ef416fc2 311 static const char * const months[12] =/* Months */
312 {
313 "Jan",
314 "Feb",
315 "Mar",
316 "Apr",
317 "May",
318 "Jun",
319 "Jul",
320 "Aug",
321 "Sep",
322 "Oct",
323 "Nov",
324 "Dec"
325 };
326
327
839a51c8
MS
328 /*
329 * Make sure we have a valid time...
330 */
331
332 if (!t)
dfd5680b
MS
333 {
334 gettimeofday(&curtime, NULL);
335 t = &curtime;
336 }
839a51c8 337
dfd5680b
MS
338 if (t->tv_sec != last_time.tv_sec ||
339 (LogTimeFormat == CUPSD_TIME_USECS && t->tv_usec != last_time.tv_usec))
ef416fc2 340 {
dfd5680b 341 last_time = *t;
ef416fc2 342
343 /*
344 * Get the date and time from the UNIX time value, and then format it
345 * into a string. Note that we *can't* use the strftime() function since
346 * it is localized and will seriously confuse automatic programs if the
347 * month names are in the wrong language!
348 *
349 * Also, we use the "timezone" variable that contains the current timezone
350 * offset from GMT in seconds so that we are reporting local time in the
351 * log files. If you want GMT, set the TZ environment variable accordingly
352 * before starting the scheduler.
353 *
354 * (*BSD and Darwin store the timezone offset in the tm structure)
355 */
356
dfd5680b 357 date = localtime(&(t->tv_sec));
ef416fc2 358
dfd5680b
MS
359 if (format == CUPSD_TIME_STANDARD)
360 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
361 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
362 date->tm_hour, date->tm_min, date->tm_sec,
363#ifdef HAVE_TM_GMTOFF
364 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
365#else
366 timezone / 3600, (timezone / 60) % 60);
367#endif /* HAVE_TM_GMTOFF */
368 else
369 snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]",
370 date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
bf3816c7 371 date->tm_hour, date->tm_min, date->tm_sec, (int)t->tv_usec,
ef416fc2 372#ifdef HAVE_TM_GMTOFF
dfd5680b 373 date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
ef416fc2 374#else
dfd5680b 375 timezone / 3600, (timezone / 60) % 60);
ef416fc2 376#endif /* HAVE_TM_GMTOFF */
377 }
378
379 return (s);
380}
381
382
22c9029b
MS
383/*
384 * 'cupsdLogFCMessage()' - Log a file checking message.
385 */
386
387void
388cupsdLogFCMessage(
389 void *context, /* I - Printer (if any) */
390 _cups_fc_result_t result, /* I - Check result */
391 const char *message) /* I - Message to log */
392{
393 cupsd_printer_t *p = (cupsd_printer_t *)context;
394 /* Printer */
395 cupsd_loglevel_t level; /* Log level */
396
397
398 if (result == _CUPS_FILE_CHECK_OK)
399 level = CUPSD_LOG_DEBUG2;
400 else
401 level = CUPSD_LOG_ERROR;
402
403 if (p)
404 {
405 cupsdLogMessage(level, "%s: %s", p->name, message);
406
407 if (result == _CUPS_FILE_CHECK_MISSING ||
408 result == _CUPS_FILE_CHECK_WRONG_TYPE)
409 {
410 strlcpy(p->state_message, message, sizeof(p->state_message));
411
412 if (cupsdSetPrinterReasons(p, "+cups-missing-filter-warning"))
413 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message);
414 }
88f9aafc
MS
415 else if (result == _CUPS_FILE_CHECK_PERMISSIONS ||
416 result == _CUPS_FILE_CHECK_RELATIVE_PATH)
22c9029b
MS
417 {
418 strlcpy(p->state_message, message, sizeof(p->state_message));
419
420 if (cupsdSetPrinterReasons(p, "+cups-insecure-filter-warning"))
421 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message);
422 }
423 }
424 else
425 cupsdLogMessage(level, "%s", message);
426}
427
428
f7deaa1a 429#ifdef HAVE_GSSAPI
430/*
431 * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
432 */
433
434int /* O - 1 on success, 0 on error */
435cupsdLogGSSMessage(
436 int level, /* I - Log level */
7e86f2f6
MS
437 OM_uint32 major_status, /* I - Major GSSAPI status */
438 OM_uint32 minor_status, /* I - Minor GSSAPI status */
f7deaa1a 439 const char *message, /* I - printf-style message string */
440 ...) /* I - Additional args as needed */
441{
442 OM_uint32 err_major_status, /* Major status code for display */
443 err_minor_status; /* Minor status code for display */
444 OM_uint32 msg_ctx; /* Message context */
445 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
446 /* Major status message */
447 minor_status_string = GSS_C_EMPTY_BUFFER;
448 /* Minor status message */
449 int ret; /* Return value */
dcb445bc 450 char buffer[8192]; /* Buffer for vsnprintf */
f7deaa1a 451
452
dcb445bc
MS
453 if (strchr(message, '%'))
454 {
455 /*
456 * Format the message string...
457 */
458
459 va_list ap; /* Pointer to arguments */
460
461 va_start(ap, message);
462 vsnprintf(buffer, sizeof(buffer), message, ap);
463 va_end(ap);
464
465 message = buffer;
466 }
467
f7deaa1a 468 msg_ctx = 0;
469 err_major_status = gss_display_status(&err_minor_status,
470 major_status,
471 GSS_C_GSS_CODE,
472 GSS_C_NO_OID,
473 &msg_ctx,
474 &major_status_string);
475
476 if (!GSS_ERROR(err_major_status))
1f0275e3
MS
477 gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE,
478 GSS_C_NULL_OID, &msg_ctx, &minor_status_string);
f7deaa1a 479
480 ret = cupsdLogMessage(level, "%s: %s, %s", message,
481 (char *)major_status_string.value,
482 (char *)minor_status_string.value);
483 gss_release_buffer(&err_minor_status, &major_status_string);
484 gss_release_buffer(&err_minor_status, &minor_status_string);
485
486 return (ret);
487}
488#endif /* HAVE_GSSAPI */
489
490
996acce8
MS
491/*
492 * 'cupsdLogClient()' - Log a client message.
493 */
494
495int /* O - 1 on success, 0 on error */
496cupsdLogClient(cupsd_client_t *con, /* I - Client connection */
497 int level, /* I - Log level */
498 const char *message, /* I - Printf-style message string */
499 ...) /* I - Additional arguments as needed */
500{
501 va_list ap, ap2; /* Argument pointers */
502 char clientmsg[1024];/* Format string for client message */
503 int status; /* Formatting status */
504
505
506 /*
507 * See if we want to log this message...
508 */
509
510 if (TestConfigFile || !ErrorLog)
511 return (1);
512
513 if (level > LogLevel)
514 return (1);
515
516 /*
517 * Format and write the log message...
518 */
519
520 if (con)
521 snprintf(clientmsg, sizeof(clientmsg), "[Client %d] %s", con->number,
522 message);
523 else
524 strlcpy(clientmsg, message, sizeof(clientmsg));
525
526 va_start(ap, message);
527
528 do
529 {
530 va_copy(ap2, ap);
531 status = format_log_line(clientmsg, ap2);
532 va_end(ap2);
533 }
534 while (status == 0);
535
536 va_end(ap);
537
538 if (status > 0)
539 return (cupsdWriteErrorLog(level, log_line));
540 else
541 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
542 "Unable to allocate memory for log line."));
543}
544
545
ef416fc2 546/*
75bd9771 547 * 'cupsdLogJob()' - Log a job message.
ef416fc2 548 */
549
550int /* O - 1 on success, 0 on error */
75bd9771
MS
551cupsdLogJob(cupsd_job_t *job, /* I - Job */
552 int level, /* I - Log level */
553 const char *message, /* I - Printf-style message string */
554 ...) /* I - Additional arguments as needed */
ef416fc2 555{
dcb445bc 556 va_list ap, ap2; /* Argument pointers */
005dd1eb
MS
557 char jobmsg[1024]; /* Format string for job message */
558 int status; /* Formatting status */
ef416fc2 559
560
561 /*
562 * See if we want to log this message...
563 */
564
94da7e34
MS
565 if (TestConfigFile || !ErrorLog)
566 return (1);
567
75c86da7 568 if (level > LogLevel && LogDebugHistory <= 0)
2e4ff8af 569 return (1);
2e4ff8af 570
250bb8a9 571#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
0dc4a6bf
MS
572 if (!strcmp(ErrorLog, "syslog"))
573 {
f495e905 574 cupsd_printer_t *printer = job ? (job->printer ? job->printer : (job->dest ? cupsdFindDest(job->dest) : NULL)) : NULL;
0dc4a6bf
MS
575 static const char * const job_states[] =
576 { /* job-state strings */
577 "Pending",
578 "PendingHeld",
579 "Processing",
580 "ProcessingStopped",
581 "Canceled",
582 "Aborted",
583 "Completed"
584 };
585
586 va_start(ap, message);
587
588 do
589 {
590 va_copy(ap2, ap);
591 status = format_log_line(message, ap2);
592 va_end(ap2);
593 }
594 while (status == 0);
595
596 va_end(ap);
597
7c91f59a
MS
598 if (job)
599 sd_journal_send("MESSAGE=%s", log_line,
600 "PRIORITY=%i", log_levels[level],
601 PWG_Event"=JobStateChanged",
602 PWG_ServiceURI"=%s", printer ? printer->uri : "",
603 PWG_JobID"=%d", job->id,
69223177 604 PWG_JobState"=%s", job->state_value < IPP_JSTATE_PENDING ? "" : job_states[job->state_value - IPP_JSTATE_PENDING],
7c91f59a
MS
605 PWG_JobImpressionsCompleted"=%d", ippGetInteger(job->impressions, 0),
606 NULL);
607 else
608 sd_journal_send("MESSAGE=%s", log_line,
609 "PRIORITY=%i", log_levels[level],
610 NULL);
611
0dc4a6bf
MS
612 return (1);
613 }
250bb8a9 614#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
0dc4a6bf 615
ef416fc2 616 /*
75bd9771 617 * Format and write the log message...
ef416fc2 618 */
619
dcb445bc
MS
620 if (job)
621 snprintf(jobmsg, sizeof(jobmsg), "[Job %d] %s", job->id, message);
622 else
623 strlcpy(jobmsg, message, sizeof(jobmsg));
624
625 va_start(ap, message);
ef416fc2 626
005dd1eb
MS
627 do
628 {
dcb445bc
MS
629 va_copy(ap2, ap);
630 status = format_log_line(jobmsg, ap2);
631 va_end(ap2);
005dd1eb
MS
632 }
633 while (status == 0);
771bd8cb 634
dcb445bc
MS
635 va_end(ap);
636
005dd1eb 637 if (status > 0)
178cb736 638 {
75c86da7 639 if (job && level > LogLevel && LogDebugHistory > 0)
178cb736
MS
640 {
641 /*
642 * Add message to the job history...
643 */
644
645 cupsd_joblog_t *temp; /* Copy of log message */
ff2b08f9
MS
646 size_t log_len = strlen(log_line);
647 /* Length of log message */
178cb736 648
ff2b08f9 649 if ((temp = malloc(sizeof(cupsd_joblog_t) + log_len)) != NULL)
178cb736
MS
650 {
651 temp->time = time(NULL);
ff2b08f9 652 memcpy(temp->message, log_line, log_len + 1);
178cb736
MS
653 }
654
655 if (!job->history)
656 job->history = cupsArrayNew(NULL, NULL);
657
658 if (job->history && temp)
659 {
660 cupsArrayAdd(job->history, temp);
661
662 if (cupsArrayCount(job->history) > LogDebugHistory)
663 {
664 /*
665 * Remove excess messages...
666 */
667
668 temp = cupsArrayFirst(job->history);
669 cupsArrayRemove(job->history, temp);
670 free(temp);
671 }
672 }
673 else if (temp)
674 free(temp);
675
676 return (1);
677 }
75c86da7 678 else if (level <= LogLevel)
178cb736
MS
679 return (cupsdWriteErrorLog(level, log_line));
680 else
681 return (1);
682 }
75bd9771
MS
683 else
684 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
996acce8 685 "Unable to allocate memory for log line."));
75bd9771 686}
ef416fc2 687
ef416fc2 688
75bd9771
MS
689/*
690 * 'cupsdLogMessage()' - Log a message to the error log file.
691 */
ef416fc2 692
75bd9771
MS
693int /* O - 1 on success, 0 on error */
694cupsdLogMessage(int level, /* I - Log level */
695 const char *message, /* I - printf-style message string */
696 ...) /* I - Additional args as needed */
697{
dcb445bc 698 va_list ap, ap2; /* Argument pointers */
005dd1eb 699 int status; /* Formatting status */
ef416fc2 700
ef416fc2 701
702 /*
75bd9771 703 * See if we want to log this message...
ef416fc2 704 */
705
9eb416ee
MS
706 if (TestConfigFile && level <= CUPSD_LOG_WARN)
707 {
708 va_start(ap, message);
709
710 vfprintf(stderr, message, ap);
711 putc('\n', stderr);
712
713 va_end(ap);
714
715 return (1);
716 }
717 else if (!ErrorLog && level <= CUPSD_LOG_WARN)
ef416fc2 718 {
a4845881 719 va_start(ap, message);
0dc4a6bf 720
250bb8a9 721#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
0dc4a6bf
MS
722 sd_journal_printv(log_levels[level], message, ap);
723
724#elif defined(HAVE_VSYSLOG)
725 vsyslog(LOG_LPR | log_levels[level], message, ap);
726
cb7f98ee 727#else
a4845881
MS
728 vfprintf(stderr, message, ap);
729 putc('\n', stderr);
250bb8a9 730#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
0dc4a6bf 731
a4845881 732 va_end(ap);
ef416fc2 733
75bd9771 734 return (1);
ef416fc2 735 }
9eb416ee 736 else if (level > LogLevel || !ErrorLog)
75bd9771 737 return (1);
ef416fc2 738
250bb8a9 739#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
9eb416ee 740 else if (!strcmp(ErrorLog, "syslog"))
a990ad13 741 {
67fe5048 742 va_start(ap, message);
a990ad13 743 sd_journal_printv(log_levels[level], message, ap);
67fe5048 744 va_end(ap);
a990ad13
MS
745 return (1);
746 }
250bb8a9 747#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
0dc4a6bf 748
ef416fc2 749 /*
75bd9771 750 * Format and write the log message...
ef416fc2 751 */
752
dcb445bc
MS
753 va_start(ap, message);
754
005dd1eb
MS
755 do
756 {
dcb445bc
MS
757 va_copy(ap2, ap);
758 status = format_log_line(message, ap2);
759 va_end(ap2);
005dd1eb
MS
760 }
761 while (status == 0);
ef416fc2 762
dcb445bc
MS
763 va_end(ap);
764
005dd1eb
MS
765 if (status > 0)
766 return (cupsdWriteErrorLog(level, log_line));
75bd9771
MS
767 else
768 return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
769 "Unable to allocate memory for log line!"));
ef416fc2 770}
771
772
773/*
774 * 'cupsdLogPage()' - Log a page to the page log file.
775 */
776
777int /* O - 1 on success, 0 on error */
778cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */
779 const char *page) /* I - Page being printed */
780{
01ce6322
MS
781 int i; /* Looping var */
782 char buffer[2048], /* Buffer for page log */
783 *bufptr, /* Pointer into buffer */
784 name[256]; /* Attribute name */
785 const char *format, /* Pointer into PageLogFormat */
786 *nameend; /* End of attribute name */
787 ipp_attribute_t *attr; /* Current attribute */
771bd8cb
MS
788 char number[256]; /* Page number */
789 int copies; /* Number of copies */
ef416fc2 790
791
01ce6322
MS
792 /*
793 * Format the line going into the page log...
794 */
795
796 if (!PageLogFormat)
797 return (1);
798
5a9febac 799 strlcpy(number, "1", sizeof(number));
771bd8cb
MS
800 copies = 1;
801 sscanf(page, "%255s%d", number, &copies);
01ce6322
MS
802
803 for (format = PageLogFormat, bufptr = buffer; *format; format ++)
804 {
805 if (*format == '%')
806 {
807 format ++;
808
809 switch (*format)
810 {
811 case '%' : /* Literal % */
812 if (bufptr < (buffer + sizeof(buffer) - 1))
813 *bufptr++ = '%';
814 break;
815
816 case 'p' : /* Printer name */
5252fc7a 817 strlcpy(bufptr, job->dest, sizeof(buffer) - (size_t)(bufptr - buffer));
01ce6322
MS
818 bufptr += strlen(bufptr);
819 break;
820
821 case 'j' : /* Job ID */
7e86f2f6 822 snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", job->id);
01ce6322
MS
823 bufptr += strlen(bufptr);
824 break;
825
826 case 'u' : /* Username */
7e86f2f6 827 strlcpy(bufptr, job->username ? job->username : "-", sizeof(buffer) - (size_t)(bufptr - buffer));
01ce6322
MS
828 bufptr += strlen(bufptr);
829 break;
830
831 case 'T' : /* Date and time */
7e86f2f6 832 strlcpy(bufptr, cupsdGetDateTime(NULL, LogTimeFormat), sizeof(buffer) - (size_t)(bufptr - buffer));
01ce6322
MS
833 bufptr += strlen(bufptr);
834 break;
835
836 case 'P' : /* Page number */
7e86f2f6 837 strlcpy(bufptr, number, sizeof(buffer) - (size_t)(bufptr - buffer));
01ce6322
MS
838 bufptr += strlen(bufptr);
839 break;
840
841 case 'C' : /* Number of copies */
7e86f2f6 842 snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", copies);
01ce6322
MS
843 bufptr += strlen(bufptr);
844 break;
845
846 case '{' : /* {attribute} */
7e86f2f6 847 if ((nameend = strchr(format, '}')) != NULL && (size_t)(nameend - format - 2) < (sizeof(name) - 1))
01ce6322
MS
848 {
849 /*
850 * Pull the name from inside the brackets...
851 */
852
07623986 853 memcpy(name, format + 1, (size_t)(nameend - format - 1));
75bd9771
MS
854 name[nameend - format - 1] = '\0';
855
856 format = nameend;
01ce6322 857
bf1bc4c6
MS
858 attr = ippFindAttribute(job->attrs, name, IPP_TAG_ZERO);
859 if (!attr && !strcmp(name, "job-billing"))
860 {
861 /*
862 * Handle alias "job-account-id" (which was standardized after
863 * "job-billing" was defined for CUPS...
864 */
865
866 attr = ippFindAttribute(job->attrs, "job-account-id", IPP_TAG_ZERO);
867 }
868 else if (!attr && !strcmp(name, "media"))
869 {
870 /*
871 * Handle alias "media-col" which uses dimensions instead of
872 * names...
873 */
874
875 attr = ippFindAttribute(job->attrs, "media-col/media-size", IPP_TAG_BEGIN_COLLECTION);
876 }
877
878 if (attr)
01ce6322
MS
879 {
880 /*
881 * Add the attribute value...
882 */
883
01ce6322
MS
884 for (i = 0;
885 i < attr->num_values &&
886 bufptr < (buffer + sizeof(buffer) - 1);
887 i ++)
888 {
889 if (i)
890 *bufptr++ = ',';
891
892 switch (attr->value_tag)
893 {
894 case IPP_TAG_INTEGER :
895 case IPP_TAG_ENUM :
7e86f2f6 896 snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", attr->values[i].integer);
01ce6322
MS
897 bufptr += strlen(bufptr);
898 break;
899
900 case IPP_TAG_BOOLEAN :
7e86f2f6 901 snprintf(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer), "%d", attr->values[i].boolean);
01ce6322
MS
902 bufptr += strlen(bufptr);
903 break;
904
905 case IPP_TAG_TEXTLANG :
906 case IPP_TAG_NAMELANG :
907 case IPP_TAG_TEXT :
908 case IPP_TAG_NAME :
909 case IPP_TAG_KEYWORD :
910 case IPP_TAG_URI :
911 case IPP_TAG_URISCHEME :
912 case IPP_TAG_CHARSET :
913 case IPP_TAG_LANGUAGE :
914 case IPP_TAG_MIMETYPE :
7e86f2f6 915 strlcpy(bufptr, attr->values[i].string.text, sizeof(buffer) - (size_t)(bufptr - buffer));
01ce6322
MS
916 bufptr += strlen(bufptr);
917 break;
918
bf1bc4c6
MS
919 case IPP_TAG_BEGIN_COLLECTION :
920 if (!strcmp(attr->name, "media-size"))
921 {
922 ipp_attribute_t *x_dimension = ippFindAttribute(ippGetCollection(attr, 0), "x-dimension", IPP_TAG_INTEGER);
923 ipp_attribute_t *y_dimension = ippFindAttribute(ippGetCollection(attr, 0), "y-dimension", IPP_TAG_INTEGER);
924 /* Media dimensions */
925
926 if (x_dimension && y_dimension)
927 {
928 pwg_media_t *pwg = pwgMediaForSize(ippGetInteger(x_dimension, 0), ippGetInteger(y_dimension, 0));
929 /* PWG media name */
930 strlcpy(bufptr, pwg->pwg, sizeof(buffer) - (size_t)(bufptr - buffer));
931 break;
932 }
933 }
934
01ce6322 935 default :
7e86f2f6 936 strlcpy(bufptr, "???", sizeof(buffer) - (size_t)(bufptr - buffer));
01ce6322
MS
937 bufptr += strlen(bufptr);
938 break;
939 }
940 }
941 }
942 else if (bufptr < (buffer + sizeof(buffer) - 1))
943 *bufptr++ = '-';
944 break;
945 }
946
947 default :
948 if (bufptr < (buffer + sizeof(buffer) - 2))
949 {
950 *bufptr++ = '%';
951 *bufptr++ = *format;
952 }
953 break;
954 }
955 }
956 else if (bufptr < (buffer + sizeof(buffer) - 1))
957 *bufptr++ = *format;
958 }
ef416fc2 959
01ce6322 960 *bufptr = '\0';
771bd8cb 961
250bb8a9 962#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
0dc4a6bf
MS
963 if (!strcmp(ErrorLog, "syslog"))
964 {
0dc4a6bf
MS
965 static const char * const job_states[] =
966 { /* job-state strings */
967 "Pending",
968 "PendingHeld",
969 "Processing",
970 "ProcessingStopped",
971 "Canceled",
972 "Aborted",
973 "Completed"
974 };
975
a990ad13
MS
976 sd_journal_send("MESSAGE=%s", buffer,
977 "PRIORITY=%i", LOG_INFO,
0dc4a6bf
MS
978 PWG_Event"=JobStateChanged",
979 PWG_ServiceURI"=%s", job->printer->uri,
980 PWG_JobID"=%d", job->id,
981 PWG_JobState"=%s", job_states[job->state_value - IPP_JSTATE_PENDING],
982 PWG_JobImpressionsCompleted"=%d", ippGetInteger(job->impressions, 0),
983 NULL);
984 return (1);
985 }
986
987#elif defined(HAVE_VSYSLOG)
ef416fc2 988 /*
989 * See if we are logging pages via syslog...
990 */
991
992 if (!strcmp(PageLog, "syslog"))
993 {
01ce6322 994 syslog(LOG_INFO, "%s", buffer);
ef416fc2 995
996 return (1);
997 }
250bb8a9 998#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
ef416fc2 999
1000 /*
1001 * Not using syslog; check the log file...
1002 */
1003
22c9029b 1004 if (!cupsdCheckLogFile(&PageFile, PageLog))
ef416fc2 1005 return (0);
1006
1007 /*
1008 * Print a page log entry of the form:
1009 *
ac884b6a 1010 * printer user job-id [DD/MON/YYYY:HH:MM:SS +TTTT] page num-copies \
ef416fc2 1011 * billing hostname
1012 */
1013
01ce6322 1014 cupsFilePrintf(PageFile, "%s\n", buffer);
ef416fc2 1015 cupsFileFlush(PageFile);
1016
1017 return (1);
1018}
1019
1020
1021/*
1022 * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format.
1023 */
1024
1025int /* O - 1 on success, 0 on error */
1026cupsdLogRequest(cupsd_client_t *con, /* I - Request to log */
1027 http_status_t code) /* I - Response code */
1028{
839a51c8 1029 char temp[2048]; /* Temporary string for URI */
ef416fc2 1030 static const char * const states[] = /* HTTP client states... */
1031 {
1032 "WAITING",
1033 "OPTIONS",
1034 "GET",
1035 "GET",
1036 "HEAD",
1037 "POST",
1038 "POST",
1039 "POST",
1040 "PUT",
1041 "PUT",
1042 "DELETE",
1043 "TRACE",
1044 "CLOSE",
1045 "STATUS"
1046 };
1047
1048
1f0275e3
MS
1049 /*
1050 * Filter requests as needed...
1051 */
1052
c9dcc485
MS
1053 if (AccessLogLevel == CUPSD_ACCESSLOG_NONE)
1054 return (1);
1055 else if (AccessLogLevel < CUPSD_ACCESSLOG_ALL)
1f0275e3
MS
1056 {
1057 /*
ba55dc12 1058 * Eliminate simple GET, POST, and PUT requests...
1f0275e3
MS
1059 */
1060
1061 if ((con->operation == HTTP_GET &&
1062 strncmp(con->uri, "/admin/conf", 11) &&
1063 strncmp(con->uri, "/admin/log", 10)) ||
1064 (con->operation == HTTP_POST && !con->request &&
1065 strncmp(con->uri, "/admin", 6)) ||
ba55dc12
MS
1066 (con->operation != HTTP_GET && con->operation != HTTP_POST &&
1067 con->operation != HTTP_PUT))
1f0275e3
MS
1068 return (1);
1069
1070 if (con->request && con->response &&
e07d4801
MS
1071 (con->response->request.status.status_code < IPP_REDIRECTION_OTHER_SITE ||
1072 con->response->request.status.status_code == IPP_NOT_FOUND))
1f0275e3
MS
1073 {
1074 /*
1075 * Check successful requests...
1076 */
1077
1078 ipp_op_t op = con->request->request.op.operation_id;
1079 static cupsd_accesslog_t standard_ops[] =
1080 {
1081 CUPSD_ACCESSLOG_ALL, /* reserved */
1082 CUPSD_ACCESSLOG_ALL, /* reserved */
1083 CUPSD_ACCESSLOG_ACTIONS,/* Print-Job */
1084 CUPSD_ACCESSLOG_ACTIONS,/* Print-URI */
1085 CUPSD_ACCESSLOG_ACTIONS,/* Validate-Job */
1086 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job */
1087 CUPSD_ACCESSLOG_ACTIONS,/* Send-Document */
1088 CUPSD_ACCESSLOG_ACTIONS,/* Send-URI */
1089 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Job */
1090 CUPSD_ACCESSLOG_ALL, /* Get-Job-Attributes */
1091 CUPSD_ACCESSLOG_ALL, /* Get-Jobs */
1092 CUPSD_ACCESSLOG_ALL, /* Get-Printer-Attributes */
1093 CUPSD_ACCESSLOG_ACTIONS,/* Hold-Job */
1094 CUPSD_ACCESSLOG_ACTIONS,/* Release-Job */
1095 CUPSD_ACCESSLOG_ACTIONS,/* Restart-Job */
1096 CUPSD_ACCESSLOG_ALL, /* reserved */
1097 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer */
1098 CUPSD_ACCESSLOG_CONFIG, /* Resume-Printer */
1099 CUPSD_ACCESSLOG_CONFIG, /* Purge-Jobs */
1100 CUPSD_ACCESSLOG_CONFIG, /* Set-Printer-Attributes */
1101 CUPSD_ACCESSLOG_ACTIONS,/* Set-Job-Attributes */
1102 CUPSD_ACCESSLOG_CONFIG, /* Get-Printer-Supported-Values */
1103 CUPSD_ACCESSLOG_ACTIONS,/* Create-Printer-Subscription */
1104 CUPSD_ACCESSLOG_ACTIONS,/* Create-Job-Subscription */
1105 CUPSD_ACCESSLOG_ALL, /* Get-Subscription-Attributes */
1106 CUPSD_ACCESSLOG_ALL, /* Get-Subscriptions */
1107 CUPSD_ACCESSLOG_ACTIONS,/* Renew-Subscription */
1108 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Subscription */
1109 CUPSD_ACCESSLOG_ALL, /* Get-Notifications */
1110 CUPSD_ACCESSLOG_ACTIONS,/* Send-Notifications */
1111 CUPSD_ACCESSLOG_ALL, /* reserved */
1112 CUPSD_ACCESSLOG_ALL, /* reserved */
1113 CUPSD_ACCESSLOG_ALL, /* reserved */
1114 CUPSD_ACCESSLOG_ALL, /* Get-Print-Support-Files */
1115 CUPSD_ACCESSLOG_CONFIG, /* Enable-Printer */
1116 CUPSD_ACCESSLOG_CONFIG, /* Disable-Printer */
1117 CUPSD_ACCESSLOG_CONFIG, /* Pause-Printer-After-Current-Job */
1118 CUPSD_ACCESSLOG_ACTIONS,/* Hold-New-Jobs */
1119 CUPSD_ACCESSLOG_ACTIONS,/* Release-Held-New-Jobs */
1120 CUPSD_ACCESSLOG_CONFIG, /* Deactivate-Printer */
1121 CUPSD_ACCESSLOG_CONFIG, /* Activate-Printer */
1122 CUPSD_ACCESSLOG_CONFIG, /* Restart-Printer */
1123 CUPSD_ACCESSLOG_CONFIG, /* Shutdown-Printer */
1124 CUPSD_ACCESSLOG_CONFIG, /* Startup-Printer */
1125 CUPSD_ACCESSLOG_ACTIONS,/* Reprocess-Job */
1126 CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Current-Job */
1127 CUPSD_ACCESSLOG_ACTIONS,/* Suspend-Current-Job */
1128 CUPSD_ACCESSLOG_ACTIONS,/* Resume-Job */
1129 CUPSD_ACCESSLOG_ACTIONS,/* Promote-Job */
1130 CUPSD_ACCESSLOG_ACTIONS /* Schedule-Job-After */
1131 };
1132 static cupsd_accesslog_t cups_ops[] =
1133 {
1134 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Default */
1135 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Printers */
1136 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Printer */
1137 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Printer */
1138 CUPSD_ACCESSLOG_ALL, /* CUPS-Get-Classes */
1139 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Add-Modify-Class */
1140 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Delete-Class */
1141 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Accept-Jobs */
1142 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Reject-Jobs */
1143 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Set-Default */
1144 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-Devices */
1145 CUPSD_ACCESSLOG_CONFIG, /* CUPS-Get-PPDs */
1146 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Move-Job */
1147 CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Authenticate-Job */
1148 CUPSD_ACCESSLOG_ALL /* CUPS-Get-PPD */
1149 };
771bd8cb 1150
1f0275e3
MS
1151
1152 if ((op <= IPP_SCHEDULE_JOB_AFTER && standard_ops[op] > AccessLogLevel) ||
1153 (op >= CUPS_GET_DEFAULT && op <= CUPS_GET_PPD &&
1154 cups_ops[op - CUPS_GET_DEFAULT] > AccessLogLevel))
1155 return (1);
1156 }
1157 }
1158
250bb8a9 1159#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
a990ad13
MS
1160 if (!strcmp(ErrorLog, "syslog"))
1161 {
1162 sd_journal_print(LOG_INFO, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s", con->http->hostname, con->username[0] != '\0' ? con->username : "-", states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)), con->http->version / 100, con->http->version % 100, code, CUPS_LLCAST con->bytes, con->request ? ippOpString(con->request->request.op.operation_id) : "-", con->response ? ippErrorString(con->response->request.status.status_code) : "-");
1163 return (1);
1164 }
0dc4a6bf
MS
1165
1166#elif defined(HAVE_VSYSLOG)
ef416fc2 1167 /*
1168 * See if we are logging accesses via syslog...
1169 */
1170
1171 if (!strcmp(AccessLog, "syslog"))
1172 {
2abf387c 1173 syslog(LOG_INFO,
1174 "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
996acce8 1175 con->http->hostname, con->username[0] != '\0' ? con->username : "-",
839a51c8 1176 states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)),
996acce8 1177 con->http->version / 100, con->http->version % 100,
2abf387c 1178 code, CUPS_LLCAST con->bytes,
1179 con->request ?
1180 ippOpString(con->request->request.op.operation_id) : "-",
1181 con->response ?
1182 ippErrorString(con->response->request.status.status_code) : "-");
ef416fc2 1183
1184 return (1);
1185 }
250bb8a9 1186#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
ef416fc2 1187
1188 /*
1189 * Not using syslog; check the log file...
1190 */
1191
22c9029b 1192 if (!cupsdCheckLogFile(&AccessFile, AccessLog))
ef416fc2 1193 return (0);
1194
1195 /*
1196 * Write a log of the request in "common log format"...
1197 */
1198
1199 cupsFilePrintf(AccessFile,
1200 "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
996acce8 1201 con->http->hostname,
c8fef167 1202 con->username[0] != '\0' ? con->username : "-",
dfd5680b
MS
1203 cupsdGetDateTime(&(con->start), LogTimeFormat),
1204 states[con->operation],
839a51c8 1205 _httpEncodeURI(temp, con->uri, sizeof(temp)),
996acce8 1206 con->http->version / 100, con->http->version % 100,
ef416fc2 1207 code, CUPS_LLCAST con->bytes,
1208 con->request ?
1209 ippOpString(con->request->request.op.operation_id) : "-",
1210 con->response ?
1211 ippErrorString(con->response->request.status.status_code) :
1212 "-");
1213
1214 cupsFileFlush(AccessFile);
1215
1216 return (1);
1217}
1218
1219
75bd9771
MS
1220/*
1221 * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
1222 */
1223
1224int /* O - 1 on success, 0 on failure */
1225cupsdWriteErrorLog(int level, /* I - Log level */
1226 const char *message) /* I - Message string */
1227{
28a463e0 1228 int ret = 1; /* Return value */
75bd9771
MS
1229 static const char levels[] = /* Log levels... */
1230 {
1231 ' ',
1232 'X',
1233 'A',
1234 'C',
1235 'E',
1236 'W',
1237 'N',
1238 'I',
1239 'D',
1240 'd'
1241 };
75bd9771
MS
1242
1243
250bb8a9 1244#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
a990ad13
MS
1245 if (!strcmp(ErrorLog, "syslog"))
1246 {
1247 sd_journal_print(log_levels[level], "%s", message);
1248 return (1);
1249 }
0dc4a6bf
MS
1250
1251#elif defined(HAVE_VSYSLOG)
75bd9771
MS
1252 /*
1253 * See if we are logging errors via syslog...
1254 */
1255
1256 if (!strcmp(ErrorLog, "syslog"))
1257 {
0dc4a6bf 1258 syslog(log_levels[level], "%s", message);
75bd9771
MS
1259 return (1);
1260 }
250bb8a9 1261#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
75bd9771
MS
1262
1263 /*
1264 * Not using syslog; check the log file...
1265 */
1266
28a463e0
MS
1267 _cupsMutexLock(&log_mutex);
1268
22c9029b 1269 if (!cupsdCheckLogFile(&ErrorFile, ErrorLog))
28a463e0
MS
1270 {
1271 ret = 0;
1272 }
1273 else
1274 {
1275 /*
1276 * Write the log message...
1277 */
75bd9771 1278
28a463e0
MS
1279 cupsFilePrintf(ErrorFile, "%c %s %s\n", levels[level],
1280 cupsdGetDateTime(NULL, LogTimeFormat), message);
1281 cupsFileFlush(ErrorFile);
1282 }
75bd9771 1283
28a463e0 1284 _cupsMutexUnlock(&log_mutex);
75bd9771 1285
28a463e0 1286 return (ret);
75bd9771
MS
1287}
1288
1289
ef416fc2 1290/*
75bd9771
MS
1291 * 'format_log_line()' - Format a line for a log file.
1292 *
1293 * This function resizes a global string buffer as needed. Each call returns
1294 * a pointer to this buffer, so the contents are only good until the next call
1295 * to format_log_line()...
1296 */
1297
005dd1eb 1298static int /* O - -1 for fatal, 0 for retry, 1 for success */
75bd9771
MS
1299format_log_line(const char *message, /* I - Printf-style format string */
1300 va_list ap) /* I - Argument list */
1301{
7e86f2f6 1302 ssize_t len; /* Length of formatted line */
75bd9771
MS
1303
1304
1305 /*
1306 * Allocate the line buffer as needed...
1307 */
1308
1309 if (!log_linesize)
1310 {
1311 log_linesize = 8192;
1312 log_line = malloc(log_linesize);
1313
1314 if (!log_line)
005dd1eb 1315 return (-1);
75bd9771
MS
1316 }
1317
1318 /*
1319 * Format the log message...
1320 */
1321
59189c00 1322 len = _cups_safe_vsnprintf(log_line, log_linesize, message, ap);
75bd9771
MS
1323
1324 /*
1325 * Resize the buffer as needed...
1326 */
1327
7e86f2f6 1328 if ((size_t)len >= log_linesize && log_linesize < 65536)
75bd9771
MS
1329 {
1330 char *temp; /* Temporary string pointer */
1331
75bd9771
MS
1332 len ++;
1333
1334 if (len < 8192)
1335 len = 8192;
1336 else if (len > 65536)
1337 len = 65536;
1338
7e86f2f6 1339 temp = realloc(log_line, (size_t)len);
75bd9771
MS
1340
1341 if (temp)
1342 {
1343 log_line = temp;
7e86f2f6 1344 log_linesize = (size_t)len;
75bd9771 1345
005dd1eb
MS
1346 return (0);
1347 }
75bd9771
MS
1348 }
1349
005dd1eb 1350 return (1);
75bd9771 1351}