2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
28 * $Id: logfile.c,v 1.29 2005/12/04 22:56:55 martinea Exp $
30 * common log file writing routine
39 char *logtype_str[] = {
41 "FATAL", /* program died for some reason, used by error() */
42 "ERROR", "WARNING", "INFO", "SUMMARY", /* information messages */
43 "START", "FINISH", /* start/end of a run */
45 "SUCCESS", "PARTIAL", "FAIL", "STRANGE", /* the end of a dump */
46 "CHUNK", "CHUNKSUCCESS", /* ... continued */
47 "STATS", /* statistics */
48 "MARKER", /* marker for reporter */
49 "CONT" /* continuation line; special */
52 char *program_str[] = {
53 "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker",
64 static int logfd = -1;
67 * Note that technically we could use two locks, a read lock
68 * from 0-EOF and a write-lock from EOF-EOF, thus leaving the
69 * beginning of the file open for read-only access. Doing so
70 * would open us up to some race conditions unless we're pretty
71 * careful, and on top of that the functions here are so far
72 * the only accesses to the logfile, so keep things simple.
76 static void open_log P((void));
77 static void close_log P((void));
82 log_add(L_FATAL, "%s", msg);
86 printf_arglist_function2(char *log_genstring, logtype_t, typ, char *, pname, char *, format)
90 char linebuf[STR_SIZE];
93 /* format error message */
95 if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
98 leader = stralloc(" "); /* continuation line */
100 leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
103 arglist_start(argp, format);
104 vsnprintf(linebuf, sizeof(linebuf)-1, format, argp);
105 /* -1 to allow for '\n' */
106 return(vstralloc(leader, linebuf, "\n", NULL));
109 printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
114 char linebuf[STR_SIZE];
118 /* format error message */
120 if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
123 leader = stralloc(" "); /* continuation line */
125 leader = vstralloc(logtype_str[(int)typ], " ", get_pname(), " ", NULL);
128 arglist_start(argp, format);
129 vsnprintf(linebuf, sizeof(linebuf)-1, format, argp);
130 /* -1 to allow for '\n' */
133 /* avoid recursive call from error() */
135 saved_errout = erroutput_type;
136 erroutput_type &= ~ERR_AMANDALOG;
138 /* append message to the log file */
140 if(multiline == -1) open_log();
142 if (fullwrite(logfd, leader, strlen(leader)) < 0)
143 error("log file write error: %s", strerror(errno));
148 if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
151 if (fullwrite(logfd, linebuf, n) < 0)
152 error("log file write error: %s", strerror(errno));
154 if(multiline != -1) multiline++;
157 erroutput_type = saved_errout;
160 void log_start_multiline()
162 assert(multiline == -1);
169 void log_end_multiline()
171 assert(multiline != -1);
177 void log_rename(datestamp)
183 char seq_str[NUM_STR_SIZE];
187 if(datestamp == NULL) datestamp = "error";
189 conf_logdir = getconf_str(CNF_LOGDIR);
190 if (*conf_logdir == '/') {
191 conf_logdir = stralloc(conf_logdir);
193 conf_logdir = stralloc2(config_dir, conf_logdir);
195 logfile = vstralloc(conf_logdir, "/log", NULL);
197 for(seq = 0; 1; seq++) { /* if you've got MAXINT files in your dir... */
198 snprintf(seq_str, sizeof(seq_str), "%d", seq);
199 fname = newvstralloc(fname,
204 if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
207 if(rename(logfile, fname) == -1)
208 error("could not rename \"%s\" to \"%s\": %s",
209 logfile, fname, strerror(errno));
217 static void open_log()
221 conf_logdir = getconf_str(CNF_LOGDIR);
222 if (*conf_logdir == '/') {
223 conf_logdir = stralloc(conf_logdir);
225 conf_logdir = stralloc2(config_dir, conf_logdir);
227 logfile = vstralloc(conf_logdir, "/log", NULL);
230 logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
233 error("could not open log file %s: %s", logfile, strerror(errno));
235 if(amflock(logfd, "log") == -1)
236 error("could not lock log file %s: %s", logfile, strerror(errno));
240 static void close_log()
242 if(amfunlock(logfd, "log") == -1)
243 error("could not unlock log file %s: %s", logfile, strerror(errno));
245 if(close(logfd) == -1)
246 error("close log file: %s", strerror(errno));
253 int get_logline(logf)
256 static char *logline = NULL;
257 char *logstr, *progstr;
262 if((logline = agets(logf)) == NULL) return 0;
267 /* continuation lines are special */
269 if(logline[0] == ' ' && logline[1] == ' ') {
271 /* curprog stays the same */
272 skip_whitespace(s, ch);
277 /* isolate logtype field */
279 skip_whitespace(s, ch);
281 skip_non_whitespace(s, ch);
284 /* isolate program name field */
286 skip_whitespace(s, ch);
288 skip_non_whitespace(s, ch);
291 /* rest of line is logtype dependent string */
293 skip_whitespace(s, ch);
298 for(curlog = L_MARKER; curlog != L_BOGUS; curlog--)
299 if(strcmp(logtype_str[curlog], logstr) == 0) break;
301 for(curprog = P_LAST; curprog != P_UNKNOWN; curprog--)
302 if(strcmp(program_str[curprog], progstr) == 0) break;