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 **/
23 #include "common.h"
24 #include "utils_debug.h"
26 #if COLLECT_DEBUG
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;
36 static void
37 cu_vdebug(const char *file, int line, const char *func,
38 const char *format, va_list ap)
39 {
40 FILE *f;
41 if(cu_debug_file != NULL) {
42 f = cu_debug_file;
43 } else {
44 /* stderr might be redirected to /dev/null. in that case */
45 /* you'll not see anything... */
46 f = stderr;
47 }
49 fprintf(f, "%s:%d:%s(): ",
50 file, line, func);
51 vfprintf(f, format, ap);
52 fprintf(f, "\n");
53 fflush(f);
54 } /* static void cu_vdebug(const char *file, int line, const char *func,
55 const char *format, va_list ap) */
57 void
58 cu_debug(const char *file, int line, const char *func,
59 const char *format, ...)
60 {
61 va_list ap;
63 va_start(ap, format);
64 cu_vdebug(file, line, func, format, ap);
65 va_end(ap);
66 } /* void cu_debug(const char *file, int line, const char *func,
67 const char *format, ...) */
69 int
70 cu_debug_startfile(const char *file, int line, const char *func,
71 const char *filename, const char *format, ...)
72 {
73 va_list ap;
75 if(cu_debug_file != NULL) {
76 DBG("Don't call this function more then once without"
77 " calling cu_debug_stopfile().");
78 return EXIT_FAILURE;
79 }
81 if(cu_debug_filename == NULL) {
82 cu_debug_filename = sstrdup(filename);
83 }
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));
89 return EXIT_FAILURE;
90 }
92 va_start(ap, format);
93 cu_vdebug(file, line, func, format, ap);
94 va_end(ap);
96 return EXIT_SUCCESS;
97 } /* int cu_debug_start(const char *file, int line, const char *func,
98 const char *format, ...) */
100 int
101 cu_debug_stopfile(const char *file, int line, const char *func,
102 const char *format, ...)
103 {
104 va_list ap;
106 va_start(ap, format);
107 cu_vdebug(file, line, func, format, ap);
108 va_end(ap);
110 if(cu_debug_file == NULL) {
111 DBG("Don't call this function more then once or without"
112 " calling cu_debug_startfile().");
113 return EXIT_FAILURE;
114 }
116 if(fclose(cu_debug_file) != 0) {
117 DBG("Cannot close debug file %s: %s.\n",
118 cu_debug_filename, strerror(errno));
119 return EXIT_FAILURE;
120 }
121 cu_debug_file = NULL;
123 sfree(cu_debug_filename);
125 return EXIT_SUCCESS;
126 } /* int cu_debug_stop(const char *file, int line, const char *func,
127 const char *format, ...) */
129 int
130 cu_debug_resetfile(const char *file, int line, const char *func,
131 const char *filename)
132 {
133 if(filename == NULL) {
134 DBG("You have to set filename when calling this function!\n");
135 return EXIT_FAILURE;
136 }
137 if(cu_debug_file != NULL) {
138 char *save_filename = NULL;
140 /* DBG_STARTFILE was called already */
141 /* reopen file */
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);
153 return EXIT_SUCCESS;
154 }
156 /* DBG_STARTFILE was NOT called already */
157 /* setting filename only */
159 if(cu_debug_filename != NULL) {
160 sfree(cu_debug_filename);
161 }
162 cu_debug_filename = smalloc(strlen(filename)+1);
163 sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
165 return EXIT_SUCCESS;
166 } /* int cu_debug_resetfile(const char *file, int line, const char *func,
167 const char *filename) */
169 #endif /* COLLECT_DEBUG */