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