ddf6284c4ee7163cf7f55fe35266b1c7021fb423
[apps/madmutt.git] / lib / debug.c
1 /*
2  * written for mutt-ng by:
3  * Rocco Rutte <pdmef@cs.tu-berlin.de>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9 #if HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #if DEBUG
14
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <time.h>
18
19 #include "debug.h"
20
21 #include "mutt.h"
22 #include "globals.h"
23 #include "str.h"
24
25 short DebugLevel = -1;
26 FILE* DebugFile = NULL;
27
28 void debug_setlevel (short level) {
29   DebugLevel = level;
30 }
31
32 void debug_start (const char* basedir) {
33   time_t t;
34   int i;
35   char buf[_POSIX_PATH_MAX];
36   char buf2[_POSIX_PATH_MAX];
37
38   if (DebugLevel < 0 || !basedir || !*basedir)
39     return;
40   /* rotate the old debug logs */
41   for (i = 3; i >= 0; i--) {
42     snprintf (buf, sizeof (buf), "%s/.muttngdebug%d", NONULL (basedir), i);
43     snprintf (buf2, sizeof (buf2), "%s/.muttngdebug%d", NONULL (basedir), i + 1);
44     rename (buf, buf2);
45   }
46   if ((DebugFile = safe_fopen (buf, "w")) != NULL) {
47     t = time (NULL);
48     setbuf (DebugFile, NULL);   /* don't buffer the debugging output! */
49     fprintf (DebugFile,
50              "Mutt-ng %s started at %s\nDebugging at level %d\n\n",
51              MUTT_VERSION, asctime (localtime (&t)), DebugLevel);
52   }
53 }
54
55 void _debug_print_intro (const char* file, int line, const char* function, int level) {
56   if (!DebugFile || DebugLevel < 0)
57     return;
58   fprintf (DebugFile, "[%d:%s:%d", level, NONULL(file), line);
59   if (function && *function)
60     fprintf (DebugFile, ":%s()", function);
61   fprintf (DebugFile, "] ");
62 }
63
64 void _debug_print_msg (const char* fmt, ...) {
65   va_list ap;
66
67   if (!DebugFile || DebugLevel < 0)
68     return;
69   va_start (ap, fmt);
70   vfprintf (DebugFile, fmt, ap);
71   va_end (ap);
72 }
73
74 #endif /* DEBUG */