Skip to contentSkip to conversation

libtraceevent: Add __tep_vprint() for overrides

PATCH New Steven Rostedt 1 participant 4 messages May 14, 2021

lore
Share & tools
lore
#0 lore raw

cover libtraceevent: Add __tep_vprint() for overrides

From: "Steven Rostedt (VMware)" <[email protected]>

I found that I wanted to override tep_vprint() but still call the default function from my override, and found that I could not do so. That is the purpose of the "__" versions that were used before.

Also, do some clean ups that were discovered while creating this.

Steven Rostedt (VMware) (3):
libtraceevent: Add include of event-parse.h into event-utils.h
libtraceevent: Have the header protection be more name space safe
libtraceevent: Add __tep_vprint() for overrides to use

 src/event-parse.h |  4 ++--
 src/event-utils.h | 12 ++++++++++--
 src/parse-utils.c | 23 +++++++++++++++++++++++
 3 files changed, 35 insertions(+), 4 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH 1/3 libtraceevent: Add include of event-parse.h into event-utils.h

From: "Steven Rostedt (VMware)" <[email protected]>

The event-parse.h references enum tep_loglevel which is defined in event-utils.h. If a program includes event-utils.h first, it will get a warning about using that enum in a parameter before it is declared.

---
 src/event-utils.h | 2 ++
 1 file changed, 2 insertions(+)
src/event-utils.h +2 -0
index 695905d..da95027 100644
@@ -10,6 +10,8 @@
 #include <stdarg.h>
 #include <stdbool.h>
 
+#include <event-parse.h>
+
 void tep_warning(const char *fmt, ...);
 void tep_info(const char *fmt, ...);
 /* Can be overridden */
Signature · 1 line
2.29.2
#2 lore raw

PATCH 2/3 libtraceevent: Have the header protection be more name space safe

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

The header protection for event-utils.h is just _UTIL_H which is not very name space safe and if another header were to use that, it could cause this header not to be included (or this header could cause the other header not to be included). Also update event-parse.h as well.

---
 src/event-parse.h | 4 ++--
 src/event-utils.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
src/event-parse.h +2 -2
index 77be2b9..d4a876f 100644
@@ -3,8 +3,8 @@
  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
  *
  */
-#ifndef _PARSE_EVENTS_H
-#define _PARSE_EVENTS_H
+#ifndef __TEP_PARSE_EVENTS_H
+#define __TEP_PARSE_EVENTS_H
 
 #include <stdbool.h>
 #include <stdarg.h>
src/event-utils.h +2 -2
index da95027..607be0c 100644
@@ -3,8 +3,8 @@
  * Copyright (C) 2010 Red Hat Inc, Steven Rostedt <[email protected]>
  *
  */
-#ifndef __UTIL_H
-#define __UTIL_H
+#ifndef __TEP_EVENT_UTIL_H
+#define __TEP_EVENT_UTIL_H
 
 #include <ctype.h>
 #include <stdarg.h>
Signature · 1 line
2.29.2
#3 lore raw

PATCH 3/3 libtraceevent: Add __tep_vprint() for overrides to use

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

If a program wants to add something special to tep_vprint() but then also call the default tep_vprint() it can't. If it overrides the function, it loses the functionality for it.

Add __tep_vprint() for this use case.

Example:

int tep_vprint(const char *name, enum tep_loglevel level,
bool print_err, const char *fmt, va_list ap)

 {
	if (quiet && !strcmp(name, "myprog"))
		return 0;
	return __tep_vprint(name, level, print_err, fmt, ap);

}

---
 src/event-utils.h |  6 ++++++
 src/parse-utils.c | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+)
src/event-utils.h +6 -0
index 607be0c..d377201 100644
@@ -14,10 +14,16 @@
 
 void tep_warning(const char *fmt, ...);
 void tep_info(const char *fmt, ...);
+
 /* Can be overridden */
 int tep_vprint(const char *name, enum tep_loglevel level,
 	       bool print_err, const char *fmt, va_list ap);
 
+/* The actual call of tep_vprint() for overrides to use */
+int __tep_vprint(const char *name, enum tep_loglevel level,
+		 bool print_err, const char *fmt, va_list ap);
+
+
 #define __deprecated(msg) __attribute__((deprecated("msg")))
 
 /* For backward compatibilty, do not use */
src/parse-utils.c +23 -0
index 04237ba..89bf1cd 100644
@@ -9,6 +9,7 @@
 #include <stdarg.h>
 #include <errno.h>
 
+#include "event-utils.h"
 #include "event-parse.h"
 
 #define __weak __attribute__((weak))
@@ -42,6 +43,28 @@ void tep_set_loglevel(enum tep_loglevel level)
  */
 int __weak tep_vprint(const char *name, enum tep_loglevel level,
 		      bool print_err, const char *fmt, va_list ap)
+{
+	return __tep_vprint(name, level, print_err, fmt, ap);
+}
+
+/**
+ * __tep_vprint - print library log messages
+ * @name: name of the library.
+ * @level: severity of the log message. This parameter is not used in this implementation, but as
+ *	   the function is weak and can be overridden, having the log level could be useful
+ *	   for other implementations.
+ * @print_err: whether to print the errno, if non zero.
+ * @fmt: printf format string of the message.
+ * @ap: list of printf parameters.
+ *
+ * This function is used to print all messages from traceevent, tracefs and trace-cmd libraries.
+ * It is defined as weak, so the application that uses those libraries can override it in order
+ * to implement its own logic for printing library logs.
+ *
+ * Return the value of errno at the function enter.
+ */
+int __tep_vprint(const char *name, enum tep_loglevel level,
+		      bool print_err, const char *fmt, va_list ap)
 {
 	int ret = errno;
 
Signature · 1 line
2.29.2