From a1797929b6bda9b86819ef84e8b6e561220e74ec Mon Sep 17 00:00:00 2001 From: msweet Date: Wed, 3 Jun 2015 19:49:54 +0000 Subject: [PATCH] Add support for logging to stderr (STR #4505) git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@12689 a1ca3aef-8c08-0410-bb20-df032aa958be --- CHANGES.txt | 2 + scheduler/conf.c | 146 ++++++++++++++++++++++++++++++++++++++++----- scheduler/conf.h | 4 +- scheduler/log.c | 10 ++++ scheduler/main.c | 25 +++++++- scheduler/server.c | 9 ++- 6 files changed, 173 insertions(+), 23 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index f4f6c4aa6..11d5bdd2e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -37,3 +37,5 @@ CHANGES IN CUPS V2.1b1 (STR #4630) - The scheduler now supports advanced ASL and journald logging when "syslog" output is configured (STR #4474) + - The scheduler now supports logging to stderr when running in the + foreground (STR #4505) diff --git a/scheduler/conf.c b/scheduler/conf.c index a58de4204..8c61870d8 100644 --- a/scheduler/conf.c +++ b/scheduler/conf.c @@ -21,6 +21,12 @@ #include #include #include +#ifdef HAVE_ASL_H +# include +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) +# define SD_JOURNAL_SUPPRESS_LOCATION +# include +#endif /* HAVE_ASL_H */ #include #ifdef HAVE_LIBPAPER @@ -269,8 +275,20 @@ cupsdCheckPermissions( "Unable to create directory \"%s\" - %s", filename, strerror(errno)); else - syslog(LOG_ERR, "Unable to create directory \"%s\" - %s", filename, - strerror(errno)); +#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 create directory \"%s\" - %s", filename, strerror(errno)); + asl_release(m); + } +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to create directory \"%s\" - %s", filename, strerror(errno)); +#else + syslog(LOG_ERR, "Unable to create directory \"%s\" - %s", filename, strerror(errno)); +#endif /* HAVE_ASL_H */ return (-1); } @@ -307,7 +325,20 @@ cupsdCheckPermissions( if (create_dir >= 0) cupsdLogMessage(CUPSD_LOG_ERROR, "\"%s\" is not a directory.", filename); else +#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, "\"%s\" is not a directory.", filename); + asl_release(m); + } +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "\"%s\" is not a directory.", filename); +#else syslog(LOG_ERR, "\"%s\" is not a directory.", filename); +#endif /* HAVE_ASL_H */ return (-1); } @@ -336,8 +367,20 @@ cupsdCheckPermissions( "Unable to change ownership of \"%s\" - %s", filename, strerror(errno)); else - syslog(LOG_ERR, "Unable to change ownership of \"%s\" - %s", filename, - strerror(errno)); +#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 change ownership of \"%s\" - %s", filename, strerror(errno)); + asl_release(m); + } +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to change ownership of \"%s\" - %s", filename, strerror(errno)); +#else + syslog(LOG_ERR, "Unable to change ownership of \"%s\" - %s", filename, strerror(errno)); +#endif /* HAVE_ASL_H */ return (1); } @@ -356,8 +399,20 @@ cupsdCheckPermissions( "Unable to change permissions of \"%s\" - %s", filename, strerror(errno)); else - syslog(LOG_ERR, "Unable to change permissions of \"%s\" - %s", filename, - strerror(errno)); +#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 change permissions of \"%s\" - %s", filename, strerror(errno)); + asl_release(m); + } +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to change permissions of \"%s\" - %s", filename, strerror(errno)); +#else + syslog(LOG_ERR, "Unable to change permissions of \"%s\" - %s", filename, strerror(errno)); +#endif /* HAVE_ASL_H */ return (1); } @@ -782,8 +837,20 @@ cupsdReadConfiguration(void) if (TestConfigFile) printf("\"%s\" contains errors.\n", CupsFilesFile); else - syslog(LOG_LPR, "Unable to read \"%s\" due to errors.", - CupsFilesFile); +#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 read \"%s\" due to errors.", CupsFilesFile); + asl_release(m); + } +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to read \"%s\" due to errors.", CupsFilesFile); +#else + syslog(LOG_LPR, "Unable to read \"%s\" due to errors.", CupsFilesFile); +#endif /* HAVE_ASL_H */ return (0); } @@ -792,8 +859,20 @@ cupsdReadConfiguration(void) cupsdLogMessage(CUPSD_LOG_INFO, "No %s, using defaults.", CupsFilesFile); else { - syslog(LOG_LPR, "Unable to open \"%s\": %s", CupsFilesFile, - strerror(errno)); +#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 \"%s\" - %s", CupsFilesFile, strerror(errno)); + asl_release(m); + +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to open \"%s\" - %s", CupsFilesFile, strerror(errno)); +#else + syslog(LOG_LPR, "Unable to open \"%s\" - %s", CupsFilesFile, strerror(errno)); +#endif /* HAVE_ASL_H */ + return (0); } @@ -806,8 +885,19 @@ cupsdReadConfiguration(void) if ((fp = cupsFileOpen(ConfigurationFile, "r")) == NULL) { - syslog(LOG_LPR, "Unable to open \"%s\": %s", ConfigurationFile, - strerror(errno)); +#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 \"%s\" - %s", ConfigurationFile, strerror(errno)); + asl_release(m); +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to open \"%s\" - %s", ConfigurationFile, strerror(errno)); +#else + syslog(LOG_LPR, "Unable to open \"%s\" - %s", ConfigurationFile, strerror(errno)); +#endif /* HAVE_ASL_H */ + return (0); } @@ -820,8 +910,20 @@ cupsdReadConfiguration(void) if (TestConfigFile) printf("\"%s\" contains errors.\n", ConfigurationFile); else - syslog(LOG_LPR, "Unable to read \"%s\" due to errors.", - ConfigurationFile); +#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 read \"%s\" due to errors.", ConfigurationFile); + asl_release(m); + } +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to read \"%s\" due to errors.", ConfigurationFile); +#else + syslog(LOG_LPR, "Unable to read \"%s\" due to errors.", ConfigurationFile); +#endif /* HAVE_ASL_H */ return (0); } @@ -948,12 +1050,24 @@ cupsdReadConfiguration(void) * Open the system log for cupsd if necessary... */ -#ifdef HAVE_VSYSLOG + if (!LogStderr) + { + if (!strcmp(AccessLog, "stderr")) + cupsdSetString(&AccessLog, "syslog"); + + if (!strcmp(ErrorLog, "stderr")) + cupsdSetString(&ErrorLog, "syslog"); + + if (!strcmp(PageLog, "stderr")) + cupsdSetString(&PageLog, "syslog"); + } + +#if defined(HAVE_VSYSLOG) && !defined(HAVE_ASL_H) && !defined(HAVE_SYSTEMD_SD_JOURNAL_H) if (!strcmp(AccessLog, "syslog") || !strcmp(ErrorLog, "syslog") || !strcmp(PageLog, "syslog")) openlog("cupsd", LOG_PID | LOG_NOWAIT | LOG_NDELAY, LOG_LPR); -#endif /* HAVE_VSYSLOG */ +#endif /* HAVE_VSYSLOG && !HAVE_ASL_H && !HAVE_SYSTEMD_SD_JOURNAL_H */ /* * Make sure each of the log files exists and gets rotated as necessary... diff --git a/scheduler/conf.h b/scheduler/conf.h index dd69bdf73..714fb4e90 100644 --- a/scheduler/conf.h +++ b/scheduler/conf.h @@ -3,7 +3,7 @@ * * Configuration file definitions for the CUPS scheduler. * - * Copyright 2007-2014 by Apple Inc. + * Copyright 2007-2015 by Apple Inc. * Copyright 1997-2007 by Easy Software Products, all rights reserved. * * These coded instructions, statements, and computer programs are the @@ -182,6 +182,8 @@ VAR cupsd_loglevel_t LogLevel VALUE(CUPSD_LOG_WARN); /* Error log level */ VAR cupsd_time_t LogTimeFormat VALUE(CUPSD_TIME_STANDARD); /* Log file time format */ +VAR cups_file_t *LogStderr VALUE(NULL); + /* Stderr file, if any */ VAR cupsd_sandboxing_t Sandboxing VALUE(CUPSD_SANDBOXING_STRICT); /* Sandboxing level */ VAR int UseSandboxing VALUE(1); diff --git a/scheduler/log.c b/scheduler/log.c index 5ade44aa3..ac229e380 100644 --- a/scheduler/log.c +++ b/scheduler/log.c @@ -124,6 +124,16 @@ cupsdCheckLogFile(cups_file_t **lf, /* IO - Log file */ if (!lf || !logname || !logname[0]) return (1); + /* + * Handle logging to stderr... + */ + + if (!strcmp(logname, "stderr")) + { + *lf = LogStderr; + return (1); + } + /* * Format the filename as needed... */ diff --git a/scheduler/main.c b/scheduler/main.c index 64675e770..d5015aa4d 100644 --- a/scheduler/main.c +++ b/scheduler/main.c @@ -3,7 +3,7 @@ * * Main loop for the CUPS scheduler. * - * Copyright 2007-2014 by Apple Inc. + * Copyright 2007-2015 by Apple Inc. * Copyright 1997-2007 by Easy Software Products, all rights reserved. * * These coded instructions, statements, and computer programs are the @@ -20,6 +20,12 @@ #define _MAIN_C_ #include "cupsd.h" #include +#ifdef HAVE_ASL_H +# include +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) +# define SD_JOURNAL_SUPPRESS_LOCATION +# include +#endif /* HAVE_ASL_H */ #include #include @@ -396,6 +402,8 @@ main(int argc, /* I - Number of command-line args */ close(i); } } + else + LogStderr = cupsFileStderr(); /* * Run in the background as needed... @@ -728,8 +736,19 @@ main(int argc, /* I - Number of command-line args */ if (!cupsdReadConfiguration()) { - syslog(LOG_LPR, "Unable to read configuration file \'%s\' - exiting!", - ConfigurationFile); +#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 read configuration file \"%s\" - exiting.", ConfigurationFile); + asl_release(m); +#elif defined(HAVE_SYSTEMD_SD_JOURNAL_H) + sd_journal_print(LOG_ERR, "Unable to read configuration file \"%s\" - exiting.", ConfigurationFile); +#else + syslog(LOG_LPR, "Unable to read configuration file \'%s\' - exiting.", ConfigurationFile); +#endif /* HAVE_ASL_H */ + break; } diff --git a/scheduler/server.c b/scheduler/server.c index a83e7c40d..e53a5c11f 100644 --- a/scheduler/server.c +++ b/scheduler/server.c @@ -142,21 +142,24 @@ cupsdStopServer(void) if (AccessFile != NULL) { - cupsFileClose(AccessFile); + if (AccessFile != LogStderr) + cupsFileClose(AccessFile); AccessFile = NULL; } if (ErrorFile != NULL) { - cupsFileClose(ErrorFile); + if (ErrorFile != LogStderr) + cupsFileClose(ErrorFile); ErrorFile = NULL; } if (PageFile != NULL) { - cupsFileClose(PageFile); + if (PageFile != LogStderr) + cupsFileClose(PageFile); PageFile = NULL; } -- 2.39.5