Bläddra i källkod

libdpkg: Move log_message to lib/log.c

Guillem Jover 18 år sedan
förälder
incheckning
3349664590
6 ändrade filer med 88 tillägg och 42 borttagningar
  1. 8 0
      ChangeLog
  2. 1 0
      lib/Makefile.am
  3. 0 39
      lib/dbmodify.c
  4. 0 3
      lib/dpkg-db.h
  5. 5 0
      lib/dpkg.h
  6. 74 0
      lib/log.c

+ 8 - 0
ChangeLog

@@ -1,3 +1,11 @@
+2008-03-25  Guillem Jover  <guillem@debian.org>
+
+	* lib/dbmodify.c (log_file, log_message): Move to ...
+	* lib/log.c: ... here. New file.
+	* lib/Makefile.am (libdpkg_a_SOURCES): Add 'log.c'.
+	* lib/dpkg-db.h (log_file, log_message): Move declaration to ...
+	* lib/dpkg.h: ... here.
+
 2008-03-25  Ian Jackson  <ian@davenant.greenend.org.uk>,
             Guillem Jover  <guillem@debian.org>
 

+ 1 - 0
lib/Makefile.am

@@ -25,6 +25,7 @@ libdpkg_a_SOURCES = \
 	fields.c \
 	gettext.h \
 	lock.c \
+	log.c \
 	md5.c md5.h \
 	mlib.c \
 	myopt.c myopt.h \

+ 0 - 39
lib/dbmodify.c

@@ -300,42 +300,3 @@ const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile) {
   return vb.buf;
 }
 
-const char *log_file= NULL;
-
-void log_message(const char *fmt, ...) {
-  static struct varbuf *log= NULL;
-  static FILE *logfd= NULL;
-  char time_str[20];
-  time_t now;
-  va_list al;
-
-  if (!log_file)
-    return;
-
-  if (!logfd) {
-    logfd= fopen(log_file, "a");
-    if (!logfd) {
-      fprintf(stderr, _("couldn't open log `%s': %s\n"), log_file,
-	      strerror(errno));
-      log_file= NULL;
-      return;
-    }
-    setlinebuf(logfd);
-    setcloexec(fileno(logfd), log_file);
-  }
-
-  if (!log) {
-    log= nfmalloc(sizeof(struct varbuf));
-    varbufinit(log);
-  } else
-    varbufreset(log);
-
-  va_start(al,fmt);
-  varbufvprintf(log, fmt, al);
-  varbufaddc(log, 0);
-  va_end(al);
-
-  time(&now);
-  strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&now));
-  fprintf(logfd, "%s %s\n", time_str, log->buf);
-}

+ 0 - 3
lib/dpkg-db.h

@@ -181,9 +181,6 @@ extern char *statusfile, *availablefile; /* initialised by modstatdb_init */
 
 const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile);
 
-extern const char *log_file;
-void log_message(const char *fmt, ...) PRINTFFORMAT(1, 2);
-
 /*** from database.c ***/
 
 struct pkginfo *findpackage(const char *name);

+ 5 - 0
lib/dpkg.h

@@ -207,6 +207,11 @@ void badusage(const char *fmt, ...) NONRETURNING PRINTFFORMAT(1, 2);
 void werr(const char *what) NONRETURNING;
 void warningf(const char *fmt, ...) PRINTFFORMAT(1, 2);
 
+/*** log.c ***/
+
+extern const char *log_file;
+void log_message(const char *fmt, ...) PRINTFFORMAT(1, 2);
+
 /*** cleanup.c ***/
 
 void cu_closefile(int argc, void **argv);

+ 74 - 0
lib/log.c

@@ -0,0 +1,74 @@
+/*
+ * dpkg - main program for package management
+ * log.c - logging related functions
+ *
+ * Copyright (C) 2005 Scott James Remnant <scott@netsplit.com>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <time.h>
+#include <errno.h>
+
+#include <dpkg.h>
+#include <dpkg-db.h>
+
+const char *log_file = NULL;
+
+void
+log_message(const char *fmt, ...)
+{
+	static struct varbuf *log = NULL;
+	static FILE *logfd = NULL;
+	char time_str[20];
+	time_t now;
+	va_list al;
+
+	if (!log_file)
+		return;
+
+	if (!logfd) {
+		logfd = fopen(log_file, "a");
+		if (!logfd) {
+			fprintf(stderr, _("couldn't open log `%s': %s\n"),
+			        log_file, strerror(errno));
+			log_file = NULL;
+			return;
+		}
+		setlinebuf(logfd);
+		setcloexec(fileno(logfd), log_file);
+	}
+
+	if (!log) {
+		log = nfmalloc(sizeof(struct varbuf));
+		varbufinit(log);
+	} else
+		varbufreset(log);
+
+	va_start(al, fmt);
+	varbufvprintf(log, fmt, al);
+	varbufaddc(log, 0);
+	va_end(al);
+
+	time(&now);
+	strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S",
+	         localtime(&now));
+	fprintf(logfd, "%s %s\n", time_str, log->buf);
+}
+