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"
26 /* *** *** *** global variables *** *** *** */
29 #define DEFAULT_FILENAME "collectd.log"
31 static void cu_vdebug(const char *file, int line, const char *func,
32 const char *format, va_list ap);
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;
40 cu_vdebug(const char *file, int line, const char *func,
41 const char *format, va_list ap)
44 if(cu_debug_file != NULL) {
47 /* stderr might be redirected to /dev/null. in that case */
48 /* you'll not see anything... */
52 fprintf(f, "%s:%d:%s(): ",
54 vfprintf(f, format, ap);
57 } /* static void cu_vdebug(const char *file, int line, const char *func,
58 const char *format, va_list ap) */
61 cu_debug(const char *file, int line, const char *func,
62 const char *format, ...)
67 cu_vdebug(file, line, func, format, ap);
69 } /* void cu_debug(const char *file, int line, const char *func,
70 const char *format, ...) */
73 cu_debug_startfile(const char *file, int line, const char *func,
74 const char *format, ...)
78 if(cu_debug_file != NULL) {
79 DBG("Don't call this function more then once without"
80 " calling cu_debug_stopfile().");
84 if(cu_debug_filename == NULL) {
85 cu_debug_filename = sstrdup(DEFAULT_FILENAME);
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));
96 cu_vdebug(file, line, func, format, ap);
100 } /* int cu_debug_start(const char *file, int line, const char *func,
101 const char *format, ...) */
104 cu_debug_stopfile(const char *file, int line, const char *func,
105 const char *format, ...)
109 va_start(ap, format);
110 cu_vdebug(file, line, func, format, ap);
113 if(cu_debug_file == NULL) {
114 DBG("Don't call this function more then once or without"
115 " calling cu_debug_startfile().");
119 if(fclose(cu_debug_file) != 0) {
120 DBG("Cannot close debug file %s: %s.\n",
121 cu_debug_filename, strerror(errno));
124 cu_debug_file = NULL;
126 sfree(cu_debug_filename);
129 } /* int cu_debug_stop(const char *file, int line, const char *func,
130 const char *format, ...) */
133 cu_debug_resetfile(const char *file, int line, const char *func,
134 const char *filename)
136 if(filename == NULL) {
137 DBG("You have to set filename when calling this function!\n");
140 if(cu_debug_file != NULL) {
141 char *save_filename = NULL;
143 /* DBG_STARTFILE was called already */
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);
159 /* DBG_STARTFILE was NOT called already */
160 /* setting filename only */
162 if(cu_debug_filename != NULL) {
163 sfree(cu_debug_filename);
165 cu_debug_filename = smalloc(strlen(filename)+1);
166 sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
169 } /* int cu_debug_resetfile(const char *file, int line, const char *func,
170 const char *filename) */
172 #else /* !COLLECT_DEBUG */
175 cu_debug(const char *file, int line, const char *func, const char *format, ...)
179 cu_debug_startfile(const char *file, int line, const char *func, const char *format, ...)
184 cu_debug_stopfile(const char *file, int line, const char *func, const char *format, ...)
189 cu_debug_resetfile(const char *file, int line, const char *func, const char *filename)
194 #endif /* COLLECT_DEBUG */