]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/log.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / scheduler / log.c
index 19e850c96403ff89beb4d25bc644f6a22a424a8d..7dd53c7f77eed35068f0231eca8f31aa4dd6b89b 100644 (file)
@@ -1,16 +1,10 @@
 /*
- * "$Id$"
- *
  * Log file routines for the CUPS scheduler.
  *
- * Copyright 2007-2015 by Apple Inc.
+ * Copyright 2007-2017 by Apple Inc.
  * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
@@ -227,20 +221,11 @@ cupsdCheckLogFile(cups_file_t **lf,       /* IO - Log file */
 
       if (*lf == NULL)
       {
-#ifdef HAVE_ASL_H
-       asl_object_t    m;              /* Log message */
-
-       m = asl_new(ASL_TYPE_MSG);
-       asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-       asl_log(NULL, m, ASL_LEVEL_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
-       asl_release(m);
-
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
         sd_journal_print(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
-
 #else
        syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
-#endif /* HAVE_ASL_H */
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
         if (FatalErrors & CUPSD_FATAL_LOG)
          cupsdEndProcess(getpid(), 0);
@@ -281,20 +266,12 @@ cupsdCheckLogFile(cups_file_t **lf,       /* IO - Log file */
 
     if ((*lf = cupsFileOpen(filename, "a")) == NULL)
     {
-#ifdef HAVE_ASL_H
-       asl_object_t    m;              /* Log message */
-
-       m = asl_new(ASL_TYPE_MSG);
-       asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-       asl_log(NULL, m, ASL_LEVEL_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
-       asl_release(m);
-
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
-        sd_journal_print(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
+      sd_journal_print(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
 
 #else
-       syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
-#endif /* HAVE_ASL_H */
+      syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename, strerror(errno));
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
       if (FatalErrors & CUPSD_FATAL_LOG)
        cupsdEndProcess(getpid(), 0);
@@ -584,56 +561,13 @@ cupsdLogJob(cupsd_job_t *job,             /* I - Job */
   if (TestConfigFile || !ErrorLog)
     return (1);
 
-  if ((level > LogLevel ||
-       (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
-      LogDebugHistory <= 0)
+  if (level > LogLevel && LogDebugHistory <= 0)
     return (1);
 
-#ifdef HAVE_ASL_H
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
   if (!strcmp(ErrorLog, "syslog"))
   {
-    asl_object_t       m;              /* Log message */
-    char               job_id[32],     /* job-id string */
-                       completed[32];  /* job-impressions-completed string */
-    cupsd_printer_t *printer = job->printer ? job->printer : cupsdFindDest(job->dest);
-    static const char * const job_states[] =
-    {                                  /* job-state strings */
-      "Pending",
-      "PendingHeld",
-      "Processing",
-      "ProcessingStopped",
-      "Canceled",
-      "Aborted",
-      "Completed"
-    };
-
-    snprintf(job_id, sizeof(job_id), "%d", job->id);
-
-    m = asl_new(ASL_TYPE_MSG);
-    asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-    asl_set(m, PWG_Event, "JobStateChanged");
-    asl_set(m, PWG_ServiceURI, printer->uri);
-    asl_set(m, PWG_JobID, job_id);
-    asl_set(m, PWG_JobState, job_states[job->state_value - IPP_JSTATE_PENDING]);
-
-    if (job->impressions)
-    {
-      snprintf(completed, sizeof(completed), "%d", ippGetInteger(job->impressions, 0));
-      asl_set(m, PWG_JobImpressionsCompleted, completed);
-    }
-
-    va_start(ap, message);
-    asl_vlog(NULL, m, log_levels[level], message, ap);
-    va_end(ap);
-
-    asl_release(m);
-    return (1);
-  }
-
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
-  if (!strcmp(ErrorLog, "syslog"))
-  {
-    cupsd_printer_t *printer = job->printer ? job->printer : cupsdFindDest(job->dest);
+    cupsd_printer_t *printer = job ? (job->printer ? job->printer : (job->dest ? cupsdFindDest(job->dest) : NULL)) : NULL;
     static const char * const job_states[] =
     {                                  /* job-state strings */
       "Pending",
@@ -657,18 +591,23 @@ cupsdLogJob(cupsd_job_t *job,             /* I - Job */
 
     va_end(ap);
 
-    sd_journal_send("MESSAGE=%s", log_line,
-                    "PRIORITY=%i", log_levels[level],
-                   PWG_Event"=JobStateChanged",
-                   PWG_ServiceURI"=%s", printer->uri,
-                   PWG_JobID"=%d", job->id,
-                   PWG_JobState"=%s", job_states[job->state_value - IPP_JSTATE_PENDING],
-                   PWG_JobImpressionsCompleted"=%d", ippGetInteger(job->impressions, 0),
-                   NULL);
+    if (job)
+      sd_journal_send("MESSAGE=%s", log_line,
+                     "PRIORITY=%i", log_levels[level],
+                     PWG_Event"=JobStateChanged",
+                     PWG_ServiceURI"=%s", printer ? printer->uri : "",
+                     PWG_JobID"=%d", job->id,
+                     PWG_JobState"=%s", job->state_value < IPP_JSTATE_PENDING ? "" : job_states[job->state_value - IPP_JSTATE_PENDING],
+                     PWG_JobImpressionsCompleted"=%d", ippGetInteger(job->impressions, 0),
+                     NULL);
+    else
+      sd_journal_send("MESSAGE=%s", log_line,
+                     "PRIORITY=%i", log_levels[level],
+                     NULL);
+
     return (1);
   }
-
-#endif /* HAVE_ASL_H */
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
  /*
   * Format and write the log message...
@@ -693,10 +632,7 @@ cupsdLogJob(cupsd_job_t *job,              /* I - Job */
 
   if (status > 0)
   {
-    if (job &&
-        (level > LogLevel ||
-         (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
-       LogDebugHistory > 0)
+    if (job && level > LogLevel && LogDebugHistory > 0)
     {
      /*
       * Add message to the job history...
@@ -735,8 +671,7 @@ cupsdLogJob(cupsd_job_t *job,               /* I - Job */
 
       return (1);
     }
-    else if (level <= LogLevel &&
-             (level != CUPSD_LOG_INFO || LogLevel >= CUPSD_LOG_DEBUG))
+    else if (level <= LogLevel)
       return (cupsdWriteErrorLog(level, log_line));
     else
       return (1);
@@ -764,19 +699,22 @@ cupsdLogMessage(int        level, /* I - Log level */
   * See if we want to log this message...
   */
 
-  if ((TestConfigFile || !ErrorLog) && level <= CUPSD_LOG_WARN)
+  if (TestConfigFile && level <= CUPSD_LOG_WARN)
   {
     va_start(ap, message);
 
-#ifdef HAVE_ASL_H
-    asl_object_t       m;              /* Log message */
+    vfprintf(stderr, message, ap);
+    putc('\n', stderr);
 
-    m = asl_new(ASL_TYPE_MSG);
-    asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-    asl_vlog(NULL, m, log_levels[level], message, ap);
-    asl_release(m);
+    va_end(ap);
 
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
+    return (1);
+  }
+  else if (!ErrorLog && level <= CUPSD_LOG_WARN)
+  {
+    va_start(ap, message);
+
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
     sd_journal_printv(log_levels[level], message, ap);
 
 #elif defined(HAVE_VSYSLOG)
@@ -785,41 +723,24 @@ cupsdLogMessage(int        level, /* I - Log level */
 #else
     vfprintf(stderr, message, ap);
     putc('\n', stderr);
-#endif /* HAVE_VSYSLOG */
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
     va_end(ap);
 
     return (1);
   }
-
-  if (level > LogLevel || !ErrorLog)
+  else if (level > LogLevel || !ErrorLog)
     return (1);
 
-#ifdef HAVE_ASL_H
-  if (!strcmp(ErrorLog, "syslog"))
-  {
-    asl_object_t       m;              /* Log message */
-
-    m = asl_new(ASL_TYPE_MSG);
-    asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-
-    va_start(ap, message);
-    asl_vlog(NULL, m, log_levels[level], message, ap);
-    va_end(ap);
-
-    asl_release(m);
-    return (1);
-  }
-
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
-  if (!strcmp(ErrorLog, "syslog"))
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
+  else if (!strcmp(ErrorLog, "syslog"))
   {
     va_start(ap, message);
     sd_journal_printv(log_levels[level], message, ap);
     va_end(ap);
     return (1);
   }
-#endif /* HAVE_ASL_H */
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
  /*
   * Format and write the log message...
@@ -1034,45 +955,7 @@ cupsdLogPage(cupsd_job_t *job,            /* I - Job being printed */
 
   *bufptr = '\0';
 
-#ifdef HAVE_ASL_H
-  if (!strcmp(ErrorLog, "syslog"))
-  {
-    asl_object_t       m;              /* Log message */
-    char               job_id[32],     /* job-id string */
-                       completed[32];  /* job-impressions-completed string */
-    static const char * const job_states[] =
-    {                                  /* job-state strings */
-      "Pending",
-      "PendingHeld",
-      "Processing",
-      "ProcessingStopped",
-      "Canceled",
-      "Aborted",
-      "Completed"
-    };
-
-    snprintf(job_id, sizeof(job_id), "%d", job->id);
-
-    m = asl_new(ASL_TYPE_MSG);
-    asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-    asl_set(m, PWG_Event, "JobStateChanged");
-    asl_set(m, PWG_ServiceURI, job->printer->uri);
-    asl_set(m, PWG_JobID, job_id);
-    asl_set(m, PWG_JobState, job_states[job->state_value - IPP_JSTATE_PENDING]);
-
-    if (job->impressions)
-    {
-      snprintf(completed, sizeof(completed), "%d", ippGetInteger(job->impressions, 0));
-      asl_set(m, PWG_JobImpressionsCompleted, completed);
-    }
-
-    asl_log(NULL, m, ASL_LEVEL_INFO, "%s", buffer);
-
-    asl_release(m);
-    return (1);
-  }
-
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
   if (!strcmp(ErrorLog, "syslog"))
   {
     static const char * const job_states[] =
@@ -1108,7 +991,7 @@ cupsdLogPage(cupsd_job_t *job,             /* I - Job being printed */
 
     return (1);
   }
-#endif /* HAVE_ASL_H */
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
  /*
   * Not using syslog; check the log file...
@@ -1269,29 +1152,7 @@ cupsdLogRequest(cupsd_client_t *con,     /* I - Request to log */
     }
   }
 
-#ifdef HAVE_ASL_H
-  if (!strcmp(ErrorLog, "syslog"))
-  {
-    asl_object_t       m;              /* Log message */
-
-    m = asl_new(ASL_TYPE_MSG);
-    asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-
-    asl_log(NULL, m, ASL_LEVEL_INFO, "REQUEST %s - %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
-           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) : "-");
-
-    asl_release(m);
-    return (1);
-  }
-
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
   if (!strcmp(ErrorLog, "syslog"))
   {
     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) : "-");
@@ -1318,7 +1179,7 @@ cupsdLogRequest(cupsd_client_t *con,      /* I - Request to log */
 
     return (1);
   }
-#endif /* HAVE_ASL_H */
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
  /*
   * Not using syslog; check the log file...
@@ -1376,20 +1237,7 @@ cupsdWriteErrorLog(int        level,     /* I - Log level */
                };
 
 
-#ifdef HAVE_ASL_H
-  if (!strcmp(ErrorLog, "syslog"))
-  {
-    asl_object_t       m;              /* Log message */
-
-    m = asl_new(ASL_TYPE_MSG);
-    asl_set(m, ASL_KEY_FACILITY, "org.cups.cupsd");
-    asl_log(NULL, m, ASL_LEVEL_INFO, "%s", message);
-
-    asl_release(m);
-    return (1);
-  }
-
-#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H)
+#ifdef HAVE_SYSTEMD_SD_JOURNAL_H
   if (!strcmp(ErrorLog, "syslog"))
   {
     sd_journal_print(log_levels[level], "%s", message);
@@ -1406,7 +1254,7 @@ cupsdWriteErrorLog(int        level,      /* I - Log level */
     syslog(log_levels[level], "%s", message);
     return (1);
   }
-#endif /* HAVE_ASL_H */
+#endif /* HAVE_SYSTEMD_SD_JOURNAL_H */
 
  /*
   * Not using syslog; check the log file...
@@ -1467,7 +1315,7 @@ format_log_line(const char *message,      /* I - Printf-style format string */
   * Format the log message...
   */
 
-  len = vsnprintf(log_line, log_linesize, message, ap);
+  len = _cups_safe_vsnprintf(log_line, log_linesize, message, ap);
 
  /*
   * Resize the buffer as needed...
@@ -1477,7 +1325,6 @@ format_log_line(const char *message,      /* I - Printf-style format string */
   {
     char       *temp;                  /* Temporary string pointer */
 
-
     len ++;
 
     if (len < 8192)
@@ -1498,8 +1345,3 @@ format_log_line(const char *message,      /* I - Printf-style format string */
 
   return (1);
 }
-
-
-/*
- * End of "$Id$".
- */