File: ApplicationContextExample.java

package info (click to toggle)
ust 2.9.0-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,812 kB
  • sloc: ansic: 28,200; sh: 12,314; java: 1,776; makefile: 1,155; python: 687; cpp: 177
file content (112 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2016 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
 *
 * 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.Handler;
import java.util.logging.Logger;

import org.lttng.ust.agent.context.ContextInfoManager;
import org.lttng.ust.agent.context.IContextInfoRetriever;
import org.lttng.ust.agent.jul.LttngLogHandler;

/**
 * Example program defining a application context retriever, which allows
 * attaching application-defined contexts to trace events.
 *
 * FIXME Use custom context names, and several names/types
 *
 * <p>
 * Usage:
 * <ul>
 * <li>$ lttng create</li>
 * <li>$ lttng enable-event -j -a</li>
 * <li>$ lttng add-context -j -t '$app.myprovider:mystringcontext'</li>
 * <li>$ lttng add-context -j -t '$app.myprovider:myshortcontext'</li>
 * <li>$ lttng start</li>
 * <li>(run this program)</li>
 * <li>$ lttng stop</li>
 * <li>$ lttng view</li>
 * <li>$ lttng destroy</li>
 * </ul>
 * </p>
 *
 * The events present in the resulting trace should carry the context
 * information defined in the example retriever.
 *
 * @author Alexandre Montplaisir
 */
public class ApplicationContextExample {

	/** Class-wide JUL logger object */
	private static final Logger LOGGER = Logger.getLogger(ApplicationContextExample.class.getName());

	private static final String RETRIEVER_NAME = "myprovider";
	private static final String CONTEXT_NAME_STRING = "mystringcontext";
	private static final String CONTEXT_NAME_SHORT = "myshortcontext";

	private static class ExampleContextInfoRetriever implements IContextInfoRetriever {

		@Override
		public Object retrieveContextInfo(String key) {
			if (CONTEXT_NAME_SHORT.equals(key)) {
				return (short) 42;
			} else if (CONTEXT_NAME_STRING.equals(key)) {
				return "context-value!";
			} else {
				return null;
			}
		}

	}

	/**
	 * Application start
	 *
	 * @param args
	 *            Command-line arguments
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void main(String args[]) throws IOException, InterruptedException {
		/* Instantiate and attach a logger object */
		Handler lttngHandler = new LttngLogHandler();
		LOGGER.addHandler(lttngHandler);

		/* Instantiate and register the context retriever */
		IContextInfoRetriever cir = new ExampleContextInfoRetriever();
		ContextInfoManager.getInstance().registerContextInfoRetriever(RETRIEVER_NAME, cir);

		/*
		 * Make sure you have a LTTng session running with the appropriate
		 * events and contexts enabled! See the class Javadoc.
		 */

		/* Trigger a tracing event using the JUL Logger created before. */
		LOGGER.info("Log event #1");
		LOGGER.warning("Log event #2");
		LOGGER.severe("Log event #3");

		/* Unregister our context retriever, and dispose the log handler */
		ContextInfoManager.getInstance().unregisterContextInfoRetriever(RETRIEVER_NAME);
		lttngHandler.close();
	}
}