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