root/anacron/log.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. xopenlog
  2. xcloselog
  3. make_msg
  4. slog
  5. log_e
  6. explain
  7. explain_e
  8. complain
  9. complain_e
  10. die
  11. die_e
  12. xdebug
  13. xdebug_e

   1 /*
   2     Anacron - run commands periodically
   3     Copyright (C) 1998  Itai Tzur <itzur@actcom.co.il>
   4     Copyright (C) 1999  Sean 'Shaleh' Perry <shaleh@debian.org>
   5     Copyright (C) 2004  Pascal Hakim <pasc@redellipse.net>
   6 
   7     This program is free software; you can redistribute it and/or modify
   8     it under the terms of the GNU General Public License as published by
   9     the Free Software Foundation; either version 2 of the License, or
  10     (at your option) any later version.
  11 
  12     This program is distributed in the hope that it will be useful,
  13     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15     GNU General Public License for more details.
  16 
  17     You should have received a copy of the GNU General Public License along
  18     with this program; if not, write to the Free Software Foundation, Inc.,
  19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20  
  21     The GNU General Public License can also be found in the file
  22     `COPYING' that comes with the Anacron source distribution.
  23 */
  24 
  25 
  26 /* Error logging
  27  *
  28  * We have two levels of logging (plus debugging if DEBUG is defined):
  29  * "explain" level for informational messages, and "complain" level for errors.
  30  *
  31  * We log everything to syslog, see the top of global.h for relevant
  32  * definitions.
  33  *
  34  * Stderr gets "complain" messages when we're in the foreground,
  35  * and "explain" messages when we're in the foreground, and not "quiet".
  36  */
  37 
  38 #include <unistd.h>
  39 #include <syslog.h>
  40 #include <stdio.h>
  41 #include <stdarg.h>
  42 #include <errno.h>
  43 #include <signal.h>
  44 #include <sys/types.h>
  45 #include <string.h>
  46 #include <stdlib.h>
  47 #include "global.h"
  48 
  49 static char truncated[] = " (truncated)";
  50 static char msg[MAX_MSG + 1];
  51 static int log_open = 0;
  52 
  53 /* Number of complaints that we've seen */
  54 int complaints = 0;
  55 
  56 static void
  57 xopenlog(void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  58 {
  59     if (!log_open)
  60     {
  61         openlog(program_name, LOG_PID, SYSLOG_FACILITY);
  62         log_open = 1;
  63     }
  64 }
  65 
  66 void
  67 xcloselog(void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  68 {
  69     if (log_open) closelog();
  70     log_open = 0;
  71 }
  72 
  73 static void
  74 make_msg(const char *fmt, va_list args)
     /* [previous][next][first][last][top][bottom][index][help]  */
  75 /* Construct the message string from its parts */
  76 {
  77     int len;
  78 
  79     /* There's some confusion in the documentation about what vsnprintf
  80      * returns when the buffer overflows.  Hmmm... */
  81     len = vsnprintf(msg, sizeof(msg), fmt, args);
  82     if (len >= sizeof(msg) - 1)
  83         strcpy(msg + sizeof(msg) - sizeof(truncated), truncated);
  84 }
  85 
  86 static void
  87 slog(int priority, const char *fmt, va_list args)
     /* [previous][next][first][last][top][bottom][index][help]  */
  88 /* Log a message, described by "fmt" and "args", with the specified
  89  * "priority". */
  90 {
  91     make_msg(fmt, args);
  92     xopenlog();
  93     syslog(priority, "%s", msg);
  94     if (!in_background)
  95     {
  96         if (priority == EXPLAIN_LEVEL && !quiet)
  97             fprintf(stderr, "%s\n", msg);
  98         else if (priority == COMPLAIN_LEVEL)
  99             fprintf(stderr, "%s: %s\n", program_name, msg);
 100     }
 101 }
 102 
 103 static void
 104 log_e(int priority, const char *fmt, va_list args)
     /* [previous][next][first][last][top][bottom][index][help]  */
 105 /* Same as slog(), but also appends an error description corresponding
 106  * to "errno". */
 107 {
 108     int saved_errno;
 109 
 110     saved_errno = errno;
 111     make_msg(fmt, args);
 112     xopenlog();
 113     syslog(priority, "%s: %s", msg, strerror(saved_errno));
 114     if (!in_background)
 115     {
 116         if (priority == EXPLAIN_LEVEL && !quiet)
 117             fprintf(stderr, "%s: %s\n", msg, strerror(saved_errno));
 118         else if (priority == COMPLAIN_LEVEL)
 119             fprintf(stderr, "%s: %s: %s\n",
 120                     program_name, msg, strerror(saved_errno));
 121     }
 122 }
 123 
 124 void
 125 explain(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 126 /* Log an "explain" level message */
 127 {
 128     va_list args;
 129 
 130     va_start(args, fmt);
 131     slog(EXPLAIN_LEVEL, fmt, args);
 132     va_end(args);
 133 }
 134 
 135 void
 136 explain_e(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 137 /* Log an "explain" level message, with an error description */
 138 {
 139     va_list args;
 140 
 141     va_start(args, fmt);
 142     log_e(EXPLAIN_LEVEL, fmt, args);
 143     va_end(args);
 144 }
 145 
 146 void
 147 complain(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 148 /* Log a "complain" level message */
 149 {
 150     va_list args;
 151 
 152     va_start(args, fmt);
 153     slog(COMPLAIN_LEVEL, fmt, args);
 154     va_end(args);
 155 
 156     complaints += 1;
 157 }
 158 
 159 void
 160 complain_e(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 161 /* Log a "complain" level message, with an error description */
 162 {
 163     va_list args;
 164 
 165     va_start(args, fmt);
 166     log_e(COMPLAIN_LEVEL, fmt, args);
 167     va_end(args);
 168 
 169     complaints += 1;
 170 }
 171 
 172 void
 173 die(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 174 /* Log a "complain" level message, and exit */
 175 {
 176     va_list args;
 177 
 178     va_start(args, fmt);
 179     slog(COMPLAIN_LEVEL, fmt, args);
 180     va_end(args);
 181     if (getpid() == primary_pid) complain("Aborted");
 182 
 183     exit(FAILURE_EXIT);
 184 }
 185 
 186 void
 187 die_e(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 188 /* Log a "complain" level message, with an error description, and exit */
 189 {
 190     va_list args;
 191 
 192     va_start(args, fmt);
 193     log_e(COMPLAIN_LEVEL, fmt, args);
 194     va_end(args);
 195     if (getpid() == primary_pid) complain("Aborted");
 196 
 197     exit(FAILURE_EXIT);
 198 }
 199 
 200 #ifdef DEBUG
 201 
 202 /* These are called through the Debug() and Debug_e() macros, defined
 203  * in global.h */
 204 
 205 void
 206 xdebug(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 207 {
 208     va_list args;
 209 
 210     va_start(args, fmt);
 211     slog(DEBUG_LEVEL, fmt, args);
 212     va_end(args);
 213 }
 214 
 215 void
 216 xdebug_e(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 217 {
 218     va_list args;
 219 
 220     va_start(args, fmt);
 221     log_e(DEBUG_LEVEL, fmt, args);
 222     va_end(args);
 223 }
 224 
 225 #endif  /* DEBUG */

/* [previous][next][first][last][top][bottom][index][help]  */