2 * Copyright (C) 2016 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 import java
.io
.IOException
;
24 import java
.util
.logging
.Handler
;
25 import java
.util
.logging
.Logger
;
27 import org
.lttng
.ust
.agent
.context
.ContextInfoManager
;
28 import org
.lttng
.ust
.agent
.context
.IContextInfoRetriever
;
29 import org
.lttng
.ust
.agent
.jul
.LttngLogHandler
;
32 * Example program defining a application context retriever, which allows
33 * attaching application-defined contexts to trace events.
35 * FIXME Use custom context names, and several names/types
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>
52 * The events present in the resulting trace should carry the context
53 * information defined in the example retriever.
55 * @author Alexandre Montplaisir
57 public class ApplicationContextExample
{
59 /** Class-wide JUL logger object */
60 private static final Logger LOGGER
= Logger
.getLogger(ApplicationContextExample
.class.getName());
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";
66 private static class ExampleContextInfoRetriever
implements IContextInfoRetriever
{
69 public Object
retrieveContextInfo(String key
) {
70 if (CONTEXT_NAME_SHORT
.equals(key
)) {
72 } else if (CONTEXT_NAME_STRING
.equals(key
)) {
73 return "context-value!";
85 * Command-line arguments
87 * @throws InterruptedException
89 public static void main(String args
[]) throws IOException
, InterruptedException
{
90 /* Instantiate and attach a logger object */
91 Handler lttngHandler
= new LttngLogHandler();
92 LOGGER
.addHandler(lttngHandler
);
94 /* Instantiate and register the context retriever */
95 IContextInfoRetriever cir
= new ExampleContextInfoRetriever();
96 ContextInfoManager
.getInstance().registerContextInfoRetriever(RETRIEVER_NAME
, cir
);
99 * Make sure you have a LTTng session running with the appropriate
100 * events and contexts enabled! See the class Javadoc.
103 /* Trigger a tracing event using the JUL Logger created before. */
104 LOGGER
.info("Log event #1");
105 LOGGER
.warning("Log event #2");
106 LOGGER
.severe("Log event #3");
108 /* Unregister our context retriever, and dispose the log handler */
109 ContextInfoManager
.getInstance().unregisterContextInfoRetriever(RETRIEVER_NAME
);
110 lttngHandler
.close();