Added copyright/GPL header to all .c and .h files in trunk
[collectd.git] / src / utils_debug.c
1 /**
2  * collectd - src/utils_debug.c
3  * Copyright (C) 2005  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 #include "common.h"
24 #include "utils_debug.h"
25
26 /* *** *** ***   global variables   *** *** *** */
27 #if COLLECT_DEBUG
28
29 #define DEFAULT_FILENAME "collectd.log"
30
31 static void cu_vdebug(const char *file, int line, const char *func,
32         const char *format, va_list ap);
33
34 /* if preemptive threads are used, these vars need some sort of locking! */
35 /* pth is non-preemptive, so no locking is necessary (?) */
36 static FILE *cu_debug_file = NULL;
37 static char *cu_debug_filename = NULL;
38
39 static void
40 cu_vdebug(const char *file, int line, const char *func,
41         const char *format, va_list ap)
42
43         FILE *f;
44         if(cu_debug_file != NULL) {
45                 f = cu_debug_file;
46         } else {
47                 /* stderr might be redirected to /dev/null. in that case */
48                 /* you'll not see anything... */
49                 f = stderr;
50         }
51
52         fprintf(f, "%s:%d:%s(): ",
53                 file, line, func);
54         vfprintf(f, format, ap);
55         fprintf(f, "\n");
56         fflush(f);
57 } /* static void cu_vdebug(const char *file, int line, const char *func,
58         const char *format, va_list ap) */
59
60 void
61 cu_debug(const char *file, int line, const char *func,
62         const char *format, ...)
63
64         va_list ap;
65
66         va_start(ap, format);
67         cu_vdebug(file, line, func, format, ap);
68         va_end(ap);
69 } /* void cu_debug(const char *file, int line, const char *func,
70         const char *format, ...) */
71
72 int
73 cu_debug_startfile(const char *file, int line, const char *func,
74         const char *format, ...)
75 {
76         va_list ap;
77
78         if(cu_debug_file != NULL) {
79                 DBG("Don't call this function more then once without"
80                         " calling cu_debug_stopfile().");
81                 return EXIT_FAILURE;
82         }
83
84         if(cu_debug_filename == NULL) {
85                 cu_debug_filename = sstrdup(DEFAULT_FILENAME);
86         }
87
88         cu_debug_file = fopen(cu_debug_filename, "a");
89         if(cu_debug_file == NULL) {
90                 DBG("Cannot open debug file %s: %s.\n",
91                         cu_debug_filename, strerror(errno));
92                 return EXIT_FAILURE;
93         }
94
95         va_start(ap, format);
96         cu_vdebug(file, line, func, format, ap);
97         va_end(ap);
98
99         return EXIT_SUCCESS;
100 } /* int cu_debug_start(const char *file, int line, const char *func,
101         const char *format, ...) */
102
103 int
104 cu_debug_stopfile(const char *file, int line, const char *func,
105         const char *format, ...)
106 {
107         va_list ap;
108
109         va_start(ap, format);
110         cu_vdebug(file, line, func, format, ap);
111         va_end(ap);
112
113         if(cu_debug_file == NULL) {
114                 DBG("Don't call this function more then once or without"
115                         " calling cu_debug_startfile().");
116                 return EXIT_FAILURE;
117         }
118
119         if(fclose(cu_debug_file) != 0) {
120                 DBG("Cannot close debug file %s: %s.\n",
121                         cu_debug_filename, strerror(errno));
122                 return EXIT_FAILURE;
123         }
124         cu_debug_file = NULL;
125
126         sfree(cu_debug_filename);
127
128         return EXIT_SUCCESS;
129 } /* int cu_debug_stop(const char *file, int line, const char *func,
130         const char *format, ...) */
131
132 int
133 cu_debug_resetfile(const char *file, int line, const char *func,
134         const char *filename)
135 {
136         if(filename == NULL) {
137                 DBG("You have to set filename when calling this function!\n");
138                 return EXIT_FAILURE;
139         }
140         if(cu_debug_file != NULL) {
141                 char *save_filename = NULL;
142
143                 /* DBG_STARTFILE was called already */
144                 /* reopen file */
145
146                 DBG_STOPFILE("Closing %s and reopening %s.",
147                         cu_debug_filename, filename);
148                 save_filename = smalloc(strlen(cu_debug_filename)+1);
149                 sstrncpy(save_filename, cu_debug_filename,
150                         strlen(cu_debug_filename)+1);
151                 cu_debug_filename = smalloc(strlen(filename)+1);
152                 sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
153                 DBG_STARTFILE("Reopening %s after closing %s.",
154                         filename, save_filename);
155                 sfree(save_filename);
156                 return EXIT_SUCCESS;
157         }
158
159         /* DBG_STARTFILE was NOT called already */
160         /* setting filename only */
161
162         if(cu_debug_filename != NULL) {
163                 sfree(cu_debug_filename);
164         }
165         cu_debug_filename = smalloc(strlen(filename)+1);
166         sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
167
168         return EXIT_SUCCESS;
169 } /* int cu_debug_resetfile(const char *file, int line, const char *func,
170         const char *filename) */
171
172 #else /* !COLLECT_DEBUG */
173
174 void
175 cu_debug(const char *file, int line, const char *func, const char *format, ...)
176 {
177 }
178 int
179 cu_debug_startfile(const char *file, int line, const char *func, const char *format, ...)
180 {
181         return EXIT_SUCCESS;
182 }
183 int
184 cu_debug_stopfile(const char *file, int line, const char *func, const char *format, ...)
185 {
186         return EXIT_SUCCESS;
187 }
188 int
189 cu_debug_resetfile(const char *file, int line, const char *func, const char *filename)
190 {
191         return EXIT_SUCCESS;
192 }
193
194 #endif /* COLLECT_DEBUG */
195