Imported Upstream version 2.5.1p1
[debian/amanda] / server-src / logfile.c
index 9faf46f8da701bb2a94c14444e04a81a36d3cb3a..9741f43e9986c48ad92c3c0ea3e0b3cca3e5d337 100644 (file)
  *                        University of Maryland at College Park
  */
 /*
- * $Id: logfile.c,v 1.17.4.1.4.2.2.3 2003/01/01 23:28:56 martinea Exp $
+ * $Id: logfile.c,v 1.31.2.1 2006/09/22 11:02:13 martinea Exp $
  *
  * common log file writing routine
  */
 #include "amanda.h"
 #include "arglist.h"
+#include "util.h"
 #include "conffile.h"
 
 #include "logfile.h"
@@ -41,14 +42,16 @@ char *logtype_str[] = {
     "ERROR", "WARNING",        "INFO", "SUMMARY",       /* information messages */
     "START", "FINISH",                            /* start/end of a run */
     "DISK",                                                     /* disk */
-    "SUCCESS", "FAIL", "STRANGE",                  /* the end of a dump */
+    "SUCCESS", "PARTIAL", "FAIL", "STRANGE",       /* the end of a dump */
+    "CHUNK", "CHUNKSUCCESS",                            /* ... continued */
     "STATS",                                              /* statistics */
     "MARKER",                                    /* marker for reporter */
     "CONT"                                /* continuation line; special */
 };
 
 char *program_str[] = {
-    "UNKNOWN", "planner", "driver", "amreport", "dumper", "taper", "amflush"
+    "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker",
+    "taper", "amflush"
 };
 
 int curlinenum;
@@ -70,22 +73,48 @@ static int logfd = -1;
   */
 
 /* local functions */
-static void open_log P((void));
-static void close_log P((void));
+static void open_log(void);
+static void close_log(void);
 
-void logerror(msg)
-char *msg;
+void
+logerror(
+    char *     msg)
 {
     log_add(L_FATAL, "%s", msg);
 }
 
+
+printf_arglist_function2(char *log_genstring, logtype_t, typ, char *, pname, char *, format)
+{
+    va_list argp;
+    char *leader = NULL;
+    char linebuf[STR_SIZE];
+
+
+    /* format error message */
+
+    if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
+
+    if(multiline > 0) {
+       leader = stralloc("  ");                /* continuation line */
+    } else {
+       leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
+    }
+
+    arglist_start(argp, format);
+    vsnprintf(linebuf, SIZEOF(linebuf)-1, format, argp);
+                                               /* -1 to allow for '\n' */
+    arglist_end(argp);
+    return(vstralloc(leader, linebuf, "\n", NULL));
+}
+
 printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
 {
     va_list argp;
     int saved_errout;
     char *leader = NULL;
     char linebuf[STR_SIZE];
-    int l, n, s;
+    size_t n;
 
 
     /* format error message */
@@ -99,7 +128,7 @@ printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
     }
 
     arglist_start(argp, format);
-    ap_vsnprintf(linebuf, sizeof(linebuf)-1, format, argp);
+    vsnprintf(linebuf, SIZEOF(linebuf)-1, format, argp);
                                                /* -1 to allow for '\n' */
     arglist_end(argp);
 
@@ -112,10 +141,9 @@ printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
 
     if(multiline == -1) open_log();
 
-    for(l = 0, n = strlen(leader); l < n; l += s) {
-       if((s = write(logfd, leader + l, n - l)) < 0) {
-           error("log file write error: %s", strerror(errno));
-       }
+    if (fullwrite(logfd, leader, strlen(leader)) < 0) {
+       error("log file write error: %s", strerror(errno));
+       /*NOTREACHED*/
     }
 
     amfree(leader);
@@ -124,10 +152,9 @@ printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
     if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n';
     linebuf[n] = '\0';
 
-    for(l = 0; l < n; l += s) {
-       if((s = write(logfd, linebuf + l, n - l)) < 0) {
-           error("log file write error: %s", strerror(errno));
-       }
+    if (fullwrite(logfd, linebuf, n) < 0) {
+       error("log file write error: %s", strerror(errno));
+       /*NOTREACHED*/
     }
 
     if(multiline != -1) multiline++;
@@ -136,7 +163,8 @@ printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
     erroutput_type = saved_errout;
 }
 
-void log_start_multiline()
+void
+log_start_multiline(void)
 {
     assert(multiline == -1);
 
@@ -145,7 +173,8 @@ void log_start_multiline()
 }
 
 
-void log_end_multiline()
+void
+log_end_multiline(void)
 {
     assert(multiline != -1);
     multiline = -1;
@@ -153,8 +182,9 @@ void log_end_multiline()
 }
 
 
-void log_rename(datestamp)
-char *datestamp;
+void
+log_rename(
+    char *     datestamp)
 {
     char *conf_logdir;
     char *logfile;
@@ -174,7 +204,7 @@ char *datestamp;
     logfile = vstralloc(conf_logdir, "/log", NULL);
 
     for(seq = 0; 1; seq++) {   /* if you've got MAXINT files in your dir... */
-       ap_snprintf(seq_str, sizeof(seq_str), "%d", seq);
+       snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
        fname = newvstralloc(fname,
                             logfile,
                             ".", datestamp,
@@ -183,9 +213,10 @@ char *datestamp;
        if(stat(fname, &statbuf) == -1 && errno == ENOENT) break;
     }
 
-    if(rename(logfile, fname) != 0) {
+    if(rename(logfile, fname) == -1) {
        error("could not rename \"%s\" to \"%s\": %s",
              logfile, fname, strerror(errno));
+       /*NOTREACHED*/
     }
 
     amfree(fname);
@@ -194,7 +225,8 @@ char *datestamp;
 }
 
 
-static void open_log()
+static void
+open_log(void)
 {
     char *conf_logdir;
 
@@ -211,28 +243,37 @@ static void open_log()
 
     if(logfd == -1) {
        error("could not open log file %s: %s", logfile, strerror(errno));
+       /*NOTREACHED*/
     }
 
-    if(amflock(logfd, "log") == -1)
+    if(amflock(logfd, "log") == -1) {
        error("could not lock log file %s: %s", logfile, strerror(errno));
+       /*NOTREACHED*/
+    }
 }
 
 
-static void close_log()
+static void
+close_log(void)
 {
-    if(amfunlock(logfd, "log") == -1)
+    if(amfunlock(logfd, "log") == -1) {
        error("could not unlock log file %s: %s", logfile, strerror(errno));
+       /*NOTREACHED*/
+    }
 
-    if(close(logfd) == -1)
+    if(close(logfd) == -1) {
        error("close log file: %s", strerror(errno));
+       /*NOTREACHED*/
+    }
 
     logfd = -1;
     amfree(logfile);
 }
 
 
-int get_logline(logf)
-FILE *logf;
+int
+get_logline(
+    FILE *     logf)
 {
     static char *logline = NULL;
     char *logstr, *progstr;
@@ -240,7 +281,12 @@ FILE *logf;
     int ch;
 
     amfree(logline);
-    if((logline = agets(logf)) == NULL) return 0;
+    while ((logline = agets(logf)) != NULL) {
+       if (logline[0] != '\0')
+           break;
+       amfree(logline);
+    }
+    if (logline == NULL) return 0;
     curlinenum++;
     s = logline;
     ch = *s++;