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.31 2006/06/01 14:54:39 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 /* the end of a dump */
46 "DONE", "PART", "PARTPARTIAL", "SUCCESS", "PARTIAL", "FAIL", "STRANGE",
47 "CHUNK", "CHUNKSUCCESS", /* ... continued */
48 "STATS", /* statistics */
49 "MARKER", /* marker for reporter */
50 "CONT" /* continuation line; special */
53 char *program_str[] = {
54 "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker",
55 "taper", "amflush", "amdump", "amidxtaped", "amfetchdump", "amcheckdump",
66 static int logfd = -1;
69 * Note that technically we could use two locks, a read lock
70 * from 0-EOF and a write-lock from EOF-EOF, thus leaving the
71 * beginning of the file open for read-only access. Doing so
72 * would open us up to some race conditions unless we're pretty
73 * careful, and on top of that the functions here are so far
74 * the only accesses to the logfile, so keep things simple.
78 static void open_log(void);
79 static void close_log(void);
83 GLogLevelFlags log_level,
86 logtype_t logtype = L_ERROR;
89 case G_LOG_LEVEL_ERROR:
90 case G_LOG_LEVEL_CRITICAL:
97 log_add(logtype, "%s", message);
100 static void log_add_full_v(logtype_t typ, char *pname, char *format, va_list argp)
103 char *xlated_fmt = gettext(format);
104 char linebuf[STR_SIZE];
106 static gboolean in_log_add = 0;
108 /* avoid recursion */
112 /* format error message */
114 if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
117 leader = stralloc(" "); /* continuation line */
119 leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
122 /* use sizeof(linebuf)-2 to save space for a trailing newline */
123 g_vsnprintf(linebuf, SIZEOF(linebuf)-2, xlated_fmt, argp);
124 /* -1 to allow for '\n' */
126 /* avoid recursive call from error() */
130 /* append message to the log file */
132 if(multiline == -1) open_log();
134 if (full_write(logfd, leader, strlen(leader)) < strlen(leader)) {
135 error(_("log file write error: %s"), strerror(errno));
141 /* add a newline if necessary */
143 if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
146 if (full_write(logfd, linebuf, n) < n) {
147 error(_("log file write error: %s"), strerror(errno));
151 if(multiline != -1) multiline++;
157 void log_add(logtype_t typ, char *format, ...)
161 arglist_start(argp, format);
162 log_add_full_v(typ, get_pname(), format, argp);
166 void log_add_full(logtype_t typ, char *pname, char *format, ...)
170 arglist_start(argp, format);
171 log_add_full_v(typ, pname, format, argp);
176 log_start_multiline(void)
178 assert(multiline == -1);
186 log_end_multiline(void)
188 assert(multiline != -1);
201 char seq_str[NUM_STR_SIZE];
205 if(datestamp == NULL) datestamp = "error";
207 conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
208 logfile = vstralloc(conf_logdir, "/log", NULL);
210 for(seq = 0; 1; seq++) { /* if you've got MAXINT files in your dir... */
211 g_snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
212 fname = newvstralloc(fname,
217 if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
220 if(rename(logfile, fname) == -1) {
221 error(_("could not rename \"%s\" to \"%s\": %s"),
222 logfile, fname, strerror(errno));
237 conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
238 logfile = vstralloc(conf_logdir, "/log", NULL);
241 logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
244 error(_("could not open log file %s: %s"), logfile, strerror(errno));
248 if(amflock(logfd, "log") == -1) {
249 error(_("could not lock log file %s: %s"), logfile, strerror(errno));
258 if(amfunlock(logfd, "log") == -1) {
259 error(_("could not unlock log file %s: %s"), logfile, strerror(errno));
263 if(close(logfd) == -1) {
264 error(_("close log file: %s"), strerror(errno));
272 /* WARNING: Function accesses globals curstr, curlog, and curprog
273 * WARNING: Function has static member logline, returned via globals */
278 static char *logline = NULL;
279 char *logstr, *progstr;
284 while ((logline = agets(logf)) != NULL) {
285 if (logline[0] != '\0')
289 if (logline == NULL) return 0;
294 /* continuation lines are special */
296 if(logline[0] == ' ' && logline[1] == ' ') {
298 /* curprog stays the same */
299 skip_whitespace(s, ch);
304 /* isolate logtype field */
306 skip_whitespace(s, ch);
308 skip_non_whitespace(s, ch);
311 /* isolate program name field */
313 skip_whitespace(s, ch);
315 skip_non_whitespace(s, ch);
318 /* rest of line is logtype dependent string */
320 skip_whitespace(s, ch);
325 for(curlog = L_MARKER; curlog != L_BOGUS; curlog--)
326 if(strcmp(logtype_str[curlog], logstr) == 0) break;
328 for(curprog = P_LAST; curprog != P_UNKNOWN; curprog--)
329 if(strcmp(program_str[curprog], progstr) == 0) break;