| 1 | |
| 2 | /* Debug macros for developemnt. |
| 3 | Copyright 1997 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 21 | |
| 22 | #define DEBUGIFY |
| 23 | #include "debugify.h" |
| 24 | #include "config.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #ifdef HAVE_STDLIB_H |
| 28 | #include <stdlib.h> |
| 29 | #endif |
| 30 | #ifdef HAVE_STRING_H |
| 31 | #include <string.h> |
| 32 | #else |
| 33 | #include <strings.h> |
| 34 | #endif |
| 35 | |
| 36 | #define REDIRECT |
| 37 | |
| 38 | static FILE *fout = NULL; |
| 39 | static char fname[128]; |
| 40 | static int file_cnt = 0; /* count number of open files */ |
| 41 | |
| 42 | void |
| 43 | puts_dbg (data) |
| 44 | const char *data; |
| 45 | { |
| 46 | FILE *fdbg; |
| 47 | |
| 48 | fdbg = fopen ("dbg.log", "a+"); |
| 49 | if (fdbg == NULL) |
| 50 | return; |
| 51 | fprintf (fdbg, data); |
| 52 | fclose (fdbg); |
| 53 | } |
| 54 | |
| 55 | /* Can't easily get the message back to gdb... write to a log instead. */ |
| 56 | void |
| 57 | fputs_dbg (data, fakestream) |
| 58 | const char *data; |
| 59 | FILE *fakestream; |
| 60 | { |
| 61 | #ifdef REDIRECT |
| 62 | puts_dbg (data); |
| 63 | #else /* REDIRECT */ |
| 64 | |
| 65 | if (fakestream == stdout || fakestream == stderr || fakestream == NULL) |
| 66 | { |
| 67 | if (fout == NULL) |
| 68 | { |
| 69 | for (file_cnt = 0; file_cnt < 20; file_cnt++) |
| 70 | { |
| 71 | sprintf (fname, "dbg%d.log", file_cnt); |
| 72 | if ((fout = fopen (fname), "r") != NULL) |
| 73 | fclose (fout); |
| 74 | else |
| 75 | break; |
| 76 | } |
| 77 | fout = fopen (fname, "w"); |
| 78 | if (fout == NULL) |
| 79 | return; |
| 80 | } |
| 81 | fakestream = fout; |
| 82 | } |
| 83 | fprintf (fakestream, data); |
| 84 | fflush (fakestream); |
| 85 | #endif /* REDIRECT */ |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | #ifdef ANSI_PROTOTYPES |
| 90 | printf_dbg (const char *format,...) |
| 91 | #else |
| 92 | printf_dbg (va_alist) |
| 93 | va_dcl |
| 94 | #endif |
| 95 | { |
| 96 | va_list args; |
| 97 | char buf[256]; |
| 98 | #ifdef ANSI_PROTOTYPES |
| 99 | va_start (args, format); |
| 100 | #else |
| 101 | char *format; |
| 102 | |
| 103 | va_start (args); |
| 104 | format = va_arg (args, char *); |
| 105 | #endif |
| 106 | vsprintf (buf, format, args); |
| 107 | puts_dbg (buf); |
| 108 | va_end (args); |
| 109 | } |