2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
4 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of U.M. not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. U.M. makes no representations about the
14 * suitability of this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
17 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Author: James da Silva, Systems Design and Analysis Group
25 * Computer Science Department
26 * University of Maryland at College Park
29 * $Id: logfile.c,v 1.31 2006/06/01 14:54:39 martinea Exp $
31 * common log file writing routine
40 char *logtype_str[] = {
42 "FATAL", /* program died for some reason, used by error() */
43 "ERROR", "WARNING", "INFO", "SUMMARY", /* information messages */
44 "START", "FINISH", /* start/end of a run */
46 /* the end of a dump */
47 "DONE", "PART", "PARTPARTIAL", "SUCCESS", "PARTIAL", "FAIL", "STRANGE",
48 "CHUNK", "CHUNKSUCCESS", /* ... continued */
49 "STATS", /* statistics */
50 "MARKER", /* marker for reporter */
51 "CONT" /* continuation line; special */
54 char *program_str[] = {
55 "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker",
56 "taper", "amflush", "amdump", "amidxtaped", "amfetchdump", "amcheckdump",
67 static int logfd = -1;
70 * Note that technically we could use two locks, a read lock
71 * from 0-EOF and a write-lock from EOF-EOF, thus leaving the
72 * beginning of the file open for read-only access. Doing so
73 * would open us up to some race conditions unless we're pretty
74 * careful, and on top of that the functions here are so far
75 * the only accesses to the logfile, so keep things simple.
79 static void open_log(void);
80 static void close_log(void);
84 GLogLevelFlags log_level,
87 logtype_t logtype = L_ERROR;
90 case G_LOG_LEVEL_ERROR:
91 case G_LOG_LEVEL_CRITICAL:
98 log_add(logtype, "%s", message);
101 static void log_add_full_v(logtype_t typ, char *pname, char *format, va_list argp)
104 char *xlated_fmt = gettext(format);
105 char linebuf[STR_SIZE];
107 static gboolean in_log_add = 0;
109 /* avoid recursion */
113 /* format error message */
115 if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
118 leader = stralloc(" "); /* continuation line */
120 leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
123 /* use sizeof(linebuf)-2 to save space for a trailing newline */
124 g_vsnprintf(linebuf, SIZEOF(linebuf)-2, xlated_fmt, argp);
125 /* -1 to allow for '\n' */
127 /* avoid recursive call from error() */
131 /* append message to the log file */
133 if(multiline == -1) open_log();
135 if (full_write(logfd, leader, strlen(leader)) < strlen(leader)) {
136 error(_("log file write error: %s"), strerror(errno));
142 /* add a newline if necessary */
144 if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
147 if (full_write(logfd, linebuf, n) < n) {
148 error(_("log file write error: %s"), strerror(errno));
152 if(multiline != -1) multiline++;
158 void log_add(logtype_t typ, char *format, ...)
162 arglist_start(argp, format);
163 log_add_full_v(typ, get_pname(), format, argp);
167 void log_add_full(logtype_t typ, char *pname, char *format, ...)
171 arglist_start(argp, format);
172 log_add_full_v(typ, pname, format, argp);
177 log_start_multiline(void)
179 assert(multiline == -1);
187 log_end_multiline(void)
189 assert(multiline != -1);
202 char seq_str[NUM_STR_SIZE];
206 if(datestamp == NULL) datestamp = "error";
208 conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
209 logfile = vstralloc(conf_logdir, "/log", NULL);
211 for(seq = 0; 1; seq++) { /* if you've got MAXINT files in your dir... */
212 g_snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
213 fname = newvstralloc(fname,
218 if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
221 if(rename(logfile, fname) == -1) {
222 g_debug(_("could not rename \"%s\" to \"%s\": %s"),
223 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 static size_t line_size = 0;
282 char *logstr, *progstr;
289 logline = g_malloc(line_size);
294 lline = fgets(logline + loffset, line_size - loffset, logf);
298 if (strlen(logline) == line_size -1 &&
299 logline[strlen(logline)-1] != '\n') {
301 logline = g_realloc(logline, line_size);
302 loffset = strlen(logline);
303 } else if (strlen(logline) == 0 ||
304 (strlen(logline) == 1 && logline[0] == '\n')) {
306 break; /* good line */
308 logline[loffset] = '\0';
310 if (logline[0] == '\0')
315 if (logline[n-1] == '\n') logline[n-1] = '\0';
321 /* continuation lines are special */
323 if(logline[0] == ' ' && logline[1] == ' ') {
325 /* curprog stays the same */
326 skip_whitespace(s, ch);
331 /* isolate logtype field */
333 skip_whitespace(s, ch);
335 skip_non_whitespace(s, ch);
338 /* isolate program name field */
340 skip_whitespace(s, ch);
342 skip_non_whitespace(s, ch);
345 /* rest of line is logtype dependent string */
347 skip_whitespace(s, ch);
352 for(curlog = L_MARKER; curlog != L_BOGUS; curlog--)
353 if(strcmp(logtype_str[curlog], logstr) == 0) break;
355 for(curprog = P_LAST; curprog != P_UNKNOWN; curprog--)
356 if(strcmp(program_str[curprog], progstr) == 0) break;