summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f369a9d)
raw | patch | inline | side by side (parent: f369a9d)
author | octo <octo> | |
Mon, 12 Dec 2005 07:35:29 +0000 (07:35 +0000) | ||
committer | octo <octo> | |
Mon, 12 Dec 2005 07:35:29 +0000 (07:35 +0000) |
src/utils_debug.c | [new file with mode: 0644] | patch | blob |
src/utils_debug.h | [new file with mode: 0644] | patch | blob |
diff --git a/src/utils_debug.c b/src/utils_debug.c
--- /dev/null
+++ b/src/utils_debug.c
@@ -0,0 +1,196 @@
+/**
+ * collectd - src/utils_debug.c
+ * Copyright (C) 2005 Niki W. Waibel
+ *
+ * This program is free software; you can redistribute it and/
+ * or modify it under the terms of the GNU General Public Li-
+ * cence as published by the Free Software Foundation; either
+ * version 2 of the Licence, or any later version.
+ *
+ * This program is distributed in the hope that it will be use-
+ * ful, but WITHOUT ANY WARRANTY; without even the implied war-
+ * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public Licence for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * Licence along with this program; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
+ * USA.
+ *
+ * Author:
+ * Niki W. Waibel <niki.waibel@gmx.net>
+**/
+
+#include "common.h"
+#include "utils_debug.h"
+
+/* *** *** *** global variables *** *** *** */
+#if COLLECT_DEBUG
+
+#define DEFAULT_FILENAME "collectd.log"
+
+static void cu_vdebug(const char *file, int line, const char *func,
+ const char *format, va_list ap);
+
+/* if preemptive threads are used, these vars need some sort of locking! */
+/* pth is non-preemptive, so no locking is necessary (?) */
+static FILE *cu_debug_file = NULL;
+static char *cu_debug_filename = NULL;
+
+static void
+cu_vdebug(const char *file, int line, const char *func,
+ const char *format, va_list ap)
+{
+ FILE *f;
+ if(cu_debug_file != NULL) {
+ f = cu_debug_file;
+ } else {
+ /* stderr might be redirected to /dev/null. in that case */
+ /* you'll not see anything... */
+ f = stderr;
+ }
+
+ fprintf(f, "%s:%d:%s(): ",
+ file, line, func);
+ vfprintf(f, format, ap);
+ fprintf(f, "\n");
+ fflush(f);
+} /* static void cu_vdebug(const char *file, int line, const char *func,
+ const char *format, va_list ap) */
+
+void
+cu_debug(const char *file, int line, const char *func,
+ const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ cu_vdebug(file, line, func, format, ap);
+ va_end(ap);
+} /* void cu_debug(const char *file, int line, const char *func,
+ const char *format, ...) */
+
+int
+cu_debug_startfile(const char *file, int line, const char *func,
+ const char *format, ...)
+{
+ va_list ap;
+
+ if(cu_debug_file != NULL) {
+ DBG("Don't call this function more then once without"
+ " calling cu_debug_stopfile().");
+ return EXIT_FAILURE;
+ }
+
+ if(cu_debug_filename == NULL) {
+ cu_debug_filename = sstrdup(DEFAULT_FILENAME);
+ }
+
+ cu_debug_file = fopen(cu_debug_filename, "a");
+ if(cu_debug_file == NULL) {
+ DBG("Cannot open debug file %s: %s.\n",
+ cu_debug_filename, strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ va_start(ap, format);
+ cu_vdebug(file, line, func, format, ap);
+ va_end(ap);
+
+ return EXIT_SUCCESS;
+} /* int cu_debug_start(const char *file, int line, const char *func,
+ const char *format, ...) */
+
+int
+cu_debug_stopfile(const char *file, int line, const char *func,
+ const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ cu_vdebug(file, line, func, format, ap);
+ va_end(ap);
+
+ if(cu_debug_file == NULL) {
+ DBG("Don't call this function more then once or without"
+ " calling cu_debug_startfile().");
+ return EXIT_FAILURE;
+ }
+
+ if(fclose(cu_debug_file) != 0) {
+ DBG("Cannot close debug file %s: %s.\n",
+ cu_debug_filename, strerror(errno));
+ return EXIT_FAILURE;
+ }
+ cu_debug_file = NULL;
+
+ sfree(cu_debug_filename);
+
+ return EXIT_SUCCESS;
+} /* int cu_debug_stop(const char *file, int line, const char *func,
+ const char *format, ...) */
+
+int
+cu_debug_resetfile(const char *file, int line, const char *func,
+ const char *filename)
+{
+ if(filename == NULL) {
+ DBG("You have to set filename when calling this function!\n");
+ return EXIT_FAILURE;
+ }
+ if(cu_debug_file != NULL) {
+ char *save_filename = NULL;
+
+ /* DBG_STARTFILE was called already */
+ /* reopen file */
+
+ DBG_STOPFILE("Closing %s and reopening %s.",
+ cu_debug_filename, filename);
+ save_filename = smalloc(strlen(cu_debug_filename)+1);
+ sstrncpy(save_filename, cu_debug_filename,
+ strlen(cu_debug_filename)+1);
+ cu_debug_filename = smalloc(strlen(filename)+1);
+ sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
+ DBG_STARTFILE("Reopening %s after closing %s.",
+ filename, save_filename);
+ sfree(save_filename);
+ return EXIT_SUCCESS;
+ }
+
+ /* DBG_STARTFILE was NOT called already */
+ /* setting filename only */
+
+ if(cu_debug_filename != NULL) {
+ sfree(cu_debug_filename);
+ }
+ cu_debug_filename = smalloc(strlen(filename)+1);
+ sstrncpy(cu_debug_filename, filename, strlen(filename)+1);
+
+ return EXIT_SUCCESS;
+} /* int cu_debug_resetfile(const char *file, int line, const char *func,
+ const char *filename) */
+
+#else /* !COLLECT_DEBUG */
+
+void
+cu_debug(const char *file, int line, const char *func, const char *format, ...)
+{
+}
+int
+cu_debug_startfile(const char *file, int line, const char *func, const char *format, ...)
+{
+ return EXIT_SUCCESS;
+}
+int
+cu_debug_stopfile(const char *file, int line, const char *func, const char *format, ...)
+{
+ return EXIT_SUCCESS;
+}
+int
+cu_debug_resetfile(const char *file, int line, const char *func, const char *filename)
+{
+ return EXIT_SUCCESS;
+}
+
+#endif /* COLLECT_DEBUG */
+
diff --git a/src/utils_debug.h b/src/utils_debug.h
--- /dev/null
+++ b/src/utils_debug.h
@@ -0,0 +1,50 @@
+/**
+ * collectd - src/utils_debug.h
+ * Copyright (C) 2005 Niki W. Waibel
+ *
+ * This program is free software; you can redistribute it and/
+ * or modify it under the terms of the GNU General Public Li-
+ * cence as published by the Free Software Foundation; either
+ * version 2 of the Licence, or any later version.
+ *
+ * This program is distributed in the hope that it will be use-
+ * ful, but WITHOUT ANY WARRANTY; without even the implied war-
+ * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public Licence for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * Licence along with this program; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
+ * USA.
+ *
+ * Author:
+ * Niki W. Waibel <niki.waibel@gmx.net>
+**/
+
+#if !COLLECTD_UTILS_DEBUG_H
+#define COLLECTD_UTILS_DEBUG_H 1
+
+#define DBG(...) cu_debug(__FILE__, __LINE__, __func__, \
+ __VA_ARGS__)
+
+#define DBG_STARTFILE(...) cu_debug_startfile(__FILE__, __LINE__, \
+ __func__, __VA_ARGS__)
+#define DBG_STOPFILE(...) cu_debug_stopfile(__FILE__, __LINE__, \
+ __func__, __VA_ARGS__)
+
+#define DBG_RESETFILE(file) cu_debug_resetfile(__FILE__, __LINE__, __func__, \
+ filename)
+
+void cu_debug(const char *file, int line, const char *func,
+ const char *format, ...);
+
+int cu_debug_startfile(const char *file, int line, const char *func,
+ const char *format, ...);
+int cu_debug_stopfile(const char *file, int line, const char *func,
+ const char *format, ...);
+
+int cu_debug_resetfile(const char *file, int line, const char *func,
+ const char *filename);
+
+#endif /* !COLLECTD_UTILS_DEBUG_H */
+