2 * collectd - src/utils_debug.c
3 * Copyright (C) 2005,2006 Niki W. Waibel
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.
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.
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
20 * Niki W. Waibel <niki.waibel at gmx.net>
28 #include "utils_debug.h"
32 static void cu_vdebug(const char *file, int line, const char *func,
33 const char *format, va_list ap);
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;
41 cu_vdebug(const char *file, int line, const char *func,
42 const char *format, va_list ap)
45 if(cu_debug_file != NULL) {
48 /* stderr might be redirected to /dev/null. in that case */
49 /* you'll not see anything... */
53 fprintf(f, "%s:%d:%s(): ",
55 vfprintf(f, format, ap);
58 } /* static void cu_vdebug(const char *file, int line, const char *func,
59 const char *format, va_list ap) */
62 cu_debug(const char *file, int line, const char *func,
63 const char *format, ...)
68 cu_vdebug(file, line, func, format, ap);
70 } /* void cu_debug(const char *file, int line, const char *func,
71 const char *format, ...) */
74 cu_debug_startfile(const char *file, int line, const char *func,
75 const char *filename, const char *format, ...)
79 if(cu_debug_file != NULL) {
80 DBG("Don't call this function more then once without"
81 " calling cu_debug_stopfile().");
85 if(cu_debug_filename == NULL) {
86 cu_debug_filename = sstrdup(filename);
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));
97 cu_vdebug(file, line, func, format, ap);
101 } /* int cu_debug_start(const char *file, int line, const char *func,
102 const char *format, ...) */
105 cu_debug_stopfile(const char *file, int line, const char *func,
106 const char *format, ...)
110 va_start(ap, format);
111 cu_vdebug(file, line, func, format, ap);
114 if(cu_debug_file == NULL) {
115 DBG("Don't call this function more then once or without"
116 " calling cu_debug_startfile().");
120 if(fclose(cu_debug_file) != 0) {
121 DBG("Cannot close debug file %s: %s.\n",
122 cu_debug_filename, strerror(errno));
125 cu_debug_file = NULL;
127 sfree(cu_debug_filename);
130 } /* int cu_debug_stop(const char *file, int line, const char *func,
131 const char *format, ...) */
134 cu_debug_resetfile(const char *file, int line, const char *func,
135 const char *filename)
137 if(filename == NULL) {
138 DBG("You have to set filename when calling this function!\n");
141 if(cu_debug_file != NULL) {
142 char *save_filename = NULL;
144 /* DBG_STARTFILE was called already */
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);
160 /* DBG_STARTFILE was NOT called already */
161 /* setting filename only */
163 if(cu_debug_filename != NULL) {
164 sfree(cu_debug_filename);
166 cu_debug_filename = smalloc(strlen(filename)+1);
167 sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
170 } /* int cu_debug_resetfile(const char *file, int line, const char *func,
171 const char *filename) */
173 #endif /* COLLECT_DEBUG */