use m_strdup and m_strlen that are inlined for efficiency
[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 "debug.h"
21
22 #include "mutt.h"
23 #include "globals.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 < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL
39       || !basedir || !*basedir)
40     return;
41   /* rotate the old debug logs */
42   for (i = 3; i >= 0; i--) {
43     snprintf (buf, sizeof (buf), "%s/.muttngdebug%d", NONULL (basedir), i);
44     snprintf (buf2, sizeof (buf2), "%s/.muttngdebug%d", NONULL (basedir), i + 1);
45     rename (buf, buf2);
46   }
47   if ((DebugFile = safe_fopen (buf, "w")) != NULL) {
48     t = time (NULL);
49     setbuf (DebugFile, NULL);   /* don't buffer the debugging output! */
50     fprintf (DebugFile,
51              "Mutt-ng %s started at %s\nDebugging at level %d\n\n",
52              MUTT_VERSION, asctime (localtime (&t)), DebugLevel);
53   }
54 }
55
56 void _debug_print_intro (const char* file, int line, const char* function, int level) {
57   if (!DebugFile || DebugLevel < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL)
58     return;
59   fprintf (DebugFile, "[%d:%s:%d", level, NONULL(file), line);
60   if (function && *function)
61     fprintf (DebugFile, ":%s()", function);
62   fprintf (DebugFile, "] ");
63 }
64
65 void _debug_print_msg (const char* fmt, ...) {
66   va_list ap;
67
68   if (!DebugFile || DebugLevel < 0)
69     return;
70   va_start (ap, fmt);
71   vfprintf (DebugFile, fmt, ap);
72   va_end (ap);
73 }
74
75 #endif /* DEBUG */