This commit adds a dedicated function to initialize the debugging infrastructure. Basically, it moves start_debug() from init.c to lib.c and renames it to mutt_log_init().
The point is to group all functions related to logging in the same place, and eventually move them to a dedicated file (as lib.c is alreay in charge of too many things). As debugfile and debuglevel will later be defined as static in lib.c, this commit also removes the MUTT_LIB_WHERE macro, which is only good at confusing tools and developers. --- init.c | 25 +------------------------ lib.c | 33 +++++++++++++++++++++++++++++++-- lib.h | 13 +++---------- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/init.c b/init.c index 0586638..3ce6888 100644 --- a/init.c +++ b/init.c @@ -2834,29 +2834,6 @@ int mutt_getvaluebyname (const char *name, const struct mapping_t *map) return (-1); } -#ifdef DEBUG -static void start_debug (void) -{ - int i; - char buf[_POSIX_PATH_MAX]; - char buf2[_POSIX_PATH_MAX]; - - /* rotate the old debug logs */ - for (i=3; i>=0; i--) - { - snprintf (buf, sizeof(buf), "%s/.muttdebug%d", NONULL(Homedir), i); - snprintf (buf2, sizeof(buf2), "%s/.muttdebug%d", NONULL(Homedir), i+1); - rename (buf, buf2); - } - if ((debugfile = safe_fopen(buf, "w")) != NULL) - { - setbuf (debugfile, NULL); /* don't buffer the debugging output! */ - dprint(1,(debugfile,"Mutt/%s (%s) debugging at level %d\n", - MUTT_VERSION, ReleaseDate, debuglevel)); - } -} -#endif - static int mutt_execute_commands (LIST *p) { BUFFER err, token; @@ -2965,7 +2942,7 @@ void mutt_init (int skip_sys_rc, LIST *commands) #ifdef DEBUG /* Start up debugging mode if requested */ if (debuglevel > 0) - start_debug (); + mutt_log_init (ReleaseDate, Homedir); #endif /* And about the host... */ diff --git a/lib.c b/lib.c index 224232b..4b80639 100644 --- a/lib.c +++ b/lib.c @@ -26,8 +26,6 @@ * some of our "standard" functions in external programs, too. */ -#define _LIB_C 1 - #if HAVE_CONFIG_H # include "config.h" #endif @@ -52,6 +50,10 @@ #include "lib.h" +#ifdef DEBUG +FILE *debugfile = NULL; +int debuglevel = 0; +#endif static const struct sysexits { @@ -1010,6 +1012,33 @@ mutt_strsysexit(int e) return sysexits_h[i].str; } +#ifdef DEBUG +int mutt_log_init (const char *reldate, const char *homedir) +{ + char buf[_POSIX_PATH_MAX]; + char buf2[_POSIX_PATH_MAX]; + int i; + + /* rotate the old debug logs */ + for (i = 3; i >= 0; i--) + { + snprintf (buf, sizeof(buf), "%s/.muttdebug%d", NONULL(homedir), i); + snprintf (buf2, sizeof(buf2), "%s/.muttdebug%d", NONULL(homedir), i + 1); + rename (buf, buf2); + } + + debugfile = safe_fopen (buf, "w"); + if (!debugfile) + return -1; + + setbuf (debugfile, NULL); /* don't buffer the debugging output! */ + dprint(1,(debugfile,"Mutt/%s (%s) debugging at level %d\n", + MUTT_VERSION, reldate, debuglevel)); + + return 0; +} +#endif + void mutt_debug (FILE *fp, const char *fmt, ...) { va_list ap; diff --git a/lib.h b/lib.h index 84d33b5..2ad091c 100644 --- a/lib.h +++ b/lib.h @@ -128,22 +128,15 @@ static inline int is_email_wsp(char c) extern void (*mutt_error) (const char *, ...); # endif -# ifdef _LIB_C -# define MUTT_LIB_WHERE -# define MUTT_LIB_INITVAL(x) = x -# else -# define MUTT_LIB_WHERE extern -# define MUTT_LIB_INITVAL(x) -# endif - void mutt_exit (int); # ifdef DEBUG -MUTT_LIB_WHERE FILE *debugfile MUTT_LIB_INITVAL(0); -MUTT_LIB_WHERE int debuglevel MUTT_LIB_INITVAL(0); +extern FILE *debugfile; +extern int debuglevel; +int mutt_log_init (const char *reldate, const char *homedir); void mutt_debug (FILE *, const char *, ...); # define dprint(N,X) do { if(debuglevel>=N && debugfile) mutt_debug X; } while (0) -- 2.9.0