From: Michael R Sweet Date: Tue, 1 Apr 2025 17:29:28 +0000 (-0400) Subject: Add cupsdLogPrinter function. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d1b0cd5f2974205de0ff4644549c488a94f2268a;p=thirdparty%2Fcups.git Add cupsdLogPrinter function. --- diff --git a/scheduler/conf.h b/scheduler/conf.h index 7d33613032..e3458f2607 100644 --- a/scheduler/conf.h +++ b/scheduler/conf.h @@ -1,7 +1,7 @@ /* * Configuration file definitions for the CUPS scheduler. * - * Copyright © 2020-2024 by OpenPrinting. + * Copyright © 2020-2025 by OpenPrinting. * Copyright © 2007-2018 by Apple Inc. * Copyright © 1997-2007 by Easy Software Products, all rights reserved. * @@ -286,6 +286,7 @@ extern int cupsdLogJob(cupsd_job_t *job, int level, const char *message, ...) _CUPS_FORMAT(3, 4); extern int cupsdLogMessage(int level, const char *message, ...) _CUPS_FORMAT(2, 3); extern int cupsdLogPage(cupsd_job_t *job, const char *page); +extern int cupsdLogPrinter(cupsd_printer_t *p, int level, const char *message, ...) _CUPS_FORMAT(3, 4); extern int cupsdLogRequest(cupsd_client_t *con, http_status_t code); extern int cupsdReadConfiguration(void); extern int cupsdWriteErrorLog(int level, const char *message); diff --git a/scheduler/log.c b/scheduler/log.c index 62a8e24aef..5eec7abb90 100644 --- a/scheduler/log.c +++ b/scheduler/log.c @@ -1,7 +1,7 @@ /* * Log file routines for the CUPS scheduler. * - * Copyright © 2020-2024 by OpenPrinting. + * Copyright © 2020-2025 by OpenPrinting. * Copyright © 2007-2018 by Apple Inc. * Copyright © 1997-2007 by Easy Software Products, all rights reserved. * @@ -9,10 +9,6 @@ * information. */ -/* - * Include necessary headers... - */ - #include "cupsd.h" #include #ifdef HAVE_ASL_H @@ -1019,6 +1015,89 @@ cupsdLogPage(cupsd_job_t *job, /* I - Job being printed */ } +/* + * 'cupsdLogPrinter()' - Log a printer message. + */ + +int /* O - 1 on success, 0 on error */ +cupsdLogPrinter( + cupsd_printer_t *p, /* I - Printer */ + int level, /* I - Log level */ + const char *message, /* I - Printf-style message string */ + ...) /* I - Additional arguments as needed */ +{ + va_list ap, ap2; /* Argument pointers */ + char pmsg[1024]; /* Format string for printer message */ + int status; /* Formatting status */ + + + /* + * See if we want to log this message... + */ + + if (TestConfigFile || !ErrorLog) + return (1); + + if (level > LogLevel) + return (1); + + /* + * Format and write the log message... + */ + + if (p) + snprintf(pmsg, sizeof(pmsg), "[Printer %s] %s", p->name, message); + else + cupsCopyString(pmsg, message, sizeof(pmsg)); + + va_start(ap, message); + + do + { + va_copy(ap2, ap); + status = format_log_line(pmsg, ap2); + va_end(ap2); + } + while (status == 0); + + va_end(ap); + + if (status > 0) + { +#ifdef HAVE_SYSTEMD_SD_JOURNAL_H + if (!strcmp(ErrorLog, "syslog")) + { + static const char * const printer_states[] = + { /* printer-state strings */ + "Idle", + "Processing", + "Stopped" + }; + + if (p) + sd_journal_send("MESSAGE=%s", log_line, + "PRIORITY=%i", log_levels[level], + PWG_Event"=PrinterStateChanged", + PWG_ServiceURI"=%s", p ? p->uri : "", + PWG_ServiceState"=%s", printer_states[p->state - IPP_PSTATE_IDLE], + NULL); + else + sd_journal_send("MESSAGE=%s", log_line, + "PRIORITY=%i", log_levels[level], + NULL); + + return (1); + } + else +#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */ + + return (cupsdWriteErrorLog(level, log_line)); + } + else + return (cupsdWriteErrorLog(CUPSD_LOG_ERROR, "Unable to allocate memory for log line.")); +} + + /* * 'cupsdLogRequest()' - Log an HTTP request in Common Log Format. */