+/*
+ * This program 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 program 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Copyright © 2006 Pierre Habouzit
+ */
+/*
+ * written for mutt-ng by:
+ * Rocco Rutte <pdmef@cs.tu-berlin.de>
+ *
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
+ */
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if DEBUG
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <time.h>
+
+#include "str.h"
+#include "file.h"
+#include "debug.h"
+
+#include "mutt.h"
+#include "globals.h"
+
+short DebugLevel = -1;
+FILE* DebugFile = NULL;
+
+void debug_setlevel(short level) {
+ DebugLevel = level;
+}
+
+void debug_start(const char* basedir) {
+ time_t t;
+ int i;
+ char buf[_POSIX_PATH_MAX];
+ char buf2[_POSIX_PATH_MAX];
+
+ if (DebugLevel < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL
+ || !basedir || !*basedir)
+ return;
+ /* rotate the old debug logs */
+ for (i = 3; i >= 0; i--) {
+ snprintf(buf, sizeof (buf), "%s/.madmuttdebug%d", NONULL(basedir), i);
+ snprintf(buf2, sizeof (buf2), "%s/.madmuttdebug%d", NONULL(basedir), i + 1);
+ rename (buf, buf2);
+ }
+
+ if ((DebugFile = safe_fopen (buf, "w")) != NULL) {
+ t = time (NULL);
+ setbuf(DebugFile, NULL); /* don't buffer the debugging output! */
+ fprintf(DebugFile,
+ "Madmutt %s started at %s\nDebugging at level %d\n\n",
+ MUTT_VERSION, asctime (localtime (&t)), DebugLevel);
+ }
+}
+
+void _debug_print_intro(const char* file, int line, const char *function, int level) {
+ if (!DebugFile || DebugLevel < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL)
+ return;
+ fprintf(DebugFile, "[%d:%s:%d", level, NONULL(file), line);
+ if (function && *function)
+ fprintf(DebugFile, ":%s()", function);
+ fprintf(DebugFile, "] ");
+}
+
+void _debug_print_msg (const char* fmt, ...) {
+ va_list ap;
+
+ if (!DebugFile || DebugLevel < 0)
+ return;
+ va_start(ap, fmt);
+ vfprintf(DebugFile, fmt, ap);
+ va_end(ap);
+}
+
+#endif /* DEBUG */