Bumped version to 3.11.5; Updated ChangeLog.
[collectd.git] / src / utils_debug.c
1 /**
2  * collectd - src/utils_debug.c
3  * Copyright (C) 2005,2006  Niki W. Waibel
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Niki W. Waibel <niki.waibel at gmx.net>
21  **/
22
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "common.h"
28 #include "utils_debug.h"
29
30 #if COLLECT_DEBUG
31
32 static void cu_vdebug(const char *file, int line, const char *func,
33         const char *format, va_list ap);
34
35 /* if preemptive threads are used, these vars need some sort of locking! */
36 /* pth is non-preemptive, so no locking is necessary */
37 static FILE *cu_debug_file = NULL;
38 static char *cu_debug_filename = NULL;
39
40 static void
41 cu_vdebug(const char *file, int line, const char *func,
42         const char *format, va_list ap)
43
44         FILE *f;
45         if(cu_debug_file != NULL) {
46                 f = cu_debug_file;
47         } else {
48                 /* stderr might be redirected to /dev/null. in that case */
49                 /* you'll not see anything... */
50                 f = stderr;
51         }
52
53         fprintf(f, "%s:%d:%s(): ",
54                 file, line, func);
55         vfprintf(f, format, ap);
56         fprintf(f, "\n");
57         fflush(f);
58 } /* static void cu_vdebug(const char *file, int line, const char *func,
59         const char *format, va_list ap) */
60
61 void
62 cu_debug(const char *file, int line, const char *func,
63         const char *format, ...)
64
65         va_list ap;
66
67         va_start(ap, format);
68         cu_vdebug(file, line, func, format, ap);
69         va_end(ap);
70 } /* void cu_debug(const char *file, int line, const char *func,
71         const char *format, ...) */
72
73 int
74 cu_debug_startfile(const char *file, int line, const char *func,
75         const char *filename, const char *format, ...)
76 {
77         va_list ap;
78
79         if(cu_debug_file != NULL) {
80                 DBG("Don't call this function more then once without"
81                         " calling cu_debug_stopfile().");
82                 return EXIT_FAILURE;
83         }
84
85         if(cu_debug_filename == NULL) {
86                 cu_debug_filename = sstrdup(filename);
87         }
88
89         cu_debug_file = fopen(cu_debug_filename, "a");
90         if(cu_debug_file == NULL) {
91                 DBG("Cannot open debug file %s: %s.\n",
92                         cu_debug_filename, strerror(errno));
93                 return EXIT_FAILURE;
94         }
95
96         va_start(ap, format);
97         cu_vdebug(file, line, func, format, ap);
98         va_end(ap);
99
100         return EXIT_SUCCESS;
101 } /* int cu_debug_start(const char *file, int line, const char *func,
102         const char *format, ...) */
103
104 int
105 cu_debug_stopfile(const char *file, int line, const char *func,
106         const char *format, ...)
107 {
108         va_list ap;
109
110         va_start(ap, format);
111         cu_vdebug(file, line, func, format, ap);
112         va_end(ap);
113
114         if(cu_debug_file == NULL) {
115                 DBG("Don't call this function more then once or without"
116                         " calling cu_debug_startfile().");
117                 return EXIT_FAILURE;
118         }
119
120         if(fclose(cu_debug_file) != 0) {
121                 DBG("Cannot close debug file %s: %s.\n",
122                         cu_debug_filename, strerror(errno));
123                 return EXIT_FAILURE;
124         }
125         cu_debug_file = NULL;
126
127         sfree(cu_debug_filename);
128
129         return EXIT_SUCCESS;
130 } /* int cu_debug_stop(const char *file, int line, const char *func,
131         const char *format, ...) */
132
133 int
134 cu_debug_resetfile(const char *file, int line, const char *func,
135         const char *filename)
136 {
137         if(filename == NULL) {
138                 DBG("You have to set filename when calling this function!\n");
139                 return EXIT_FAILURE;
140         }
141         if(cu_debug_file != NULL) {
142                 char *save_filename = NULL;
143
144                 /* DBG_STARTFILE was called already */
145                 /* reopen file */
146
147                 DBG_STOPFILE("Closing %s and reopening %s.",
148                         cu_debug_filename, filename);
149                 save_filename = smalloc(strlen(cu_debug_filename)+1);
150                 sstrncpy(save_filename, cu_debug_filename,
151                         strlen(cu_debug_filename)+1);
152                 cu_debug_filename = smalloc(strlen(filename)+1);
153                 sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
154                 DBG_STARTFILE("Reopening %s after closing %s.",
155                         filename, save_filename);
156                 sfree(save_filename);
157                 return EXIT_SUCCESS;
158         }
159
160         /* DBG_STARTFILE was NOT called already */
161         /* setting filename only */
162
163         if(cu_debug_filename != NULL) {
164                 sfree(cu_debug_filename);
165         }
166         cu_debug_filename = smalloc(strlen(filename)+1);
167         sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
168
169         return EXIT_SUCCESS;
170 } /* int cu_debug_resetfile(const char *file, int line, const char *func,
171         const char *filename) */
172
173 #endif /* COLLECT_DEBUG */
174