move rx.[hc].
[apps/madmutt.git] / lib-lib / debug.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * written for mutt-ng by:
21  * Rocco Rutte <pdmef@cs.tu-berlin.de>
22  *
23  * This file is part of mutt-ng, see http://www.muttng.org/.
24  * It's licensed under the GNU General Public License,
25  * please see the file GPL in the top level source directory.
26  */
27 #if HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #if DEBUG
32
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <time.h>
36
37 #include "str.h"
38 #include "file.h"
39 #include "debug.h"
40
41 #include "mutt.h"
42 #include "globals.h"
43
44 short DebugLevel = -1;
45 FILE* DebugFile = NULL;
46
47 void debug_setlevel(short level) {
48     DebugLevel = level;
49 }
50
51 void debug_start(const char* basedir) {
52     time_t t;
53     int i;
54     char buf[_POSIX_PATH_MAX];
55     char buf2[_POSIX_PATH_MAX];
56
57     if (DebugLevel < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL
58     || !basedir || !*basedir)
59         return;
60     /* rotate the old debug logs */
61     for (i = 3; i >= 0; i--) {
62         snprintf(buf, sizeof (buf), "%s/.madmuttdebug%d", NONULL(basedir), i);
63         snprintf(buf2, sizeof (buf2), "%s/.madmuttdebug%d", NONULL(basedir), i + 1);
64         rename (buf, buf2);
65     }
66
67     if ((DebugFile = safe_fopen (buf, "w")) != NULL) {
68         t = time (NULL);
69         setbuf(DebugFile, NULL);   /* don't buffer the debugging output! */
70         fprintf(DebugFile,
71                 "Madmutt %s started at %s\nDebugging at level %d\n\n",
72                 MUTT_VERSION, asctime (localtime (&t)), DebugLevel);
73     }
74 }
75
76 void _debug_print_intro(const char* file, int line, const char *function, int level) {
77     if (!DebugFile || DebugLevel < DEBUG_MIN_LEVEL || DebugLevel > DEBUG_MAX_LEVEL)
78         return;
79     fprintf(DebugFile, "[%d:%s:%d", level, NONULL(file), line);
80     if (function && *function)
81         fprintf(DebugFile, ":%s()", function);
82     fprintf(DebugFile, "] ");
83 }
84
85 void _debug_print_msg (const char* fmt, ...) {
86     va_list ap;
87
88     if (!DebugFile || DebugLevel < 0)
89         return;
90     va_start(ap, fmt);
91     vfprintf(DebugFile, fmt, ap);
92     va_end(ap);
93 }
94
95 #endif /* DEBUG */