more muttng -> madmutt
[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 <lib-lib/str.h>
20 #include <lib-lib/file.h>
21 #include "debug.h"
22
23 #include "mutt.h"
24 #include "globals.h"
25
26 short DebugLevel = -1;
27 FILE* DebugFile = NULL;
28
29 void debug_setlevel (short level) {
30   DebugLevel = level;
31 }
32
33 void debug_start (const char* basedir) {
34   time_t t;
35   int i;
36   char buf[_POSIX_PATH_MAX];
37   char buf2[_POSIX_PATH_MAX];
38
39   if (DebugLevel < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL
40       || !basedir || !*basedir)
41     return;
42   /* rotate the old debug logs */
43   for (i = 3; i >= 0; i--) {
44     snprintf (buf, sizeof (buf), "%s/.madmuttdebug%d", NONULL (basedir), i);
45     snprintf (buf2, sizeof (buf2), "%s/.madmuttdebug%d", NONULL (basedir), i + 1);
46     rename (buf, buf2);
47   }
48   if ((DebugFile = safe_fopen (buf, "w")) != NULL) {
49     t = time (NULL);
50     setbuf (DebugFile, NULL);   /* don't buffer the debugging output! */
51     fprintf (DebugFile,
52              "Madmutt %s started at %s\nDebugging at level %d\n\n",
53              MUTT_VERSION, asctime (localtime (&t)), DebugLevel);
54   }
55 }
56
57 void _debug_print_intro (const char* file, int line, const char* function, int level) {
58   if (!DebugFile || DebugLevel < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL)
59     return;
60   fprintf (DebugFile, "[%d:%s:%d", level, NONULL(file), line);
61   if (function && *function)
62     fprintf (DebugFile, ":%s()", function);
63   fprintf (DebugFile, "] ");
64 }
65
66 void _debug_print_msg (const char* fmt, ...) {
67   va_list ap;
68
69   if (!DebugFile || DebugLevel < 0)
70     return;
71   va_start (ap, fmt);
72   vfprintf (DebugFile, fmt, ap);
73   va_end (ap);
74 }
75
76 #endif /* DEBUG */