Commit | Line | Data |
---|---|---|
849202d4 DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@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 | ||
849202d4 DG |
23 | import java.util.logging.Logger; |
24 | ||
25 | /* | |
26 | * That's the import you need being the path in the liblttng-ust-jul Jar file. | |
27 | */ | |
501f6777 | 28 | import org.lttng.ust.agent.LTTngAgent; |
849202d4 | 29 | |
5bfeaeca AM |
30 | /** |
31 | * Example application using the LTTng-UST Java JUL agent. | |
32 | * | |
33 | * @author David Goulet | |
34 | */ | |
35 | public class Hello { | |
36 | ||
849202d4 DG |
37 | /* Of course :) */ |
38 | private static final int answer = 42; | |
39 | ||
40 | /* | |
41 | * Static reference to the LTTngAgent. Used to dispose of it at the end | |
42 | * which is recommended but not mandatory to do. | |
43 | */ | |
44 | private static LTTngAgent lttngAgent; | |
45 | ||
5bfeaeca AM |
46 | /** |
47 | * Application start | |
48 | * | |
49 | * @param args | |
50 | * Command-line arguments | |
51 | * @throws Exception | |
52 | */ | |
53 | public static void main(String args[]) throws Exception { | |
849202d4 DG |
54 | /* |
55 | * For this example, a custom "hello" logger is created. Note that JUL | |
56 | * has a default "global" that can also be used. | |
57 | */ | |
58 | Logger helloLog = Logger.getLogger("hello"); | |
59 | ||
60 | /* | |
8685da11 | 61 | * Get the LTTngAgent singleton reference. This will also initialize |
849202d4 DG |
62 | * the Agent and make it register to the session daemon if available. |
63 | * When this returns, the Agent is registered and fully ready. If no | |
64 | * session daemon is found, it will return and retry every 3 seconds in | |
65 | * the background. TCP is used for communication. | |
66 | * | |
8685da11 | 67 | * Note that the LTTngAgent once registered is a separate thread in |
849202d4 DG |
68 | * your Java application. |
69 | */ | |
70 | lttngAgent = LTTngAgent.getLTTngAgent(); | |
71 | ||
72 | /* | |
73 | * Gives you time to do some lttng commands before any event is hit. | |
74 | */ | |
75 | Thread.sleep(5000); | |
76 | ||
77 | /* Trigger a tracing event using the JUL Logger created before. */ | |
78 | helloLog.info("Hello World, the answer is " + answer); | |
79 | ||
80 | /* | |
81 | * From this point on, the above message will be collected in the trace | |
82 | * if the event "hello" is enabled for the JUL domain using the lttng | |
83 | * command line or the lttng-ctl API. For instance: | |
84 | * | |
85 | * $ lttng enable-event -j hello | |
86 | * | |
87 | * A new logger is created here and fired after. The Agent has an | |
88 | * internal timer that is fired every 5 seconds in order to enable | |
89 | * events that were not found at first but might need to be enabled | |
90 | * when new Logger appears. Unfortunately, there is no way right now to | |
91 | * get notify of that so we have to actively poll. | |
92 | * | |
93 | * Using the --all command for instance, it will make this Logger | |
94 | * available in a LTTng trace after the internal Agent's timer is | |
95 | * fired. (lttng enable-event -a -j). | |
96 | */ | |
97 | Logger helloLogDelayed = Logger.getLogger("hello_delay"); | |
98 | ||
99 | System.out.println("Firing hello delay in 10 seconds..."); | |
100 | Thread.sleep(10000); | |
101 | helloLogDelayed.info("Hello World delayed..."); | |
102 | ||
103 | System.out.println("Cleaning Hello"); | |
104 | ||
105 | /* | |
106 | * Again, this is highly recommended so the session daemon socket gets | |
8685da11 | 107 | * cleaned up explicitly but it is not mandatory to do this step. |
849202d4 DG |
108 | */ |
109 | lttngAgent.dispose(); | |
110 | } | |
111 | } |