488bdca5c3e05835f2a384d5f9ead82a45ffdfae
[debian/amanda] / server-src / logfile.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: logfile.c,v 1.31 2006/06/01 14:54:39 martinea Exp $
29  *
30  * common log file writing routine
31  */
32 #include "amanda.h"
33 #include "arglist.h"
34 #include "util.h"
35 #include "conffile.h"
36
37 #include "logfile.h"
38
39 char *logtype_str[] = {
40     "BOGUS",
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 */
44     "DISK",                                                      /* disk */
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 */
51 };
52
53 char *program_str[] = {
54     "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker",
55     "taper", "amflush", "amdump", "amidxtaped", "amfetchdump", "amcheckdump"
56 };
57
58 int curlinenum;
59 logtype_t curlog;
60 program_t curprog;
61 char *curstr;
62
63 int multiline = -1;
64 static char *logfile;
65 static int logfd = -1;
66
67  /*
68   * Note that technically we could use two locks, a read lock
69   * from 0-EOF and a write-lock from EOF-EOF, thus leaving the
70   * beginning of the file open for read-only access.  Doing so
71   * would open us up to some race conditions unless we're pretty
72   * careful, and on top of that the functions here are so far
73   * the only accesses to the logfile, so keep things simple.
74   */
75
76 /* local functions */
77 static void open_log(void);
78 static void close_log(void);
79
80 void
81 logerror(
82     char *      msg)
83 {
84     log_add(L_FATAL, "%s", msg);
85 }
86
87
88 printf_arglist_function2(char *log_genstring, logtype_t, typ, char *, pname, char *, format)
89 {
90     va_list argp;
91     char *leader = NULL;
92     char linebuf[STR_SIZE];
93     char *xlated_fmt = dgettext("C", format);
94
95     /* format error message */
96
97     if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
98
99     if(multiline > 0) {
100         leader = stralloc("  ");                /* continuation line */
101     } else {
102         leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
103     }
104
105     arglist_start(argp, format);
106     g_vsnprintf(linebuf, SIZEOF(linebuf)-1, xlated_fmt, argp);
107                                                 /* -1 to allow for '\n' */
108     arglist_end(argp);
109     return(vstralloc(leader, linebuf, "\n", NULL));
110 }
111
112 printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
113 {
114     va_list argp;
115     int saved_errout;
116     char *leader = NULL;
117     char *xlated_fmt = gettext(format);
118     char linebuf[STR_SIZE];
119     size_t n;
120
121     /* format error message */
122
123     if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
124
125     if(multiline > 0) {
126         leader = stralloc("  ");                /* continuation line */
127     } else {
128         leader = vstralloc(logtype_str[(int)typ], " ", get_pname(), " ", NULL);
129     }
130
131     arglist_start(argp, format);
132     /* use sizeof(linebuf)-2 to save space for a trailing newline */
133     g_vsnprintf(linebuf, SIZEOF(linebuf)-2, xlated_fmt, argp);
134                                                 /* -1 to allow for '\n' */
135     arglist_end(argp);
136
137     /* avoid recursive call from error() */
138
139     saved_errout = erroutput_type;
140     erroutput_type &= ~ERR_AMANDALOG;
141
142     /* append message to the log file */
143
144     if(multiline == -1) open_log();
145
146     if (full_write(logfd, leader, strlen(leader)) < strlen(leader)) {
147         error(_("log file write error: %s"), strerror(errno));
148         /*NOTREACHED*/
149     }
150
151     amfree(leader);
152
153     /* add a newline if necessary */
154     n = strlen(linebuf);
155     if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
156     linebuf[n] = '\0';
157
158     if (full_write(logfd, linebuf, n) < n) {
159         error(_("log file write error: %s"), strerror(errno));
160         /*NOTREACHED*/
161     }
162
163     if(multiline != -1) multiline++;
164     else close_log();
165
166     erroutput_type = saved_errout;
167 }
168
169 void
170 log_start_multiline(void)
171 {
172     assert(multiline == -1);
173
174     multiline = 0;
175     open_log();
176 }
177
178
179 void
180 log_end_multiline(void)
181 {
182     assert(multiline != -1);
183     multiline = -1;
184     close_log();
185 }
186
187
188 void
189 log_rename(
190     char *      datestamp)
191 {
192     char *conf_logdir;
193     char *logfile;
194     char *fname = NULL;
195     char seq_str[NUM_STR_SIZE];
196     unsigned int seq;
197     struct stat statbuf;
198
199     if(datestamp == NULL) datestamp = "error";
200
201     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
202     logfile = vstralloc(conf_logdir, "/log", NULL);
203
204     for(seq = 0; 1; seq++) {    /* if you've got MAXINT files in your dir... */
205         g_snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
206         fname = newvstralloc(fname,
207                              logfile,
208                              ".", datestamp,
209                              ".", seq_str,
210                              NULL);
211         if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
212     }
213
214     if(rename(logfile, fname) == -1) {
215         error(_("could not rename \"%s\" to \"%s\": %s"),
216               logfile, fname, strerror(errno));
217         /*NOTREACHED*/
218     }
219
220     amfree(fname);
221     amfree(logfile);
222     amfree(conf_logdir);
223 }
224
225
226 static void
227 open_log(void)
228 {
229     char *conf_logdir;
230
231     /* now that we have a logfile, let the debug module know how to write
232      * error messages to it.  This is due to some rather obscure linking 
233      * problems. */
234     set_logerror(logerror);
235
236     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
237     logfile = vstralloc(conf_logdir, "/log", NULL);
238     amfree(conf_logdir);
239
240     logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
241
242     if(logfd == -1) {
243         error(_("could not open log file %s: %s"), logfile, strerror(errno));
244         /*NOTREACHED*/
245     }
246
247     if(amflock(logfd, "log") == -1) {
248         error(_("could not lock log file %s: %s"), logfile, strerror(errno));
249         /*NOTREACHED*/
250     }
251 }
252
253
254 static void
255 close_log(void)
256 {
257     if(amfunlock(logfd, "log") == -1) {
258         error(_("could not unlock log file %s: %s"), logfile, strerror(errno));
259         /*NOTREACHED*/
260     }
261
262     if(close(logfd) == -1) {
263         error(_("close log file: %s"), strerror(errno));
264         /*NOTREACHED*/
265     }
266
267     logfd = -1;
268     amfree(logfile);
269 }
270
271 /* WARNING: Function accesses globals curstr, curlog, and curprog
272  * WARNING: Function has static member logline, returned via globals */
273 int
274 get_logline(
275     FILE *      logf)
276 {
277     static char *logline = NULL;
278     char *logstr, *progstr;
279     char *s;
280     int ch;
281
282     amfree(logline);
283     while ((logline = agets(logf)) != NULL) {
284         if (logline[0] != '\0')
285             break;
286         amfree(logline);
287     }
288     if (logline == NULL) return 0;
289     curlinenum++;
290     s = logline;
291     ch = *s++;
292
293     /* continuation lines are special */
294
295     if(logline[0] == ' ' && logline[1] == ' ') {
296         curlog = L_CONT;
297         /* curprog stays the same */
298         skip_whitespace(s, ch);
299         curstr = s-1;
300         return 1;
301     }
302
303     /* isolate logtype field */
304
305     skip_whitespace(s, ch);
306     logstr = s - 1;
307     skip_non_whitespace(s, ch);
308     s[-1] = '\0';
309
310     /* isolate program name field */
311
312     skip_whitespace(s, ch);
313     progstr = s - 1;
314     skip_non_whitespace(s, ch);
315     s[-1] = '\0';
316
317     /* rest of line is logtype dependent string */
318
319     skip_whitespace(s, ch);
320     curstr = s - 1;
321
322     /* lookup strings */
323
324     for(curlog = L_MARKER; curlog != L_BOGUS; curlog--)
325         if(strcmp(logtype_str[curlog], logstr) == 0) break;
326
327     for(curprog = P_LAST; curprog != P_UNKNOWN; curprog--)
328         if(strcmp(program_str[curprog], progstr) == 0) break;
329
330     return 1;
331 }