]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Add cupsdLogPrinter function.
authorMichael R Sweet <msweet@msweet.org>
Tue, 1 Apr 2025 17:29:28 +0000 (13:29 -0400)
committerMichael R Sweet <msweet@msweet.org>
Tue, 1 Apr 2025 17:29:28 +0000 (13:29 -0400)
scheduler/conf.h
scheduler/log.c

index 7d336130323dbb09614592f54582362ef2d1da64..e3458f260749c51691637f9f93f0f19dc39d30e2 100644 (file)
@@ -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);
index 62a8e24aefcad5add3fceffab473be68026d8f6b..5eec7abb9044dc6595ce9066c9ba2179304f2b3f 100644 (file)
@@ -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 <stdarg.h>
 #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.
  */