]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/log.c
Update svn:keyword properties.
[thirdparty/cups.git] / scheduler / log.c
index 77ab3e5e86f7fa098cb93f92f076e4ac92e920f1..0d49357364b91c74374256bd64eac30d90538007 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: log.c 6875 2007-08-27 23:25:06Z mike $"
+ * "$Id$"
  *
- *   Log file routines for the Common UNIX Printing System (CUPS).
+ *   Log file routines for the CUPS scheduler.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2012 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
  *
  * Contents:
  *
+ *   cupsdCheckLogFile()     - Open/rotate a log file if it needs it.
  *   cupsdGetDateTime()   - Returns a pointer to a date/time string.
  *   cupsdLogGSSMessage() - Log a GSSAPI error...
+ *   cupsdLogJob()        - Log a job message.
  *   cupsdLogMessage()    - Log a message to the error log file.
  *   cupsdLogPage()       - Log a page to the page log file.
  *   cupsdLogRequest()    - Log an HTTP request in Common Log Format.
- *   check_log_file()     - Open/rotate a log file if it needs it.
+ *   cupsdWriteErrorLog() - Write a line to the ErrorLog.
+ *   format_log_line()    - Format a line for a log file.
  */
 
 /*
 #include <syslog.h>
 
 
+/*
+ * Local globals...
+ */
+
+static int     log_linesize = 0;       /* Size of line for output file */
+static char    *log_line = NULL;       /* Line for output file */
+
+#ifdef HAVE_VSYSLOG
+static const int syslevels[] =         /* SYSLOG levels... */
+               {
+                 0,
+                 LOG_EMERG,
+                 LOG_ALERT,
+                 LOG_CRIT,
+                 LOG_ERR,
+                 LOG_WARNING,
+                 LOG_NOTICE,
+                 LOG_INFO,
+                 LOG_DEBUG,
+                 LOG_DEBUG
+               };
+#endif /* HAVE_VSYSLOG */
+
+
 /*
  * Local functions...
  */
 
-static int     check_log_file(cups_file_t **, const char *);
+static int     format_log_line(const char *message, va_list ap);
+
+
+/*
+ * 'cupsdCheckLogFile()' - Open/rotate a log file if it needs it.
+ */
+
+int                                    /* O  - 1 if log file open */
+cupsdCheckLogFile(cups_file_t **lf,    /* IO - Log file */
+                 const char  *logname) /* I  - Log filename */
+{
+  char         backname[1024],         /* Backup log filename */
+               filename[1024],         /* Formatted log filename */
+               *ptr;                   /* Pointer into filename */
+  const char   *logptr;                /* Pointer into log filename */
+
+
+ /*
+  * See if we have a log file to check...
+  */
+
+  if (!lf || !logname || !logname[0])
+    return (1);
+
+ /*
+  * Format the filename as needed...
+  */
+
+  if (!*lf ||
+      (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
+       MaxLogSize > 0))
+  {
+   /*
+    * Handle format strings...
+    */
+
+    filename[sizeof(filename) - 1] = '\0';
+
+    if (logname[0] != '/')
+    {
+      strlcpy(filename, ServerRoot, sizeof(filename));
+      strlcat(filename, "/", sizeof(filename));
+    }
+    else
+      filename[0] = '\0';
+
+    for (logptr = logname, ptr = filename + strlen(filename);
+         *logptr && ptr < (filename + sizeof(filename) - 1);
+        logptr ++)
+      if (*logptr == '%')
+      {
+       /*
+        * Format spec...
+       */
+
+        logptr ++;
+       if (*logptr == 's')
+       {
+        /*
+         * Insert the server name...
+         */
+
+         strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename));
+         ptr += strlen(ptr);
+       }
+        else
+       {
+        /*
+         * Otherwise just insert the character...
+         */
+
+         *ptr++ = *logptr;
+       }
+      }
+      else
+       *ptr++ = *logptr;
+
+    *ptr = '\0';
+  }
+
+ /*
+  * See if the log file is open...
+  */
+
+  if (!*lf)
+  {
+   /*
+    * Nope, open the log file...
+    */
+
+    if ((*lf = cupsFileOpen(filename, "a")) == NULL)
+    {
+     /*
+      * If the file is in CUPS_LOGDIR then try to create a missing directory...
+      */
+
+      if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
+      {
+       /*
+        * Try updating the permissions of the containing log directory, using
+       * the log file permissions as a basis...
+       */
+
+        int log_dir_perm = 0300 | LogFilePerm;
+                                       /* LogFilePerm + owner write/search */
+       if (log_dir_perm & 0040)
+         log_dir_perm |= 0010;         /* Add group search */
+       if (log_dir_perm & 0004)
+         log_dir_perm |= 0001;         /* Add other search */
+
+        cupsdCheckPermissions(CUPS_LOGDIR, NULL, log_dir_perm, RunUser, Group,
+                             1, -1);
+
+        *lf = cupsFileOpen(filename, "a");
+      }
+
+      if (*lf == NULL)
+      {
+       syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
+              strerror(errno));
+
+        if (FatalErrors & CUPSD_FATAL_LOG)
+         cupsdEndProcess(getpid(), 0);
+
+       return (0);
+      }
+    }
+
+    if (strncmp(filename, "/dev/", 5))
+    {
+     /*
+      * Change ownership and permissions of non-device logs...
+      */
+
+      fchown(cupsFileNumber(*lf), RunUser, Group);
+      fchmod(cupsFileNumber(*lf), LogFilePerm);
+    }
+  }
+
+ /*
+  * Do we need to rotate the log?
+  */
+
+  if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
+      MaxLogSize > 0)
+  {
+   /*
+    * Rotate log file...
+    */
+
+    cupsFileClose(*lf);
+
+    strlcpy(backname, filename, sizeof(backname));
+    strlcat(backname, ".O", sizeof(backname));
+
+    unlink(backname);
+    rename(filename, backname);
+
+    if ((*lf = cupsFileOpen(filename, "a")) == NULL)
+    {
+      syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
+             strerror(errno));
+
+      if (FatalErrors & CUPSD_FATAL_LOG)
+       cupsdEndProcess(getpid(), 0);
+
+      return (0);
+    }
+
+   /*
+    * Change ownership and permissions of non-device logs...
+    */
+
+    fchown(cupsFileNumber(*lf), RunUser, Group);
+    fchmod(cupsFileNumber(*lf), LogFilePerm);
+  }
+
+  return (1);
+}
 
 
 /*
@@ -43,11 +248,14 @@ static int check_log_file(cups_file_t **, const char *);
  */
 
 char *                                 /* O - Date/time string */
-cupsdGetDateTime(time_t t)             /* I - Time value */
+cupsdGetDateTime(struct timeval *t,    /* I - Time value or NULL for current */
+                 cupsd_time_t   format)        /* I - Format to use */
 {
-  struct tm    *date;                  /* Date/time value */
-  static time_t        last_time = -1;         /* Last time value */
-  static char  s[1024];                /* Date/time string */
+  struct timeval       curtime;        /* Current time value */
+  struct tm            *date;          /* Date/time value */
+  static struct timeval        last_time = { 0, 0 };
+                                       /* Last time we formatted */
+  static char          s[1024];        /* Date/time string */
   static const char * const months[12] =/* Months */
                {
                  "Jan",
@@ -65,9 +273,20 @@ cupsdGetDateTime(time_t t)          /* I - Time value */
                };
 
 
-  if (t != last_time)
+ /*
+  * Make sure we have a valid time...
+  */
+
+  if (!t)
   {
-    last_time = t;
+    gettimeofday(&curtime, NULL);
+    t = &curtime;
+  }
+
+  if (t->tv_sec != last_time.tv_sec ||
+      (LogTimeFormat == CUPSD_TIME_USECS && t->tv_usec != last_time.tv_usec))
+  {
+    last_time = *t;
 
    /*
     * Get the date and time from the UNIX time value, and then format it
@@ -83,15 +302,25 @@ cupsdGetDateTime(time_t t)         /* I - Time value */
     * (*BSD and Darwin store the timezone offset in the tm structure)
     */
 
-    date = localtime(&t);
+    date = localtime(&(t->tv_sec));
 
-    snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
-            date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
-            date->tm_hour, date->tm_min, date->tm_sec,
+    if (format == CUPSD_TIME_STANDARD)
+      snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d %+03ld%02ld]",
+              date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
+              date->tm_hour, date->tm_min, date->tm_sec,
+#ifdef HAVE_TM_GMTOFF
+              date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
+#else
+              timezone / 3600, (timezone / 60) % 60);
+#endif /* HAVE_TM_GMTOFF */
+    else
+      snprintf(s, sizeof(s), "[%02d/%s/%04d:%02d:%02d:%02d.%06d %+03ld%02ld]",
+              date->tm_mday, months[date->tm_mon], 1900 + date->tm_year,
+              date->tm_hour, date->tm_min, date->tm_sec, (int)t->tv_usec,
 #ifdef HAVE_TM_GMTOFF
-             date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
+              date->tm_gmtoff / 3600, (date->tm_gmtoff / 60) % 60);
 #else
-             timezone / 3600, (timezone / 60) % 60);
+              timezone / 3600, (timezone / 60) % 60);
 #endif /* HAVE_TM_GMTOFF */
   }
 
@@ -99,6 +328,52 @@ cupsdGetDateTime(time_t t)          /* I - Time value */
 }
 
 
+/*
+ * 'cupsdLogFCMessage()' - Log a file checking message.
+ */
+
+void
+cupsdLogFCMessage(
+    void              *context,                /* I - Printer (if any) */
+    _cups_fc_result_t result,          /* I - Check result */
+    const char        *message)                /* I - Message to log */
+{
+  cupsd_printer_t      *p = (cupsd_printer_t *)context;
+                                       /* Printer */
+  cupsd_loglevel_t     level;          /* Log level */
+
+
+  if (result == _CUPS_FILE_CHECK_OK)
+    level = CUPSD_LOG_DEBUG2;
+  else
+    level = CUPSD_LOG_ERROR;
+
+  if (p)
+  {
+    cupsdLogMessage(level, "%s: %s", p->name, message);
+
+    if (result == _CUPS_FILE_CHECK_MISSING ||
+        result == _CUPS_FILE_CHECK_WRONG_TYPE)
+    {
+      strlcpy(p->state_message, message, sizeof(p->state_message));
+
+      if (cupsdSetPrinterReasons(p, "+cups-missing-filter-warning"))
+        cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message);
+    }
+    else if (result == _CUPS_FILE_CHECK_PERMISSIONS ||
+             result == _CUPS_FILE_CHECK_RELATIVE_PATH)
+    {
+      strlcpy(p->state_message, message, sizeof(p->state_message));
+
+      if (cupsdSetPrinterReasons(p, "+cups-insecure-filter-warning"))
+        cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE, p, NULL, "%s", message);
+    }
+  }
+  else
+    cupsdLogMessage(level, "%s", message);
+}
+
+
 #ifdef HAVE_GSSAPI
 /*
  * 'cupsdLogGSSMessage()' - Log a GSSAPI error...
@@ -120,7 +395,23 @@ cupsdLogGSSMessage(
                minor_status_string = GSS_C_EMPTY_BUFFER;
                                        /* Minor status message */
   int          ret;                    /* Return value */
+  char         buffer[8192];           /* Buffer for vsnprintf */
+
 
+  if (strchr(message, '%'))
+  {
+   /*
+    * Format the message string...
+    */
+
+    va_list    ap;                     /* Pointer to arguments */
+
+    va_start(ap, message);
+    vsnprintf(buffer, sizeof(buffer), message, ap);
+    va_end(ap);
+
+    message = buffer;
+  }
 
   msg_ctx             = 0;
   err_major_status    = gss_display_status(&err_minor_status,
@@ -131,12 +422,8 @@ cupsdLogGSSMessage(
                                           &major_status_string);
 
   if (!GSS_ERROR(err_major_status))
-    err_major_status = gss_display_status(&err_minor_status,
-                                         minor_status,
-                                         GSS_C_MECH_CODE,
-                                         GSS_C_NULL_OID,
-                                         &msg_ctx,
-                                         &minor_status_string);
+    gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE,
+                       GSS_C_NULL_OID, &msg_ctx, &minor_status_string);
 
   ret = cupsdLogMessage(level, "%s: %s, %s", message,
                        (char *)major_status_string.value,
@@ -150,176 +437,163 @@ cupsdLogGSSMessage(
 
 
 /*
- * 'cupsdLogMessage()' - Log a message to the error log file.
+ * 'cupsdLogJob()' - Log a job message.
  */
 
 int                                    /* O - 1 on success, 0 on error */
-cupsdLogMessage(int        level,      /* I - Log level */
-                const char *message,   /* I - printf-style message string */
-               ...)                    /* I - Additional args as needed */
+cupsdLogJob(cupsd_job_t *job,          /* I - Job */
+            int         level,         /* I - Log level */
+           const char  *message,       /* I - Printf-style message string */
+           ...)                        /* I - Additional arguments as needed */
 {
-  int                  len;            /* Length of message */
-  va_list              ap;             /* Argument pointer */
-  static const char    levels[] =      /* Log levels... */
-               {
-                 ' ',
-                 'X',
-                 'A',
-                 'C',
-                 'E',
-                 'W',
-                 'N',
-                 'I',
-                 'D',
-                 'd'
-               };
-#ifdef HAVE_VSYSLOG
-  static const int syslevels[] =       /* SYSLOG levels... */
-               {
-                 0,
-                 LOG_EMERG,
-                 LOG_ALERT,
-                 LOG_CRIT,
-                 LOG_ERR,
-                 LOG_WARNING,
-                 LOG_NOTICE,
-                 LOG_INFO,
-                 LOG_DEBUG,
-                 LOG_DEBUG
-               };
-#endif /* HAVE_VSYSLOG */
-  static int   linesize = 0;           /* Size of line for output file */
-  static char  *line = NULL;           /* Line for output file */
+  va_list              ap, ap2;        /* Argument pointers */
+  char                 jobmsg[1024];   /* Format string for job message */
+  int                  status;         /* Formatting status */
 
 
  /*
   * See if we want to log this message...
   */
 
-  if (TestConfigFile)
-  {
-    if (level <= CUPSD_LOG_WARN)
-    {
-      va_start(ap, message);
-      vfprintf(stderr, message, ap);
-      putc('\n', stderr);
-      va_end(ap);
-    }
-
-    return (1);
-  }
-
-  if (level > LogLevel || !ErrorLog)
-    return (1);
-
-#ifdef HAVE_VSYSLOG
- /*
-  * See if we are logging errors via syslog...
-  */
-
-  if (!strcmp(ErrorLog, "syslog"))
-  {
-    va_start(ap, message);
-    vsyslog(syslevels[level], message, ap);
-    va_end(ap);
-
+  if (TestConfigFile || !ErrorLog)
     return (1);
-  }
-#endif /* HAVE_VSYSLOG */
-
- /*
-  * Not using syslog; check the log file...
-  */
 
-  if (!check_log_file(&ErrorFile, ErrorLog))
-    return (0);
+  if ((level > LogLevel ||
+       (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
+      LogDebugHistory <= 0)
+    return (1);
 
  /*
-  * Print the log level and date/time...
+  * Format and write the log message...
   */
 
-  cupsFilePrintf(ErrorFile, "%c %s ", levels[level], cupsdGetDateTime(time(NULL)));
+  if (job)
+    snprintf(jobmsg, sizeof(jobmsg), "[Job %d] %s", job->id, message);
+  else
+    strlcpy(jobmsg, message, sizeof(jobmsg));
 
- /*
-  * Allocate the line buffer as needed...
-  */
+  va_start(ap, message);
 
-  if (!linesize)
+  do
   {
-    linesize = 8192;
-    line     = malloc(linesize);
+    va_copy(ap2, ap);
+    status = format_log_line(jobmsg, ap2);
+    va_end(ap2);
+  }
+  while (status == 0);
+
+  va_end(ap);
 
-    if (!line)
+  if (status > 0)
+  {
+    if (job &&
+        (level > LogLevel ||
+         (level == CUPSD_LOG_INFO && LogLevel < CUPSD_LOG_DEBUG)) &&
+       LogDebugHistory > 0)
     {
-      cupsFilePrintf(ErrorFile,
-                     "ERROR: Unable to allocate memory for line - %s\n",
-                     strerror(errno));
-      cupsFileFlush(ErrorFile);
+     /*
+      * Add message to the job history...
+      */
 
-      return (0);
-    }
-  }
+      cupsd_joblog_t *temp;            /* Copy of log message */
 
- /*
-  * Format the log message...
-  */
 
-  va_start(ap, message);
-  len = vsnprintf(line, linesize, message, ap);
-  va_end(ap);
+      if ((temp = malloc(sizeof(cupsd_joblog_t) + strlen(log_line))) != NULL)
+      {
+        temp->time = time(NULL);
+       strlcpy(temp->message, log_line, sizeof(temp->message));
+      }
 
- /*
-  * Resize the buffer as needed...
-  */
+      if (!job->history)
+       job->history = cupsArrayNew(NULL, NULL);
 
-  if (len >= linesize)
-  {
-    char       *temp;                  /* Temporary string pointer */
+      if (job->history && temp)
+      {
+       cupsArrayAdd(job->history, temp);
 
+       if (cupsArrayCount(job->history) > LogDebugHistory)
+       {
+        /*
+         * Remove excess messages...
+         */
 
-    len ++;
+         temp = cupsArrayFirst(job->history);
+         cupsArrayRemove(job->history, temp);
+         free(temp);
+       }
+      }
+      else if (temp)
+       free(temp);
 
-    if (len < 8192)
-      len = 8192;
-    else if (len > 65536)
-      len = 65536;
+      return (1);
+    }
+    else if (level <= LogLevel &&
+             (level != CUPSD_LOG_INFO || LogLevel >= CUPSD_LOG_DEBUG))
+      return (cupsdWriteErrorLog(level, log_line));
+    else
+      return (1);
+  }
+  else
+    return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
+                               "Unable to allocate memory for log line!"));
+}
 
-    temp = realloc(line, len);
 
-    if (temp)
-    {
-      line     = temp;
-      linesize = len;
-    }
+/*
+ * 'cupsdLogMessage()' - Log a message to the error log file.
+ */
 
-    va_start(ap, message);
-    len = vsnprintf(line, linesize, message, ap);
-    va_end(ap);
-  }
+int                                    /* O - 1 on success, 0 on error */
+cupsdLogMessage(int        level,      /* I - Log level */
+                const char *message,   /* I - printf-style message string */
+               ...)                    /* I - Additional args as needed */
+{
+  va_list              ap, ap2;        /* Argument pointers */
+  int                  status;         /* Formatting status */
 
-  if (len >= linesize)
-    len = linesize - 1;
 
  /*
-  * Then the log message...
+  * See if we want to log this message...
   */
 
-  cupsFilePuts(ErrorFile, line);
+  if ((TestConfigFile || !ErrorLog) && level <= CUPSD_LOG_WARN)
+  {
+    va_start(ap, message);
+#ifdef HAVE_VSYSLOG
+    vsyslog(LOG_LPR | syslevels[level], message, ap);
+#else
+    vfprintf(stderr, message, ap);
+    putc('\n', stderr);
+#endif /* HAVE_VSYSLOG */
+    va_end(ap);
 
- /*
-  * Then a newline...
-  */
+    return (1);
+  }
 
-  if (len > 0 && line[len - 1] != '\n')
-    cupsFilePutChar(ErrorFile, '\n');
+  if (level > LogLevel || !ErrorLog)
+    return (1);
 
  /*
-  * Flush the line to the file and return...
+  * Format and write the log message...
   */
 
-  cupsFileFlush(ErrorFile);
+  va_start(ap, message);
 
-  return (1);
+  do
+  {
+    va_copy(ap2, ap);
+    status = format_log_line(message, ap2);
+    va_end(ap2);
+  }
+  while (status == 0);
+
+  va_end(ap);
+
+  if (status > 0)
+    return (cupsdWriteErrorLog(level, log_line));
+  else
+    return (cupsdWriteErrorLog(CUPSD_LOG_ERROR,
+                               "Unable to allocate memory for log line!"));
 }
 
 
@@ -331,13 +605,159 @@ int                                      /* O - 1 on success, 0 on error */
 cupsdLogPage(cupsd_job_t *job,         /* I - Job being printed */
              const char  *page)                /* I - Page being printed */
 {
-  ipp_attribute_t *billing,            /* job-billing attribute */
-                  *hostname;           /* job-originating-host-name attribute */
+  int                  i;              /* Looping var */
+  char                 buffer[2048],   /* Buffer for page log */
+                       *bufptr,        /* Pointer into buffer */
+                       name[256];      /* Attribute name */
+  const char           *format,        /* Pointer into PageLogFormat */
+                       *nameend;       /* End of attribute name */
+  ipp_attribute_t      *attr;          /* Current attribute */
+  char                 number[256];    /* Page number */
+  int                  copies;         /* Number of copies */
+
+
+ /*
+  * Format the line going into the page log...
+  */
+
+  if (!PageLogFormat)
+    return (1);
+
+  strlcpy(number, "1", sizeof(number));
+  copies = 1;
+  sscanf(page, "%255s%d", number, &copies);
 
+  for (format = PageLogFormat, bufptr = buffer; *format; format ++)
+  {
+    if (*format == '%')
+    {
+      format ++;
+
+      switch (*format)
+      {
+        case '%' :                     /* Literal % */
+           if (bufptr < (buffer + sizeof(buffer) - 1))
+             *bufptr++ = '%';
+           break;
+
+        case 'p' :                     /* Printer name */
+           strlcpy(bufptr, job->printer->name,
+                   sizeof(buffer) - (bufptr - buffer));
+           bufptr += strlen(bufptr);
+           break;
+
+        case 'j' :                     /* Job ID */
+           snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%d", job->id);
+           bufptr += strlen(bufptr);
+           break;
+
+        case 'u' :                     /* Username */
+           strlcpy(bufptr, job->username ? job->username : "-",
+                   sizeof(buffer) - (bufptr - buffer));
+           bufptr += strlen(bufptr);
+           break;
+
+        case 'T' :                     /* Date and time */
+           strlcpy(bufptr, cupsdGetDateTime(NULL, LogTimeFormat),
+                   sizeof(buffer) - (bufptr - buffer));
+           bufptr += strlen(bufptr);
+           break;
+
+        case 'P' :                     /* Page number */
+           strlcpy(bufptr, number, sizeof(buffer) - (bufptr - buffer));
+           bufptr += strlen(bufptr);
+           break;
+
+        case 'C' :                     /* Number of copies */
+           snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%d", copies);
+           bufptr += strlen(bufptr);
+           break;
+
+        case '{' :                     /* {attribute} */
+           if ((nameend = strchr(format, '}')) != NULL &&
+               (nameend - format - 2) < (sizeof(name) - 1))
+           {
+            /*
+             * Pull the name from inside the brackets...
+             */
+
+             memcpy(name, format + 1, nameend - format - 1);
+             name[nameend - format - 1] = '\0';
+
+             format = nameend;
+
+             if ((attr = ippFindAttribute(job->attrs, name,
+                                          IPP_TAG_ZERO)) != NULL)
+             {
+              /*
+               * Add the attribute value...
+               */
+
+                for (i = 0;
+                    i < attr->num_values &&
+                        bufptr < (buffer + sizeof(buffer) - 1);
+                    i ++)
+               {
+                 if (i)
+                   *bufptr++ = ',';
+
+                 switch (attr->value_tag)
+                 {
+                   case IPP_TAG_INTEGER :
+                   case IPP_TAG_ENUM :
+                       snprintf(bufptr, sizeof(buffer) - (bufptr - buffer),
+                                "%d", attr->values[i].integer);
+                       bufptr += strlen(bufptr);
+                       break;
+
+                    case IPP_TAG_BOOLEAN :
+                       snprintf(bufptr, sizeof(buffer) - (bufptr - buffer),
+                                "%d", attr->values[i].boolean);
+                       bufptr += strlen(bufptr);
+                       break;
+
+                   case IPP_TAG_TEXTLANG :
+                   case IPP_TAG_NAMELANG :
+                   case IPP_TAG_TEXT :
+                   case IPP_TAG_NAME :
+                   case IPP_TAG_KEYWORD :
+                   case IPP_TAG_URI :
+                   case IPP_TAG_URISCHEME :
+                   case IPP_TAG_CHARSET :
+                   case IPP_TAG_LANGUAGE :
+                   case IPP_TAG_MIMETYPE :
+                       strlcpy(bufptr, attr->values[i].string.text,
+                               sizeof(buffer) - (bufptr - buffer));
+                       bufptr += strlen(bufptr);
+                       break;
+
+                   default :
+                       strlcpy(bufptr, "???",
+                               sizeof(buffer) - (bufptr - buffer));
+                       bufptr += strlen(bufptr);
+                       break;
+                 }
+               }
+             }
+             else if (bufptr < (buffer + sizeof(buffer) - 1))
+               *bufptr++ = '-';
+             break;
+           }
+
+        default :
+           if (bufptr < (buffer + sizeof(buffer) - 2))
+           {
+             *bufptr++ = '%';
+             *bufptr++ = *format;
+           }
+           break;
+      }
+    }
+    else if (bufptr < (buffer + sizeof(buffer) - 1))
+      *bufptr++ = *format;
+  }
 
-  billing  = ippFindAttribute(job->attrs, "job-billing", IPP_TAG_ZERO);
-  hostname = ippFindAttribute(job->attrs, "job-originating-host-name",
-                              IPP_TAG_ZERO);
+  *bufptr = '\0';
 
 #ifdef HAVE_VSYSLOG
  /*
@@ -346,10 +766,7 @@ cupsdLogPage(cupsd_job_t *job,             /* I - Job being printed */
 
   if (!strcmp(PageLog, "syslog"))
   {
-    syslog(LOG_INFO, "PAGE %s %s %d %s %s %s", job->printer->name,
-           job->username ? job->username : "-",
-           job->id, page, billing ? billing->values[0].string.text : "-",
-           hostname->values[0].string.text);
+    syslog(LOG_INFO, "%s", buffer);
 
     return (1);
   }
@@ -359,7 +776,7 @@ cupsdLogPage(cupsd_job_t *job,              /* I - Job being printed */
   * Not using syslog; check the log file...
   */
 
-  if (!check_log_file(&PageFile, PageLog))
+  if (!cupsdCheckLogFile(&PageFile, PageLog))
     return (0);
 
  /*
@@ -369,11 +786,7 @@ cupsdLogPage(cupsd_job_t *job,             /* I - Job being printed */
   *        billing hostname
   */
 
-  cupsFilePrintf(PageFile, "%s %s %d %s %s %s %s\n", job->printer->name,
-                job->username ? job->username : "-",
-                job->id, cupsdGetDateTime(time(NULL)), page,
-                billing ? billing->values[0].string.text : "-",
-                hostname->values[0].string.text);
+  cupsFilePrintf(PageFile, "%s\n", buffer);
   cupsFileFlush(PageFile);
 
   return (1);
@@ -388,6 +801,7 @@ int                                 /* O - 1 on success, 0 on error */
 cupsdLogRequest(cupsd_client_t *con,   /* I - Request to log */
                 http_status_t  code)   /* I - Response code */
 {
+  char temp[2048];                     /* Temporary string for URI */
   static const char * const states[] = /* HTTP client states... */
                {
                  "WAITING",
@@ -407,6 +821,114 @@ cupsdLogRequest(cupsd_client_t *con,      /* I - Request to log */
                };
 
 
+ /*
+  * Filter requests as needed...
+  */
+
+  if (AccessLogLevel < CUPSD_ACCESSLOG_ALL)
+  {
+   /*
+    * Eliminate simple GET, POST, and PUT requests...
+    */
+
+    if ((con->operation == HTTP_GET &&
+         strncmp(con->uri, "/admin/conf", 11) &&
+        strncmp(con->uri, "/admin/log", 10)) ||
+       (con->operation == HTTP_POST && !con->request &&
+        strncmp(con->uri, "/admin", 6)) ||
+       (con->operation != HTTP_GET && con->operation != HTTP_POST &&
+        con->operation != HTTP_PUT))
+      return (1);
+
+    if (con->request && con->response &&
+        (con->response->request.status.status_code < IPP_REDIRECTION_OTHER_SITE ||
+        con->response->request.status.status_code == IPP_NOT_FOUND))
+    {
+     /*
+      * Check successful requests...
+      */
+
+      ipp_op_t op = con->request->request.op.operation_id;
+      static cupsd_accesslog_t standard_ops[] =
+      {
+        CUPSD_ACCESSLOG_ALL,   /* reserved */
+        CUPSD_ACCESSLOG_ALL,   /* reserved */
+        CUPSD_ACCESSLOG_ACTIONS,/* Print-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Print-URI */
+        CUPSD_ACCESSLOG_ACTIONS,/* Validate-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Create-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Send-Document */
+        CUPSD_ACCESSLOG_ACTIONS,/* Send-URI */
+        CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Job */
+        CUPSD_ACCESSLOG_ALL,   /* Get-Job-Attributes */
+        CUPSD_ACCESSLOG_ALL,   /* Get-Jobs */
+        CUPSD_ACCESSLOG_ALL,   /* Get-Printer-Attributes */
+        CUPSD_ACCESSLOG_ACTIONS,/* Hold-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Release-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Restart-Job */
+       CUPSD_ACCESSLOG_ALL,    /* reserved */
+        CUPSD_ACCESSLOG_CONFIG,        /* Pause-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Resume-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Purge-Jobs */
+        CUPSD_ACCESSLOG_CONFIG,        /* Set-Printer-Attributes */
+        CUPSD_ACCESSLOG_ACTIONS,/* Set-Job-Attributes */
+        CUPSD_ACCESSLOG_CONFIG,        /* Get-Printer-Supported-Values */
+        CUPSD_ACCESSLOG_ACTIONS,/* Create-Printer-Subscription */
+        CUPSD_ACCESSLOG_ACTIONS,/* Create-Job-Subscription */
+        CUPSD_ACCESSLOG_ALL,   /* Get-Subscription-Attributes */
+        CUPSD_ACCESSLOG_ALL,   /* Get-Subscriptions */
+        CUPSD_ACCESSLOG_ACTIONS,/* Renew-Subscription */
+        CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Subscription */
+        CUPSD_ACCESSLOG_ALL,   /* Get-Notifications */
+        CUPSD_ACCESSLOG_ACTIONS,/* Send-Notifications */
+        CUPSD_ACCESSLOG_ALL,   /* reserved */
+        CUPSD_ACCESSLOG_ALL,   /* reserved */
+        CUPSD_ACCESSLOG_ALL,   /* reserved */
+        CUPSD_ACCESSLOG_ALL,   /* Get-Print-Support-Files */
+        CUPSD_ACCESSLOG_CONFIG,        /* Enable-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Disable-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Pause-Printer-After-Current-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Hold-New-Jobs */
+        CUPSD_ACCESSLOG_ACTIONS,/* Release-Held-New-Jobs */
+        CUPSD_ACCESSLOG_CONFIG,        /* Deactivate-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Activate-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Restart-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Shutdown-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* Startup-Printer */
+        CUPSD_ACCESSLOG_ACTIONS,/* Reprocess-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Cancel-Current-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Suspend-Current-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Resume-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* Promote-Job */
+        CUPSD_ACCESSLOG_ACTIONS        /* Schedule-Job-After */
+      };
+      static cupsd_accesslog_t cups_ops[] =
+      {
+        CUPSD_ACCESSLOG_ALL,   /* CUPS-Get-Default */
+        CUPSD_ACCESSLOG_ALL,   /* CUPS-Get-Printers */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Add-Modify-Printer */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Delete-Printer */
+        CUPSD_ACCESSLOG_ALL,   /* CUPS-Get-Classes */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Add-Modify-Class */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Delete-Class */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Accept-Jobs */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Reject-Jobs */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Set-Default */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Get-Devices */
+        CUPSD_ACCESSLOG_CONFIG,        /* CUPS-Get-PPDs */
+        CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Move-Job */
+        CUPSD_ACCESSLOG_ACTIONS,/* CUPS-Authenticate-Job */
+        CUPSD_ACCESSLOG_ALL    /* CUPS-Get-PPD */
+      };
+
+
+      if ((op <= IPP_SCHEDULE_JOB_AFTER && standard_ops[op] > AccessLogLevel) ||
+          (op >= CUPS_GET_DEFAULT && op <= CUPS_GET_PPD &&
+          cups_ops[op - CUPS_GET_DEFAULT] > AccessLogLevel))
+        return (1);
+    }
+  }
+
 #ifdef HAVE_VSYSLOG
  /*
   * See if we are logging accesses via syslog...
@@ -417,7 +939,7 @@ cupsdLogRequest(cupsd_client_t *con,        /* I - Request to log */
     syslog(LOG_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], con->uri,
+          states[con->operation], _httpEncodeURI(temp, con->uri, sizeof(temp)),
           con->http.version / 100, con->http.version % 100,
           code, CUPS_LLCAST con->bytes,
           con->request ?
@@ -433,7 +955,7 @@ cupsdLogRequest(cupsd_client_t *con,        /* I - Request to log */
   * Not using syslog; check the log file...
   */
 
-  if (!check_log_file(&AccessFile, AccessLog))
+  if (!cupsdCheckLogFile(&AccessFile, AccessLog))
     return (0);
 
  /*
@@ -442,8 +964,11 @@ cupsdLogRequest(cupsd_client_t *con,       /* I - Request to log */
 
   cupsFilePrintf(AccessFile,
                  "%s - %s %s \"%s %s HTTP/%d.%d\" %d " CUPS_LLFMT " %s %s\n",
-                con->http.hostname, con->username[0] != '\0' ? con->username : "-",
-                cupsdGetDateTime(con->start), states[con->operation], con->uri,
+                con->http.hostname,
+                con->username[0] != '\0' ? con->username : "-",
+                cupsdGetDateTime(&(con->start), LogTimeFormat),
+                states[con->operation],
+                _httpEncodeURI(temp, con->uri, sizeof(temp)),
                 con->http.version / 100, con->http.version % 100,
                 code, CUPS_LLCAST con->bytes,
                 con->request ?
@@ -459,157 +984,118 @@ cupsdLogRequest(cupsd_client_t *con,    /* I - Request to log */
 
 
 /*
- * 'check_log_file()' - Open/rotate a log file if it needs it.
+ * 'cupsdWriteErrorLog()' - Write a line to the ErrorLog.
  */
 
-static int                             /* O  - 1 if log file open */
-check_log_file(cups_file_t **lf,       /* IO - Log file */
-              const char  *logname)    /* I  - Log filename */
+int                                    /* O - 1 on success, 0 on failure */
+cupsdWriteErrorLog(int        level,   /* I - Log level */
+                   const char *message)        /* I - Message string */
 {
-  char         backname[1024],         /* Backup log filename */
-               filename[1024],         /* Formatted log filename */
-               *ptr;                   /* Pointer into filename */
-  const char   *logptr;                /* Pointer into log filename */
+  static const char    levels[] =      /* Log levels... */
+               {
+                 ' ',
+                 'X',
+                 'A',
+                 'C',
+                 'E',
+                 'W',
+                 'N',
+                 'I',
+                 'D',
+                 'd'
+               };
 
 
+#ifdef HAVE_VSYSLOG
  /*
-  * See if we have a log file to check...
+  * See if we are logging errors via syslog...
   */
 
-  if (!lf || !logname || !logname[0])
+  if (!strcmp(ErrorLog, "syslog"))
+  {
+    syslog(syslevels[level], "%s", message);
     return (1);
+  }
+#endif /* HAVE_VSYSLOG */
 
  /*
-  * Format the filename as needed...
+  * Not using syslog; check the log file...
   */
 
-  if (!*lf ||
-      (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
-       MaxLogSize > 0))
-  {
-   /*
-    * Handle format strings...
-    */
+  if (!cupsdCheckLogFile(&ErrorFile, ErrorLog))
+    return (0);
 
-    filename[sizeof(filename) - 1] = '\0';
+ /*
+  * Write the log message...
+  */
 
-    if (logname[0] != '/')
-    {
-      strlcpy(filename, ServerRoot, sizeof(filename));
-      strlcat(filename, "/", sizeof(filename));
-    }
-    else
-      filename[0] = '\0';
+  cupsFilePrintf(ErrorFile, "%c %s %s\n", levels[level],
+                 cupsdGetDateTime(NULL, LogTimeFormat), message);
+  cupsFileFlush(ErrorFile);
 
-    for (logptr = logname, ptr = filename + strlen(filename);
-         *logptr && ptr < (filename + sizeof(filename) - 1);
-        logptr ++)
-      if (*logptr == '%')
-      {
-       /*
-        * Format spec...
-       */
+  return (1);
+}
 
-        logptr ++;
-       if (*logptr == 's')
-       {
-        /*
-         * Insert the server name...
-         */
 
-         strlcpy(ptr, ServerName, sizeof(filename) - (ptr - filename));
-         ptr += strlen(ptr);
-       }
-        else
-       {
-        /*
-         * Otherwise just insert the character...
-         */
+/*
+ * 'format_log_line()' - Format a line for a log file.
+ *
+ * This function resizes a global string buffer as needed.  Each call returns
+ * a pointer to this buffer, so the contents are only good until the next call
+ * to format_log_line()...
+ */
 
-         *ptr++ = *logptr;
-       }
-      }
-      else
-       *ptr++ = *logptr;
+static int                             /* O - -1 for fatal, 0 for retry, 1 for success */
+format_log_line(const char *message,   /* I - Printf-style format string */
+                va_list    ap)         /* I - Argument list */
+{
+  int          len;                    /* Length of formatted line */
 
-    *ptr = '\0';
-  }
 
  /*
-  * See if the log file is open...
+  * Allocate the line buffer as needed...
   */
 
-  if (!*lf)
+  if (!log_linesize)
   {
-   /*
-    * Nope, open the log file...
-    */
-
-    if ((*lf = cupsFileOpen(filename, "a")) == NULL)
-    {
-     /*
-      * If the file is in CUPS_LOGDIR then try to create a missing directory...
-      */
-
-      if (!strncmp(filename, CUPS_LOGDIR, strlen(CUPS_LOGDIR)))
-      {
-        cupsdCheckPermissions(CUPS_LOGDIR, NULL, 0755, RunUser, Group, 1, -1);
+    log_linesize = 8192;
+    log_line     = malloc(log_linesize);
 
-        *lf = cupsFileOpen(filename, "a");
-      }
-
-      if (*lf == NULL)
-      {
-       syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
-              strerror(errno));
-       return (0);
-      }
-    }
+    if (!log_line)
+      return (-1);
+  }
 
-    if (strncmp(filename, "/dev/", 5))
-    {
-     /*
-      * Change ownership and permissions of non-device logs...
-      */
+ /*
+  * Format the log message...
+  */
 
-      fchown(cupsFileNumber(*lf), RunUser, Group);
-      fchmod(cupsFileNumber(*lf), LogFilePerm);
-    }
-  }
+  len = vsnprintf(log_line, log_linesize, message, ap);
 
  /*
-  * Do we need to rotate the log?
+  * Resize the buffer as needed...
   */
 
-  if (strncmp(logname, "/dev/", 5) && cupsFileTell(*lf) > MaxLogSize &&
-      MaxLogSize > 0)
+  if (len >= log_linesize && log_linesize < 65536)
   {
-   /*
-    * Rotate log file...
-    */
+    char       *temp;                  /* Temporary string pointer */
 
-    cupsFileClose(*lf);
 
-    strcpy(backname, filename);
-    strlcat(backname, ".O", sizeof(backname));
+    len ++;
 
-    unlink(backname);
-    rename(filename, backname);
+    if (len < 8192)
+      len = 8192;
+    else if (len > 65536)
+      len = 65536;
 
-    if ((*lf = cupsFileOpen(filename, "a")) == NULL)
+    temp = realloc(log_line, len);
+
+    if (temp)
     {
-      syslog(LOG_ERR, "Unable to open log file \"%s\" - %s", filename,
-             strerror(errno));
+      log_line     = temp;
+      log_linesize = len;
 
       return (0);
     }
-
-   /*
-    * Change ownership and permissions of non-device logs...
-    */
-
-    fchown(cupsFileNumber(*lf), RunUser, Group);
-    fchmod(cupsFileNumber(*lf), LogFilePerm);
   }
 
   return (1);
@@ -617,5 +1103,5 @@ check_log_file(cups_file_t **lf,   /* IO - Log file */
 
 
 /*
- * End of "$Id: log.c 6875 2007-08-27 23:25:06Z mike $".
+ * End of "$Id$".
  */