lintian doesn't like orphan packages with uploaders...
[debian/amanda] / server-src / logfile.c
index 488bdca5c3e05835f2a384d5f9ead82a45ffdfae..8bab3cfdf91a536aa8d8f897760db22b4bf74f9e 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
  * Copyright (c) 1991-1998 University of Maryland at College Park
+ * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
  * All Rights Reserved.
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
@@ -52,7 +53,8 @@ char *logtype_str[] = {
 
 char *program_str[] = {
     "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker",
-    "taper", "amflush", "amdump", "amidxtaped", "amfetchdump", "amcheckdump"
+    "taper", "amflush", "amdump", "amidxtaped", "amfetchdump", "amcheckdump",
+    "amvault",
 };
 
 int curlinenum;
@@ -78,45 +80,35 @@ static void open_log(void);
 static void close_log(void);
 
 void
-logerror(
-    char *     msg)
+amanda_log_trace_log(
+    GLogLevelFlags log_level,
+    const gchar *message)
 {
-    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];
-    char *xlated_fmt = dgettext("C", format);
-
-    /* format error message */
+    logtype_t logtype = L_ERROR;
 
-    if((int)typ <= (int)L_BOGUS || (int)typ > (int)L_MARKER) typ = L_BOGUS;
+    switch (log_level) {
+       case G_LOG_LEVEL_ERROR:
+       case G_LOG_LEVEL_CRITICAL:
+           logtype = L_FATAL;
+           break;
 
-    if(multiline > 0) {
-       leader = stralloc("  ");                /* continuation line */
-    } else {
-       leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
+       default:
+           return;
     }
-
-    arglist_start(argp, format);
-    g_vsnprintf(linebuf, SIZEOF(linebuf)-1, xlated_fmt, argp);
-                                               /* -1 to allow for '\n' */
-    arglist_end(argp);
-    return(vstralloc(leader, linebuf, "\n", NULL));
+    log_add(logtype, "%s", message);
 }
 
-printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
+static void log_add_full_v(logtype_t typ, char *pname, char *format, va_list argp)
 {
-    va_list argp;
-    int saved_errout;
     char *leader = NULL;
     char *xlated_fmt = gettext(format);
     char linebuf[STR_SIZE];
     size_t n;
+    static gboolean in_log_add = 0;
+
+    /* avoid recursion */
+    if (in_log_add)
+       return;
 
     /* format error message */
 
@@ -125,19 +117,16 @@ printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
     if(multiline > 0) {
        leader = stralloc("  ");                /* continuation line */
     } else {
-       leader = vstralloc(logtype_str[(int)typ], " ", get_pname(), " ", NULL);
+       leader = vstralloc(logtype_str[(int)typ], " ", pname, " ", NULL);
     }
 
-    arglist_start(argp, format);
     /* use sizeof(linebuf)-2 to save space for a trailing newline */
     g_vsnprintf(linebuf, SIZEOF(linebuf)-2, xlated_fmt, argp);
                                                /* -1 to allow for '\n' */
-    arglist_end(argp);
 
     /* avoid recursive call from error() */
 
-    saved_errout = erroutput_type;
-    erroutput_type &= ~ERR_AMANDALOG;
+    in_log_add = 1;
 
     /* append message to the log file */
 
@@ -163,7 +152,25 @@ printf_arglist_function1(void log_add, logtype_t, typ, char *, format)
     if(multiline != -1) multiline++;
     else close_log();
 
-    erroutput_type = saved_errout;
+    in_log_add = 0;
+}
+
+void log_add(logtype_t typ, char *format, ...)
+{
+    va_list argp;
+
+    arglist_start(argp, format);
+    log_add_full_v(typ, get_pname(), format, argp);
+    arglist_end(argp);
+}
+
+void log_add_full(logtype_t typ, char *pname, char *format, ...)
+{
+    va_list argp;
+
+    arglist_start(argp, format);
+    log_add_full_v(typ, pname, format, argp);
+    arglist_end(argp);
 }
 
 void
@@ -212,9 +219,8 @@ log_rename(
     }
 
     if(rename(logfile, fname) == -1) {
-       error(_("could not rename \"%s\" to \"%s\": %s"),
+       g_debug(_("could not rename \"%s\" to \"%s\": %s"),
              logfile, fname, strerror(errno));
-       /*NOTREACHED*/
     }
 
     amfree(fname);
@@ -228,11 +234,6 @@ open_log(void)
 {
     char *conf_logdir;
 
-    /* now that we have a logfile, let the debug module know how to write
-     * error messages to it.  This is due to some rather obscure linking 
-     * problems. */
-    set_logerror(logerror);
-
     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
     logfile = vstralloc(conf_logdir, "/log", NULL);
     amfree(conf_logdir);
@@ -275,17 +276,44 @@ get_logline(
     FILE *     logf)
 {
     static char *logline = NULL;
+    static size_t line_size = 0;
+    char *lline;
+    size_t loffset = 0;
     char *logstr, *progstr;
     char *s;
     int ch;
+    int n;
 
-    amfree(logline);
-    while ((logline = agets(logf)) != NULL) {
-       if (logline[0] != '\0')
-           break;
-       amfree(logline);
+    if (!logline) {
+       line_size = 256;
+       logline = g_malloc(line_size);
+    }
+
+    logline[0] = '\0';
+    while(1) {
+       lline = fgets(logline + loffset, line_size - loffset, logf);
+       if (lline == NULL) {
+           break; /* EOF */
+       }
+       if (strlen(logline) == line_size -1 &&
+                  logline[strlen(logline)-1] != '\n') {
+           line_size *= 2;
+           logline = g_realloc(logline, line_size);
+           loffset = strlen(logline);
+       } else if (strlen(logline) == 0 ||
+                  (strlen(logline) == 1 && logline[0] == '\n')) {
+       } else {
+           break; /* good line */
+       }
+       logline[loffset] = '\0';
     }
-    if (logline == NULL) return 0;
+    if (logline[0] == '\0')
+       return 0;
+
+    /* remove \n */
+    n = strlen(logline);
+    if (logline[n-1] == '\n') logline[n-1] = '\0';
+
     curlinenum++;
     s = logline;
     ch = *s++;