Introduce vtracelog
authorMaxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>
Tue, 3 Mar 2020 23:10:31 +0000 (18:10 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 4 Mar 2020 15:24:32 +0000 (10:24 -0500)
vtracelog works the same as vtracef, but takes a log level
as a parameter and has the same limitations.

Signed-off-by: Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
.gitignore
doc/examples/demo-tracelog/Makefile
doc/examples/demo-tracelog/demo-vtracelog.c [new file with mode: 0644]
doc/man/tracelog.3.txt
include/lttng/tracelog.h
liblttng-ust/tracelog.c

index 6574f44eb45901e56efb805d2bff772898a54f38..78009627ed682d5f122e481eea93d73d7465679d 100644 (file)
@@ -56,6 +56,7 @@ doc/examples/gen-tp/sample_tracepoint.h
 doc/examples/demo-tracef/demo-tracef
 doc/examples/demo-tracef/demo-vtracef
 doc/examples/demo-tracelog/demo-tracelog
+doc/examples/demo-tracelog/demo-vtracelog
 doc/examples/cmake-multiple-shared-libraries/build/
 
 doc/man/*.xml
index 0d9a20aa62f27ba59af9a8f521cc68b33aa02e55..623979d3cd0e5921c8f70f7fed845278561d7afc 100644 (file)
@@ -20,7 +20,7 @@ LIBS = -ldl -llttng-ust       # On Linux
 LOCAL_CPPFLAGS += -I.
 AM_V_P := :
 
-all: demo-tracelog
+all: demo-tracelog demo-vtracelog
 
 demo-tracelog.o: demo-tracelog.c
        @if $(AM_V_P); then set -x; else echo "  CC       $@"; fi; \
@@ -32,6 +32,16 @@ demo-tracelog: demo-tracelog.o
                $(CC) $(LDFLAGS) $(AM_CFLAGS) $(AM_LDFLAGS) $(CFLAGS) \
                -o $@ $< $(LIBS)
 
+demo-vtracelog.o: demo-vtracelog.c
+       @if $(AM_V_P); then set -x; else echo "  CC       $@"; fi; \
+               $(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) $(AM_CFLAGS) $(AM_CPPFLAGS) \
+               $(CFLAGS) -c -o $@ $<
+
+demo-vtracelog: demo-vtracelog.o
+       @if $(AM_V_P); then set -x; else echo "  CCLD     $@"; fi; \
+               $(CC) $(LDFLAGS) $(AM_CFLAGS) $(AM_LDFLAGS) $(CFLAGS) \
+               -o $@ $< $(LIBS)
+
 .PHONY: clean
 clean:
-       rm -f *.o *.a demo-tracelog
+       rm -f *.o *.a demo-tracelog demo-vtracelog
diff --git a/doc/examples/demo-tracelog/demo-vtracelog.c b/doc/examples/demo-tracelog/demo-vtracelog.c
new file mode 100644 (file)
index 0000000..c3e604c
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2020  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; version 2.1 of
+ * the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <lttng/tracelog.h>
+
+void print_err(const char* msg, ...)
+{
+       va_list ap;
+
+       va_start(ap, msg);
+       vtracelog(TRACE_ERR, msg, ap);
+       va_end(ap);
+}
+
+int main(int argc, char **argv)
+{
+       int i;
+       int delay = 0;
+       const char *str = "mystring test";
+       long l = 0x42;
+
+       if (argc > 2)
+               delay = atoi(argv[1]);
+
+       fprintf(stderr, "Demo program starting.\n");
+
+       sleep(delay);
+
+       fprintf(stderr, "Tracing... ");
+
+       for (i = 0; i < 5; i++) {
+               print_err("This is a \"%s\" formatted %d error event %lx", str, i, l);
+       }
+
+       fprintf(stderr, " done.\n");
+       return 0;
+}
index 1281ce7c8d940d5ec35d26055e3ed13d8c2fc3af..5cf1953ae9ea573e1ba6f82e3d58e9230b2e3aa9 100644 (file)
@@ -15,6 +15,7 @@ SYNOPSIS
 
 [verse]
 #define *tracelog*('level', 'fmt', ...)
+#define *vtracelog*('level', 'fmt', 'va_list' ap)
 
 Link with `-llttng-ust`.
 
@@ -34,9 +35,9 @@ The available values for the 'level' parameter are:
 
 include::log-levels.txt[]
 
-To use `tracelog()`, include `<lttng/tracelog.h>` where you need it, and
-link your application with `liblttng-ust`. See the <<example,EXAMPLE>>
-section below for a complete usage example.
+To use `tracelog()` or `vtracelog()`, include `<lttng/tracelog.h>` where you
+need it, and link your application with `liblttng-ust`.
+See the <<example,EXAMPLE>> section below for a complete usage example.
 
 Once your application is instrumented with `tracelog()` calls and
 ready to run, use man:lttng-enable-event(1) to enable the
@@ -67,7 +68,7 @@ If you do not need to attach a specific log level to a `tracelog()`
 call, use man:tracef(3) instead.
 
 See also the <<limitations,LIMITATIONS>> section below for important
-limitations to consider when using `tracelog()`.
+limitations to consider when using `tracelog()` or `vtracelog()`.
 
 
 [[example]]
index 4309d12e981eb524077677dd9f55c35154c37dc4..7aa8680987847ea2c76e9cae43c8aa05929dcc52 100644 (file)
@@ -59,6 +59,13 @@ TP_TRACELOG_CB_TEMPLATE(TRACE_DEBUG);
                                fmt, ## __VA_ARGS__); \
        } while (0)
 
+#define vtracelog(level, fmt, ap)                                      \
+       do {                                                            \
+               if (caa_unlikely(__tracepoint_lttng_ust_tracelog___##level.state)) \
+                       _lttng_ust_tracelog_##level(__FILE__, __LINE__, __func__, \
+                               fmt, ap); \
+       } while (0)
+
 #ifdef __cplusplus
 }
 #endif
index 65fc87ede57818c21d2af3b87ea7c8f3e133e5d7..ed8a067d84cec0eaf6c8e08b4710da4a90ef9940 100644 (file)
 #include "lttng-ust-tracelog-provider.h"
 
 #define TRACELOG_CB(level) \
-       void _lttng_ust_tracelog_##level(const char *file, \
+       static inline __attribute__((always_inline)) \
+       void __lttng_ust_vtracelog_##level(const char *file, \
                        int line, const char *func, \
-                       const char *fmt, ...) \
+                       const char *fmt, va_list ap) \
        { \
-               va_list ap; \
                char *msg; \
-               int len; \
+               const int len = vasprintf(&msg, fmt, ap); \
                \
-               va_start(ap, fmt); \
-               len = vasprintf(&msg, fmt, ap); \
                /* len does not include the final \0 */ \
                if (len < 0) \
                        goto end; \
                        LTTNG_UST_CALLER_IP()); \
                free(msg); \
        end: \
+               return; \
+       } \
+       \
+       void _lttng_ust_vtracelog_##level(const char *file, \
+                       int line, const char *func, \
+                       const char *fmt, va_list ap) \
+       { \
+               __lttng_ust_vtracelog_##level(file, line, func, fmt, ap); \
+       } \
+       \
+       void _lttng_ust_tracelog_##level(const char *file, \
+                       int line, const char *func, \
+                       const char *fmt, ...) \
+       { \
+               va_list ap; \
+               \
+               va_start(ap, fmt); \
+               __lttng_ust_vtracelog_##level(file, line, func, fmt, ap); \
                va_end(ap); \
        }
 
This page took 0.028545 seconds and 4 git commands to generate.