X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=server-src%2Flogfile.c;h=8bab3cfdf91a536aa8d8f897760db22b4bf74f9e;hb=691567b16c13087b31ee4c2b6d038e57872fae82;hp=de063dcfd1782f023f03e8b51f55a98cf169c5a7;hpb=eefb15c5c04acb3c75f0c704ea664feb1bbae75c;p=debian%2Famanda diff --git a/server-src/logfile.c b/server-src/logfile.c index de063dc..8bab3cf 100644 --- a/server-src/logfile.c +++ b/server-src/logfile.c @@ -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 @@ -25,7 +26,7 @@ * University of Maryland at College Park */ /* - * $Id: logfile.c,v 1.29 2005/12/04 22:56:55 martinea Exp $ + * $Id: logfile.c,v 1.31 2006/06/01 14:54:39 martinea Exp $ * * common log file writing routine */ @@ -42,7 +43,8 @@ char *logtype_str[] = { "ERROR", "WARNING", "INFO", "SUMMARY", /* information messages */ "START", "FINISH", /* start/end of a run */ "DISK", /* disk */ - "SUCCESS", "PARTIAL", "FAIL", "STRANGE", /* the end of a dump */ + /* the end of a dump */ + "DONE", "PART", "PARTPARTIAL", "SUCCESS", "PARTIAL", "FAIL", "STRANGE", "CHUNK", "CHUNKSUCCESS", /* ... continued */ "STATS", /* statistics */ "MARKER", /* marker for reporter */ @@ -51,7 +53,8 @@ char *logtype_str[] = { char *program_str[] = { "UNKNOWN", "planner", "driver", "amreport", "dumper", "chunker", - "taper", "amflush" + "taper", "amflush", "amdump", "amidxtaped", "amfetchdump", "amcheckdump", + "amvault", }; int curlinenum; @@ -73,47 +76,39 @@ 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 +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]; + logtype_t logtype = L_ERROR; + switch (log_level) { + case G_LOG_LEVEL_ERROR: + case G_LOG_LEVEL_CRITICAL: + logtype = L_FATAL; + break; - /* 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); + default: + return; } - - arglist_start(argp, format); - vsnprintf(linebuf, sizeof(linebuf)-1, format, argp); - /* -1 to allow for '\n' */ - 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]; - int n; + size_t n; + static gboolean in_log_add = 0; + /* avoid recursion */ + if (in_log_add) + return; /* format error message */ @@ -122,42 +117,64 @@ 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); - vsnprintf(linebuf, sizeof(linebuf)-1, format, argp); + /* 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 */ if(multiline == -1) open_log(); - if (fullwrite(logfd, leader, strlen(leader)) < 0) - error("log file write error: %s", strerror(errno)); + if (full_write(logfd, leader, strlen(leader)) < strlen(leader)) { + error(_("log file write error: %s"), strerror(errno)); + /*NOTREACHED*/ + } amfree(leader); + /* add a newline if necessary */ n = strlen(linebuf); if(n == 0 || linebuf[n-1] != '\n') linebuf[n++] = '\n'; linebuf[n] = '\0'; - if (fullwrite(logfd, linebuf, n) < 0) - error("log file write error: %s", strerror(errno)); + if (full_write(logfd, linebuf, n) < n) { + error(_("log file write error: %s"), strerror(errno)); + /*NOTREACHED*/ + } if(multiline != -1) multiline++; else close_log(); - erroutput_type = saved_errout; + in_log_add = 0; } -void log_start_multiline() +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 +log_start_multiline(void) { assert(multiline == -1); @@ -166,7 +183,8 @@ void log_start_multiline() } -void log_end_multiline() +void +log_end_multiline(void) { assert(multiline != -1); multiline = -1; @@ -174,8 +192,9 @@ void log_end_multiline() } -void log_rename(datestamp) -char *datestamp; +void +log_rename( + char * datestamp) { char *conf_logdir; char *logfile; @@ -186,16 +205,11 @@ char *datestamp; if(datestamp == NULL) datestamp = "error"; - conf_logdir = getconf_str(CNF_LOGDIR); - if (*conf_logdir == '/') { - conf_logdir = stralloc(conf_logdir); - } else { - conf_logdir = stralloc2(config_dir, conf_logdir); - } + conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR)); logfile = vstralloc(conf_logdir, "/log", NULL); for(seq = 0; 1; seq++) { /* if you've got MAXINT files in your dir... */ - snprintf(seq_str, sizeof(seq_str), "%d", seq); + g_snprintf(seq_str, SIZEOF(seq_str), "%u", seq); fname = newvstralloc(fname, logfile, ".", datestamp, @@ -204,9 +218,10 @@ char *datestamp; if(stat(fname, &statbuf) == -1 && errno == ENOENT) break; } - if(rename(logfile, fname) == -1) - error("could not rename \"%s\" to \"%s\": %s", + if(rename(logfile, fname) == -1) { + g_debug(_("could not rename \"%s\" to \"%s\": %s"), logfile, fname, strerror(errno)); + } amfree(fname); amfree(logfile); @@ -214,52 +229,91 @@ char *datestamp; } -static void open_log() +static void +open_log(void) { char *conf_logdir; - conf_logdir = getconf_str(CNF_LOGDIR); - if (*conf_logdir == '/') { - conf_logdir = stralloc(conf_logdir); - } else { - conf_logdir = stralloc2(config_dir, conf_logdir); - } + conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR)); logfile = vstralloc(conf_logdir, "/log", NULL); amfree(conf_logdir); logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600); - if(logfd == -1) - error("could not open log file %s: %s", logfile, strerror(errno)); + if(logfd == -1) { + error(_("could not open log file %s: %s"), logfile, strerror(errno)); + /*NOTREACHED*/ + } - if(amflock(logfd, "log") == -1) - error("could not lock log file %s: %s", logfile, strerror(errno)); + 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) - error("could not unlock log file %s: %s", logfile, strerror(errno)); + if(amfunlock(logfd, "log") == -1) { + error(_("could not unlock log file %s: %s"), logfile, strerror(errno)); + /*NOTREACHED*/ + } - if(close(logfd) == -1) - error("close log file: %s", strerror(errno)); + if(close(logfd) == -1) { + error(_("close log file: %s"), strerror(errno)); + /*NOTREACHED*/ + } logfd = -1; amfree(logfile); } - -int get_logline(logf) -FILE *logf; +/* WARNING: Function accesses globals curstr, curlog, and curprog + * WARNING: Function has static member logline, returned via globals */ +int +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; + + 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[0] == '\0') + return 0; + + /* remove \n */ + n = strlen(logline); + if (logline[n-1] == '\n') logline[n-1] = '\0'; - amfree(logline); - if((logline = agets(logf)) == NULL) return 0; curlinenum++; s = logline; ch = *s++;