From 849202d4eec83bef9ea3ebba9c73e31a6ba28d49 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Wed, 20 Nov 2013 15:24:02 -0500 Subject: [PATCH] JUL: add Hello.java in doc/examples Signed-off-by: David Goulet Signed-off-by: Mathieu Desnoyers --- doc/examples/Makefile.am | 23 ++++++- doc/examples/java-jul/.intree | 0 doc/examples/java-jul/Hello.java | 102 +++++++++++++++++++++++++++++++ doc/examples/java-jul/Makefile | 48 +++++++++++++++ doc/examples/java-jul/README | 7 +++ doc/examples/java-jul/run | 24 ++++++++ 6 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 doc/examples/java-jul/.intree create mode 100644 doc/examples/java-jul/Hello.java create mode 100644 doc/examples/java-jul/Makefile create mode 100644 doc/examples/java-jul/README create mode 100755 doc/examples/java-jul/run diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index 553be33c..725806d7 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -4,6 +4,15 @@ doc_examples_gen_tpdir = ${docdir}/examples/gen-tp doc_examples_demodir = ${docdir}/examples/demo doc_examples_hello_static_libdir = ${docdir}/examples/hello-static-lib +if BUILD_JNI_INTERFACE +doc_examples_java_juldir = ${docdir}/examples/java-jul +dist_doc_examples_java_jul_DATA = java-jul/Makefile \ + java-jul/Hello.java \ + java-jul/run +SUBDIRS_JUL = java-jul +bin_SCRIPTS = java-jul/Makefile +endif + dist_doc_examples_DATA = README dist_doc_examples_easy_ust_DATA = easy-ust/Makefile \ @@ -40,20 +49,30 @@ SUBDIRS_PROXY = easy-ust demo gen-tp hello-static-lib all-local: @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ cp -f $(srcdir)/Makefile.examples.template $(builddir); \ - for subdir in $(SUBDIRS_PROXY); do \ + for subdir in $(SUBDIRS_PROXY) $(SUBDIRS_JUL); do \ cp -fR $(srcdir)/$$subdir $(builddir); \ done; \ fi for subdir in $(SUBDIRS_PROXY); do \ (cd $$subdir && $(MAKE) AM_CPPFLAGS="$(CPPFLAGS) -I../../../include/" AM_CFLAGS='$(CFLAGS)' AM_LDFLAGS='$(LDFLAGS) -L../../../liblttng-ust/.libs/ -Wl,-rpath="$(PWD)/../../liblttng-ust/.libs/"' LTTNG_GEN_TP_PATH="../../../tools/" $(AM_MAKEFLAGS) all && cd ..) || exit 1; \ done + @if [ x"$(SUBDIRS_JUL)" != x"" ]; then \ + for subdir in $(SUBDIRS_JUL); do \ + (cd $(SUBDIRS_JUL) && $(MAKE) JAVA_CLASSPATH_OVERRIDE="../../../liblttng-ust-jul" JAVA_JARFILE_OVERRIDE="liblttng-ust-jul.jar" $(AM_MAKEFLAGS) all && cd ..) || exit 1; \ + done; \ + fi clean-local: for subdir in $(SUBDIRS_PROXY); do \ (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) clean && cd ..) || exit 1; \ done + @if [ x"$(SUBDIRS_JUL)" != x"" ]; then \ + for subdir in $(SUBDIRS_JUL); do \ + (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) clean && cd ..) || exit 1; \ + done; \ + fi @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ - for subdir in $(SUBDIRS_PROXY); do \ + for subdir in $(SUBDIRS_PROXY) $(SUBDIRS_JUL); do \ rm -rf $(builddir)/$$subdir; \ done; \ rm -f $(builddir)/Makefile.examples.template; \ diff --git a/doc/examples/java-jul/.intree b/doc/examples/java-jul/.intree new file mode 100644 index 00000000..e69de29b diff --git a/doc/examples/java-jul/Hello.java b/doc/examples/java-jul/Hello.java new file mode 100644 index 00000000..a4414723 --- /dev/null +++ b/doc/examples/java-jul/Hello.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2013 - David Goulet + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +/* + * That's the import you need being the path in the liblttng-ust-jul Jar file. + */ +import org.lttng.ust.jul.LTTngAgent; + +public class Hello +{ + /* Of course :) */ + private static final int answer = 42; + + /* + * Static reference to the LTTngAgent. Used to dispose of it at the end + * which is recommended but not mandatory to do. + */ + private static LTTngAgent lttngAgent; + + public static void main(String args[]) throws Exception + { + /* + * For this example, a custom "hello" logger is created. Note that JUL + * has a default "global" that can also be used. + */ + Logger helloLog = Logger.getLogger("hello"); + + /* + * Get the LTTngAgent singelton reference. This will also initialize + * the Agent and make it register to the session daemon if available. + * When this returns, the Agent is registered and fully ready. If no + * session daemon is found, it will return and retry every 3 seconds in + * the background. TCP is used for communication. + * + * Note that the LTTngAgent once registered is a seperate thread in + * your Java application. + */ + lttngAgent = LTTngAgent.getLTTngAgent(); + + /* + * Gives you time to do some lttng commands before any event is hit. + */ + Thread.sleep(5000); + + /* Trigger a tracing event using the JUL Logger created before. */ + helloLog.info("Hello World, the answer is " + answer); + + /* + * From this point on, the above message will be collected in the trace + * if the event "hello" is enabled for the JUL domain using the lttng + * command line or the lttng-ctl API. For instance: + * + * $ lttng enable-event -j hello + * + * A new logger is created here and fired after. The Agent has an + * internal timer that is fired every 5 seconds in order to enable + * events that were not found at first but might need to be enabled + * when new Logger appears. Unfortunately, there is no way right now to + * get notify of that so we have to actively poll. + * + * Using the --all command for instance, it will make this Logger + * available in a LTTng trace after the internal Agent's timer is + * fired. (lttng enable-event -a -j). + */ + Logger helloLogDelayed = Logger.getLogger("hello_delay"); + + System.out.println("Firing hello delay in 10 seconds..."); + Thread.sleep(10000); + helloLogDelayed.info("Hello World delayed..."); + + System.out.println("Cleaning Hello"); + + /* + * Again, this is highly recommended so the session daemon socket gets + * cleaned up explicitely but it is not mandatory to do this step. + */ + lttngAgent.dispose(); + } +} diff --git a/doc/examples/java-jul/Makefile b/doc/examples/java-jul/Makefile new file mode 100644 index 00000000..3af44d3e --- /dev/null +++ b/doc/examples/java-jul/Makefile @@ -0,0 +1,48 @@ +# +# Copyright (C) 2013 - David Goulet +# +# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR +# IMPLIED. ANY USE IS AT YOUR OWN RISK. +# +# Permission is hereby granted to use or copy this program for any purpose, +# provided the above notices are retained on all copies. Permission to modify +# the code and to distribute modified code is granted, provided the above +# notices are retained, and a notice that the code was modified is included +# with the above copyright notice. +# +# This Makefile is not using automake so that users may see how to build a +# program with tracepoint provider probes as stand-alone shared objects. +# +# This makefile is purposefully kept simple to support GNU and BSD make. +# + +# Default JUL jar name. +JARFILE=liblttng-ust-jul.jar + +# Check if the top level makefile overrides the JUL Jar file name. +ifneq "$(JAVA_JARFILE_OVERRIDE)" "" + JARFILE=$(JAVA_JARFILE_OVERRIDE) +endif + +# Check if the top level makefile overrides the JUL classpath. +ifeq "$(JAVA_CLASSPATH_OVERRIDE)" "" + CLASSPATH=/usr/local/lib/lttng/java/$(JARFILE):/usr/lib/lttng/java/$(JARFILE) +else + CLASSPATH=$(JAVA_CLASSPATH_OVERRIDE)/$(JARFILE) +endif + +JFLAGS = -g +JC = javac -cp "$(CLASSPATH):." +.SUFFIXES: .java .class +.java.class: + $(JC) $(JFLAGS) $*.java + +CLASSES = Hello.java + +all: classes + +classes: $(CLASSES:.java=.class) + +.PHONY: clean +clean: + $(RM) *.class diff --git a/doc/examples/java-jul/README b/doc/examples/java-jul/README new file mode 100644 index 00000000..d8998b69 --- /dev/null +++ b/doc/examples/java-jul/README @@ -0,0 +1,7 @@ +Type "make" to compile and use "./run" to start the example. + +The Hello.java file contains a lot of useful information in order to instrument +your application. + +The "run" file shows you how to link your Java application with the LTTng UST +JUL library. diff --git a/doc/examples/java-jul/run b/doc/examples/java-jul/run new file mode 100755 index 00000000..da8365b7 --- /dev/null +++ b/doc/examples/java-jul/run @@ -0,0 +1,24 @@ +#!/bin/bash + +# +# The -cp path should be changed to the lttng ust JUL jar file on your system +# or locally to the project. Same goes for the Java library path in order to +# find the JNI JUL library. +# + +DIR=`dirname $0` +JARFILE="liblttng-ust-jul.jar" + +cd $DIR + +if [ -f "$DIR/.intree" ]; then + CLASSPATH="../../../liblttng-ust-jul/$JARFILE" + LIBPATH="../../../liblttng-ust-jul/.libs" +else + CLASSPATH="/usr/local/lib/lttng/java/$JARFILE:/usr/lib/lttng/java/$JARFILE" + LIBPATH="/usr/local/lib:/usr/lib" +fi + +java -cp "$CLASSPATH:." -Djava.library.path="$LIBPATH" Hello + +cd - -- 2.34.1