[Pdbg] [PATCH 05/18] libpdbg: Add logging api

Amitay Isaacs amitay at ozlabs.org
Wed May 16 16:17:23 AEST 2018


From: Amitay Isaacs <amitay at gmail.com>

To avoid churn define short-cut macros for logging using PR_ prefix.

Signed-off-by: Amitay Isaacs <amitay at gmail.com>
---
 Makefile.am       |  3 ++-
 libpdbg/debug.c   | 59 +++++++++++++++++++++++++++++++++++++++++++++++
 libpdbg/debug.h   | 32 +++++++++++++++++++++++++
 libpdbg/libpdbg.h | 13 +++++++++++
 4 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 libpdbg/debug.c
 create mode 100644 libpdbg/debug.h

diff --git a/Makefile.am b/Makefile.am
index 6c138da..8760184 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,7 +50,8 @@ libpdbg_la_SOURCES = \
 	libpdbg/adu.c \
 	libpdbg/device.c \
 	libpdbg/target.c \
-	libpdbg/htm.c
+	libpdbg/htm.c \
+	libpdbg/debug.c
 
 M4_V = $(M4_V_$(V))
 M4_V_ = $(M4_V_$(AM_DEFAULT_VERBOSITY))
diff --git a/libpdbg/debug.c b/libpdbg/debug.c
new file mode 100644
index 0000000..99fc726
--- /dev/null
+++ b/libpdbg/debug.c
@@ -0,0 +1,59 @@
+/* Copyright 2018 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 	http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "debug.h"
+
+static int pdbg_loglevel = PDBG_ERROR;
+
+static void pdbg_logfunc_default(int loglevel, const char *fmt, va_list ap)
+{
+	if (loglevel > pdbg_loglevel)
+		return;
+
+	vfprintf(stderr, fmt, ap);
+}
+
+static pdbg_log_func_t pdbg_logfunc = pdbg_logfunc_default;
+
+void pdbg_set_logfunc(pdbg_log_func_t fn)
+{
+	if (fn == NULL)
+		return;
+
+	pdbg_logfunc = fn;
+}
+
+void pdbg_set_loglevel(int loglevel)
+{
+	if (loglevel < PDBG_ERROR)
+		pdbg_loglevel = PDBG_ERROR;
+	else if (loglevel > PDBG_DEBUG)
+		pdbg_loglevel = PDBG_DEBUG;
+	else
+		pdbg_loglevel = loglevel;
+}
+
+void pdbg_log(int loglevel, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	pdbg_logfunc(loglevel, fmt, ap);
+	va_end(ap);
+}
diff --git a/libpdbg/debug.h b/libpdbg/debug.h
new file mode 100644
index 0000000..c318e3a
--- /dev/null
+++ b/libpdbg/debug.h
@@ -0,0 +1,32 @@
+/* Copyright 2018 IBM Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 	http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __LIBPDBG_DEBUG_H
+#define __LIBPDBG_DEBUG_H
+
+#include "libpdbg.h"
+
+#define PR_ERROR(x, args...) \
+	pdbg_log(PDBG_ERROR, x, ##args)
+#define PR_WARNING(x, args...) \
+	pdbg_log(PDBG_WARNING, x, ##args)
+#define PR_NOTICE(x, args...) \
+	pdbg_log(PDBG_NOTICE, x, ##args)
+#define PR_INFO(x, args...) \
+	pdbg_log(PDBG_INFO, x, ##args)
+#define PR_DEBUG(x, args...) \
+	pdbg_log(PDBG_DEBUG, "%s[%d]: " x, __FUNCTION__, __LINE__, ##args)
+
+#endif
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index 4b053b2..0dac243 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -3,6 +3,7 @@
 
 #include <stdio.h>
 #include <stdint.h>
+#include <stdarg.h>
 
 struct pdbg_target;
 struct pdbg_target_class;
@@ -136,4 +137,16 @@ int adu_putmem(struct pdbg_target *target, uint64_t addr, uint8_t *input, uint64
 int opb_read(struct pdbg_target *target, uint32_t addr, uint32_t *data);
 int opb_write(struct pdbg_target *target, uint32_t addr, uint32_t data);
 
+#define PDBG_ERROR	0
+#define PDBG_WARNING	1
+#define PDBG_NOTICE	2
+#define PDBG_INFO	3
+#define PDBG_DEBUG	4
+
+typedef void (*pdbg_log_func_t)(int loglevel, const char *fmt, va_list ap);
+
+void pdbg_set_logfunc(pdbg_log_func_t fn);
+void pdbg_set_loglevel(int loglevel);
+void pdbg_log(int loglevel, const char *fmt, ...);
+
 #endif
-- 
2.17.0



More information about the Pdbg mailing list