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