Explorar el Código

Move and generalize debug API from dpkg to libdpkg

Guillem Jover hace 15 años
padre
commit
5e7235bf69
Se han modificado 8 ficheros con 152 adiciones y 33 borrados
  1. 2 0
      lib/dpkg/Makefile.am
  2. 84 0
      lib/dpkg/debug.c
  3. 54 0
      lib/dpkg/debug.h
  4. 5 0
      lib/dpkg/libdpkg.Versions
  5. 1 1
      src/depcon.c
  6. 1 12
      src/help.c
  7. 4 2
      src/main.c
  8. 1 18
      src/main.h

+ 2 - 0
lib/dpkg/Makefile.am

@@ -30,6 +30,7 @@ libdpkg_a_SOURCES = \
 	compress.c \
 	database.c \
 	dbmodify.c \
+	debug.c \
 	dir.c \
 	dump.c \
 	ehandle.c \
@@ -69,6 +70,7 @@ pkginclude_HEADERS = \
 	buffer.h \
 	command.h \
 	compress.h \
+	debug.h \
 	dir.h \
 	dpkg.h \
 	dpkg-db.h \

+ 84 - 0
lib/dpkg/debug.c

@@ -0,0 +1,84 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * debug.c - debugging support
+ *
+ * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
+ * Copyright © 2011 Guillem Jover <guillem@debian.orgian>
+ *
+ * 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 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include <dpkg/debug.h>
+
+static int debug_mask = 0;
+static FILE *debug_output = NULL;
+
+/**
+ * Set the debugging output file.
+ */
+void
+debug_set_output(FILE *output)
+{
+	debug_output = output;
+}
+
+/**
+ * Set the debugging mask.
+ *
+ * The mask determines what debugging flags are going to take effect at
+ * run-time. The output will be set to stderr if it has not been set before.
+ */
+void
+debug_set_mask(int mask)
+{
+	debug_mask = mask;
+	if (!debug_output)
+		debug_output = stderr;
+}
+
+/**
+ * Check if a debugging flag is currently set on the debugging mask.
+ */
+bool
+debug_has_flag(int flag)
+{
+	return debug_mask & flag;
+}
+
+/**
+ * Output a debugging message.
+ *
+ * The message will be printed to the previously specified output if the
+ * specified flag is present in the current debugging mask.
+ */
+void
+debug(int flag, const char *fmt, ...)
+{
+	va_list args;
+
+	if (!debug_has_flag(flag))
+		return;
+
+	fprintf(debug_output, "D0%05o: ", flag);
+	va_start(args, fmt);
+	vfprintf(debug_output, fmt, args);
+	va_end(args);
+	putc('\n', debug_output);
+}

+ 54 - 0
lib/dpkg/debug.h

@@ -0,0 +1,54 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * debug.h - debugging support
+ *
+ * Copyright © 2011 Guillem Jover <guillem@debian.orgian>
+ *
+ * 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 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIBDPKG_DEBUG_H
+#define LIBDPKG_DEBUG_H
+
+#include <dpkg/macros.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+
+DPKG_BEGIN_DECLS
+
+enum debugflags {
+	dbg_general = 01,
+	dbg_scripts = 02,
+	dbg_eachfile = 010,
+	dbg_eachfiledetail = 0100,
+	dbg_conff = 020,
+	dbg_conffdetail = 0200,
+	dbg_depcon = 040,
+	dbg_depcondetail = 0400,
+	dbg_veryverbose = 01000,
+	dbg_stupidlyverbose = 02000,
+	dbg_triggers = 010000,
+	dbg_triggersdetail = 020000,
+	dbg_triggersstupid = 040000,
+};
+
+void debug_set_output(FILE *output);
+void debug_set_mask(int mask);
+bool debug_has_flag(int flag);
+void debug(int flag, const char *fmt, ...) DPKG_ATTR_PRINTF(2);
+
+DPKG_END_DECLS
+
+#endif /* LIBDPKG_DEBUG_H */

+ 5 - 0
lib/dpkg/libdpkg.Versions

@@ -28,6 +28,11 @@ LIBDPKG_PRIVATE {
 	warningv;
 	warning;
 
+	debug_set_output;
+	debug_set_mask;
+	debug_has_flag;
+	debug;
+
 	# Generic cleanup
 	cu_closepipe;
 	cu_closefile;

+ 1 - 1
src/depcon.c

@@ -110,7 +110,7 @@ findbreakcyclerecursive(struct pkginfo *pkg, struct cyclesofarlink *sofar)
     return false;
   pkg->clientdata->color = gray;
 
-  if (f_debug & dbg_depcondetail) {
+  if (debug_has_flag(dbg_depcondetail)) {
     struct varbuf str_pkgs = VARBUF_INIT;
 
     for (sol = sofar; sol; sol = sol->prev) {

+ 1 - 12
src/help.c

@@ -190,7 +190,7 @@ preexecscript(struct command *cmd)
     if (chdir("/"))
       ohshite(_("failed to chdir to `%.255s'"), "/");
   }
-  if (f_debug & dbg_scripts) {
+  if (debug_has_flag(dbg_scripts)) {
     struct varbuf args = VARBUF_INIT;
     const char **argv = cmd->argv;
 
@@ -458,17 +458,6 @@ void clear_istobes(void) {
   pkg_db_iter_free(it);
 }
 
-void debug(int which, const char *fmt, ...) {
-  va_list args;
-
-  if (!(f_debug & which)) return;
-  fprintf(stderr,"D0%05o: ",which);
-  va_start(args, fmt);
-  vfprintf(stderr, fmt, args);
-  va_end(args);
-  putc('\n',stderr);
-}
-
 /*
  * Returns true if the directory contains conffiles belonging to pkg,
  * false otherwise.

+ 4 - 2
src/main.c

@@ -179,7 +179,6 @@ const char printforhelp[]= N_(
 int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
 int f_autodeconf=0, f_nodebsig=0;
 int f_triggers = 0;
-unsigned long f_debug=0;
 int fc_downgrade=1, fc_configureany=0, fc_hold=0, fc_removereinstreq=0, fc_overwrite=0;
 int fc_removeessential=0, fc_conflicts=0, fc_depends=0, fc_dependsversion=0;
 int fc_breaks=0, fc_badpath=0, fc_overwritediverted=0, fc_architecture=0;
@@ -225,6 +224,7 @@ static const struct forceinfo {
 
 static void setdebug(const struct cmdinfo *cpi, const char *value) {
   char *endp;
+  unsigned long mask;
 
   if (*value == 'h') {
     printf(_(
@@ -251,8 +251,10 @@ static void setdebug(const struct cmdinfo *cpi, const char *value) {
     exit(0);
   }
 
-  f_debug= strtoul(value,&endp,8);
+  mask = strtoul(value, &endp, 8);
   if (value == endp || *endp) badusage(_("--debug requires an octal argument"));
+
+  debug_set_mask(mask);
 }
 
 static void

+ 1 - 18
src/main.h

@@ -21,6 +21,7 @@
 #ifndef MAIN_H
 #define MAIN_H
 
+#include <dpkg/debug.h>
 #include <dpkg/pkg-list.h>
 
 /* These two are defined in filesdb.h. */
@@ -124,7 +125,6 @@ extern const char *const statusstrings[];
 extern int f_pending, f_recursive, f_alsoselect, f_skipsame, f_noact;
 extern int f_autodeconf, f_nodebsig;
 extern int f_triggers;
-extern unsigned long f_debug;
 extern int fc_downgrade, fc_configureany, fc_hold, fc_removereinstreq, fc_overwrite;
 extern int fc_removeessential, fc_conflicts, fc_depends, fc_dependsversion;
 extern int fc_breaks, fc_badpath, fc_overwritediverted, fc_architecture;
@@ -253,23 +253,6 @@ void clear_istobes(void);
 bool isdirectoryinuse(struct filenamenode *namenode, struct pkginfo *pkg);
 bool hasdirectoryconffiles(struct filenamenode *namenode, struct pkginfo *pkg);
 
-enum debugflags {
-  dbg_general=           00001,
-  dbg_scripts=           00002,
-  dbg_eachfile=          00010,
-  dbg_eachfiledetail=    00100,
-  dbg_conff=             00020,
-  dbg_conffdetail=       00200,
-  dbg_depcon=            00040,
-  dbg_depcondetail=      00400,
-  dbg_veryverbose=       01000,
-  dbg_stupidlyverbose=   02000,
-  dbg_triggers =        010000,
-  dbg_triggersdetail =  020000,
-  dbg_triggersstupid =  040000,
-};
-
-void debug(int which, const char *fmt, ...) DPKG_ATTR_PRINTF(2);
 void log_action(const char *action, struct pkginfo *pkg);
 
 /* from trigproc.c */