Fix: add missing example file to dist tarball
[lttng-ust.git] / doc / examples / java-jul / ApplicationContextExample.java
CommitLineData
8ab5c06b
AM
1/*
2 * Copyright (C) 2016 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22
23import java.io.IOException;
24import java.util.logging.Handler;
25import java.util.logging.Logger;
26
27import org.lttng.ust.agent.context.ContextInfoManager;
28import org.lttng.ust.agent.context.IContextInfoRetriever;
29import org.lttng.ust.agent.jul.LttngLogHandler;
30
31/**
32 * Example program defining a application context retriever, which allows
33 * attaching application-defined contexts to trace events.
34 *
35 * FIXME Use custom context names, and several names/types
36 *
37 * <p>
38 * Usage:
39 * <ul>
40 * <li>$ lttng create</li>
41 * <li>$ lttng enable-event -j -a</li>
42 * <li>$ lttng add-context -j -t '$app.myprovider:mystringcontext'</li>
43 * <li>$ lttng add-context -j -t '$app.myprovider:myshortcontext'</li>
44 * <li>$ lttng start</li>
45 * <li>(run this program)</li>
46 * <li>$ lttng stop</li>
47 * <li>$ lttng view</li>
48 * <li>$ lttng destroy</li>
49 * </ul>
50 * </p>
51 *
52 * The events present in the resulting trace should carry the context
53 * information defined in the example retriever.
54 *
55 * @author Alexandre Montplaisir
56 */
57public class ApplicationContextExample {
58
59 /** Class-wide JUL logger object */
60 private static final Logger LOGGER = Logger.getLogger(ApplicationContextExample.class.getName());
61
62 private static final String RETRIEVER_NAME = "myprovider";
63 private static final String CONTEXT_NAME_STRING = "mystringcontext";
64 private static final String CONTEXT_NAME_SHORT = "myshortcontext";
65
66 private static class ExampleContextInfoRetriever implements IContextInfoRetriever {
67
68 @Override
69 public Object retrieveContextInfo(String key) {
70 switch (key) {
71 case CONTEXT_NAME_SHORT:
72 return (short) 42;
73 case CONTEXT_NAME_STRING:
74 return "context-value!";
75 default:
76 return null;
77 }
78 }
79
80 }
81
82 /**
83 * Application start
84 *
85 * @param args
86 * Command-line arguments
87 * @throws IOException
88 * @throws InterruptedException
89 */
90 public static void main(String args[]) throws IOException, InterruptedException {
91 /* Instantiate and attach a logger object */
92 Handler lttngHandler = new LttngLogHandler();
93 LOGGER.addHandler(lttngHandler);
94
95 /* Instantiate and register the context retriever */
96 IContextInfoRetriever cir = new ExampleContextInfoRetriever();
97 ContextInfoManager.getInstance().registerContextInfoRetriever(RETRIEVER_NAME, cir);
98
99 /*
100 * Make sure you have a LTTng session running with the appropriate
101 * events and contexts enabled! See the class Javadoc.
102 */
103
104 /* Trigger a tracing event using the JUL Logger created before. */
105 LOGGER.info("Log event #1");
106 LOGGER.warning("Log event #2");
107 LOGGER.severe("Log event #3");
108
109 /* Unregister our context retriever, and dispose the log handler */
110 ContextInfoManager.getInstance().unregisterContextInfoRetriever(RETRIEVER_NAME);
111 lttngHandler.close();
112 }
113}
This page took 0.02719 seconds and 4 git commands to generate.