"Fossies" - the Fresh Open Source Software Archive

Member "anacron-2.5.3/log.c" (9 Dec 2007, 4994 Bytes) of package /linux/misc/old/anacron.2.5.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "log.c" see the Fossies "Dox" file reference documentation.

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