Imported Upstream version 3.1.0
[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 amanda_log_trace_log(
82     GLogLevelFlags log_level,
83     const gchar *message)
84 {
85     logtype_t logtype = L_ERROR;
86
87     switch (log_level) {
88         case G_LOG_LEVEL_ERROR:
89         case G_LOG_LEVEL_CRITICAL:
90             logtype = L_FATAL;
91             break;
92
93         default:
94             return;
95     }
96     log_add(logtype, "%s", message);
97 }
98
99
100 printf_arglist_function2(char *log_genstring, logtype_t, typ, char *, pname, char *, format)
101 {
102     va_list argp;
103     char *leader = NULL;
104     char linebuf[STR_SIZE];
105     char *xlated_fmt = dgettext("C", format);
106
107     /* format error message */
108
109     if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
110
111     if(multiline > 0) {
112         leader = stralloc("  ");                /* continuation line */
113     } else {
114         leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
115     }
116
117     arglist_start(argp, format);
118     g_vsnprintf(linebuf, SIZEOF(linebuf)-1, xlated_fmt, argp);
119                                                 /* -1 to allow for '\n' */
120     arglist_end(argp);
121     return(vstralloc(leader, linebuf, "\n", NULL));
122 }
123
124 printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
125 {
126     va_list argp;
127     char *leader = NULL;
128     char *xlated_fmt = gettext(format);
129     char linebuf[STR_SIZE];
130     size_t n;
131     static gboolean in_log_add = 0;
132
133     /* avoid recursion */
134     if (in_log_add)
135         return;
136
137     /* format error message */
138
139     if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
140
141     if(multiline > 0) {
142         leader = stralloc("  ");                /* continuation line */
143     } else {
144         leader = vstralloc(logtype_str[(int)typ], " ", get_pname(), " ", NULL);
145     }
146
147     arglist_start(argp, format);
148     /* use sizeof(linebuf)-2 to save space for a trailing newline */
149     g_vsnprintf(linebuf, SIZEOF(linebuf)-2, xlated_fmt, argp);
150                                                 /* -1 to allow for '\n' */
151     arglist_end(argp);
152
153     /* avoid recursive call from error() */
154
155     in_log_add = 1;
156
157     /* append message to the log file */
158
159     if(multiline == -1) open_log();
160
161     if (full_write(logfd, leader, strlen(leader)) < strlen(leader)) {
162         error(_("log file write error: %s"), strerror(errno));
163         /*NOTREACHED*/
164     }
165
166     amfree(leader);
167
168     /* add a newline if necessary */
169     n = strlen(linebuf);
170     if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
171     linebuf[n] = '\0';
172
173     if (full_write(logfd, linebuf, n) < n) {
174         error(_("log file write error: %s"), strerror(errno));
175         /*NOTREACHED*/
176     }
177
178     if(multiline != -1) multiline++;
179     else close_log();
180
181     in_log_add = 0;
182 }
183
184 void
185 log_start_multiline(void)
186 {
187     assert(multiline == -1);
188
189     multiline = 0;
190     open_log();
191 }
192
193
194 void
195 log_end_multiline(void)
196 {
197     assert(multiline != -1);
198     multiline = -1;
199     close_log();
200 }
201
202
203 void
204 log_rename(
205     char *      datestamp)
206 {
207     char *conf_logdir;
208     char *logfile;
209     char *fname = NULL;
210     char seq_str[NUM_STR_SIZE];
211     unsigned int seq;
212     struct stat statbuf;
213
214     if(datestamp == NULL) datestamp = "error";
215
216     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
217     logfile = vstralloc(conf_logdir, "/log", NULL);
218
219     for(seq = 0; 1; seq++) {    /* if you've got MAXINT files in your dir... */
220         g_snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
221         fname = newvstralloc(fname,
222                              logfile,
223                              ".", datestamp,
224                              ".", seq_str,
225                              NULL);
226         if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
227     }
228
229     if(rename(logfile, fname) == -1) {
230         error(_("could not rename \"%s\" to \"%s\": %s"),
231               logfile, fname, strerror(errno));
232         /*NOTREACHED*/
233     }
234
235     amfree(fname);
236     amfree(logfile);
237     amfree(conf_logdir);
238 }
239
240
241 static void
242 open_log(void)
243 {
244     char *conf_logdir;
245
246     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
247     logfile = vstralloc(conf_logdir, "/log", NULL);
248     amfree(conf_logdir);
249
250     logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
251
252     if(logfd == -1) {
253         error(_("could not open log file %s: %s"), logfile, strerror(errno));
254         /*NOTREACHED*/
255     }
256
257     if(amflock(logfd, "log") == -1) {
258         error(_("could not lock log file %s: %s"), logfile, strerror(errno));
259         /*NOTREACHED*/
260     }
261 }
262
263
264 static void
265 close_log(void)
266 {
267     if(amfunlock(logfd, "log") == -1) {
268         error(_("could not unlock log file %s: %s"), logfile, strerror(errno));
269         /*NOTREACHED*/
270     }
271
272     if(close(logfd) == -1) {
273         error(_("close log file: %s"), strerror(errno));
274         /*NOTREACHED*/
275     }
276
277     logfd = -1;
278     amfree(logfile);
279 }
280
281 /* WARNING: Function accesses globals curstr, curlog, and curprog
282  * WARNING: Function has static member logline, returned via globals */
283 int
284 get_logline(
285     FILE *      logf)
286 {
287     static char *logline = NULL;
288     char *logstr, *progstr;
289     char *s;
290     int ch;
291
292     amfree(logline);
293     while ((logline = agets(logf)) != NULL) {
294         if (logline[0] != '\0')
295             break;
296         amfree(logline);
297     }
298     if (logline == NULL) return 0;
299     curlinenum++;
300     s = logline;
301     ch = *s++;
302
303     /* continuation lines are special */
304
305     if(logline[0] == ' ' && logline[1] == ' ') {
306         curlog = L_CONT;
307         /* curprog stays the same */
308         skip_whitespace(s, ch);
309         curstr = s-1;
310         return 1;
311     }
312
313     /* isolate logtype field */
314
315     skip_whitespace(s, ch);
316     logstr = s - 1;
317     skip_non_whitespace(s, ch);
318     s[-1] = '\0';
319
320     /* isolate program name field */
321
322     skip_whitespace(s, ch);
323     progstr = s - 1;
324     skip_non_whitespace(s, ch);
325     s[-1] = '\0';
326
327     /* rest of line is logtype dependent string */
328
329     skip_whitespace(s, ch);
330     curstr = s - 1;
331
332     /* lookup strings */
333
334     for(curlog = L_MARKER; curlog != L_BOGUS; curlog--)
335         if(strcmp(logtype_str[curlog], logstr) == 0) break;
336
337     for(curprog = P_LAST; curprog != P_UNKNOWN; curprog--)
338         if(strcmp(program_str[curprog], progstr) == 0) break;
339
340     return 1;
341 }