2 * collectd - src/utils_debug.c
3 * Copyright (C) 2005 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>
24 #include "utils_debug.h"
28 static void cu_vdebug(const char *file, int line, const char *func,
29 const char *format, va_list ap);
31 /* if preemptive threads are used, these vars need some sort of locking! */
32 /* pth is non-preemptive, so no locking is necessary */
33 static FILE *cu_debug_file = NULL;
34 static char *cu_debug_filename = NULL;
37 cu_vdebug(const char *file, int line, const char *func,
38 const char *format, va_list ap)
41 if(cu_debug_file != NULL) {
44 /* stderr might be redirected to /dev/null. in that case */
45 /* you'll not see anything... */
49 fprintf(f, "%s:%d:%s(): ",
51 vfprintf(f, format, ap);
54 } /* static void cu_vdebug(const char *file, int line, const char *func,
55 const char *format, va_list ap) */
58 cu_debug(const char *file, int line, const char *func,
59 const char *format, ...)
64 cu_vdebug(file, line, func, format, ap);
66 } /* void cu_debug(const char *file, int line, const char *func,
67 const char *format, ...) */
70 cu_debug_startfile(const char *file, int line, const char *func,
71 const char *filename, const char *format, ...)
75 if(cu_debug_file != NULL) {
76 DBG("Don't call this function more then once without"
77 " calling cu_debug_stopfile().");
81 if(cu_debug_filename == NULL) {
82 cu_debug_filename = sstrdup(filename);
85 cu_debug_file = fopen(cu_debug_filename, "a");
86 if(cu_debug_file == NULL) {
87 DBG("Cannot open debug file %s: %s.\n",
88 cu_debug_filename, strerror(errno));
93 cu_vdebug(file, line, func, format, ap);
97 } /* int cu_debug_start(const char *file, int line, const char *func,
98 const char *format, ...) */
101 cu_debug_stopfile(const char *file, int line, const char *func,
102 const char *format, ...)
106 va_start(ap, format);
107 cu_vdebug(file, line, func, format, ap);
110 if(cu_debug_file == NULL) {
111 DBG("Don't call this function more then once or without"
112 " calling cu_debug_startfile().");
116 if(fclose(cu_debug_file) != 0) {
117 DBG("Cannot close debug file %s: %s.\n",
118 cu_debug_filename, strerror(errno));
121 cu_debug_file = NULL;
123 sfree(cu_debug_filename);
126 } /* int cu_debug_stop(const char *file, int line, const char *func,
127 const char *format, ...) */
130 cu_debug_resetfile(const char *file, int line, const char *func,
131 const char *filename)
133 if(filename == NULL) {
134 DBG("You have to set filename when calling this function!\n");
137 if(cu_debug_file != NULL) {
138 char *save_filename = NULL;
140 /* DBG_STARTFILE was called already */
143 DBG_STOPFILE("Closing %s and reopening %s.",
144 cu_debug_filename, filename);
145 save_filename = smalloc(strlen(cu_debug_filename)+1);
146 sstrncpy(save_filename, cu_debug_filename,
147 strlen(cu_debug_filename)+1);
148 cu_debug_filename = smalloc(strlen(filename)+1);
149 sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
150 DBG_STARTFILE("Reopening %s after closing %s.",
151 filename, save_filename);
152 sfree(save_filename);
156 /* DBG_STARTFILE was NOT called already */
157 /* setting filename only */
159 if(cu_debug_filename != NULL) {
160 sfree(cu_debug_filename);
162 cu_debug_filename = smalloc(strlen(filename)+1);
163 sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
166 } /* int cu_debug_resetfile(const char *file, int line, const char *func,
167 const char *filename) */
169 #endif /* COLLECT_DEBUG */