Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event; | |
14 | ||
15 | import java.util.HashMap; | |
16 | import java.util.List; | |
17 | ||
18 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
19 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; | |
20 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
21 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; | |
22 | ||
23 | /** | |
24 | * <b><u>EventDefinition</u></b> | |
25 | * <p> | |
26 | * Represents an instance of an event. | |
27 | */ | |
28 | public class EventDefinition implements IDefinitionScope { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | /** | |
35 | * The corresponding event declaration. | |
36 | */ | |
aa572e22 | 37 | private final EventDeclaration declaration; |
866e5b51 FC |
38 | |
39 | /** | |
40 | * The timestamp of the current event. | |
41 | */ | |
aa572e22 | 42 | private long timestamp; |
866e5b51 FC |
43 | |
44 | /** | |
45 | * The event context structure definition. | |
46 | */ | |
aa572e22 | 47 | private StructDefinition context; |
866e5b51 FC |
48 | |
49 | /** | |
50 | * The event fields structure definition. | |
51 | */ | |
aa572e22 | 52 | private StructDefinition fields; |
866e5b51 FC |
53 | |
54 | /** | |
55 | * The StreamInputReader that reads this event definition. | |
56 | */ | |
07002e0a | 57 | private final StreamInputReader streamInputReader; |
866e5b51 FC |
58 | |
59 | // ------------------------------------------------------------------------ | |
60 | // Constructors | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | /** | |
64 | * Constructs an event definition. | |
65 | * | |
66 | * @param declaration | |
67 | * The corresponding event declaration. | |
68 | */ | |
69 | public EventDefinition(EventDeclaration declaration, | |
70 | StreamInputReader streamInputReader) { | |
71 | this.declaration = declaration; | |
72 | this.streamInputReader = streamInputReader; | |
73 | } | |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Getters/Setters/Predicates | |
77 | // ------------------------------------------------------------------------ | |
78 | ||
79 | @Override | |
80 | public String getPath() { | |
81 | return "event"; //$NON-NLS-1$ | |
82 | } | |
83 | ||
9ac2eb62 MK |
84 | /** |
85 | * Gets the declaration (the form) of the data | |
86 | * | |
87 | * @return the event declaration | |
88 | */ | |
866e5b51 FC |
89 | public EventDeclaration getDeclaration() { |
90 | return declaration; | |
91 | } | |
92 | ||
9ac2eb62 MK |
93 | /** |
94 | * Gets the fields of a definition | |
95 | * | |
96 | * @return the fields of a definition in struct form. Can be null. | |
97 | */ | |
866e5b51 FC |
98 | public StructDefinition getFields() { |
99 | return fields; | |
100 | } | |
101 | ||
9ac2eb62 MK |
102 | /** |
103 | * Gets the context of this event | |
104 | * | |
105 | * @return the context in struct form | |
106 | */ | |
866e5b51 FC |
107 | public StructDefinition getContext() { |
108 | return context; | |
109 | } | |
110 | ||
9ac2eb62 MK |
111 | /** |
112 | * Gets the stream input reader that this event was made by | |
113 | * | |
114 | * @return the parent | |
115 | */ | |
866e5b51 FC |
116 | public StreamInputReader getStreamInputReader() { |
117 | return streamInputReader; | |
118 | } | |
119 | ||
9ac2eb62 MK |
120 | /** |
121 | * Gets the context of packet the event is in. | |
122 | * | |
123 | * @return the packet context | |
124 | */ | |
866e5b51 FC |
125 | public StructDefinition getPacketContext() { |
126 | return streamInputReader.getCurrentPacketContext(); | |
127 | } | |
128 | ||
9ac2eb62 MK |
129 | /** |
130 | * gets the CPU the event was generated by. Slightly LTTng specific | |
131 | * | |
132 | * @return The CPU the event was generated by | |
133 | */ | |
866e5b51 FC |
134 | public int getCPU() { |
135 | return streamInputReader.getCPU(); | |
136 | } | |
137 | ||
aa572e22 MK |
138 | /** |
139 | * @return the timestamp | |
140 | */ | |
141 | public long getTimestamp() { | |
142 | return timestamp; | |
143 | } | |
144 | ||
145 | /** | |
9ac2eb62 MK |
146 | * @param timestamp |
147 | * the timestamp to set | |
aa572e22 MK |
148 | */ |
149 | public void setTimestamp(long timestamp) { | |
150 | this.timestamp = timestamp; | |
151 | } | |
152 | ||
153 | /** | |
9ac2eb62 MK |
154 | * @param context |
155 | * the context to set | |
aa572e22 MK |
156 | */ |
157 | public void setContext(StructDefinition context) { | |
158 | this.context = context; | |
159 | } | |
160 | ||
161 | /** | |
9ac2eb62 MK |
162 | * @param fields |
163 | * the fields to set | |
aa572e22 MK |
164 | */ |
165 | public void setFields(StructDefinition fields) { | |
166 | this.fields = fields; | |
167 | } | |
168 | ||
866e5b51 FC |
169 | // ------------------------------------------------------------------------ |
170 | // Operations | |
171 | // ------------------------------------------------------------------------ | |
172 | ||
173 | @Override | |
174 | public Definition lookupDefinition(String lookupPath) { | |
175 | if (lookupPath.equals("context")) { //$NON-NLS-1$ | |
176 | return context; | |
177 | } else if (lookupPath.equals("fields")) { //$NON-NLS-1$ | |
178 | return fields; | |
179 | } else { | |
180 | return null; | |
181 | } | |
182 | } | |
183 | ||
184 | @Override | |
185 | public String toString() { | |
07002e0a | 186 | HashMap<String, Definition> definitions; |
866e5b51 | 187 | List<String> list; |
07002e0a MK |
188 | StringBuilder retString = new StringBuilder(); |
189 | final String cr = System.getProperty("line.separator");//$NON-NLS-1$ | |
866e5b51 | 190 | |
07002e0a MK |
191 | retString.append("Event type: " + declaration.getName() + cr); //$NON-NLS-1$ |
192 | retString.append("Timestamp: " + Long.toString(timestamp) + cr); //$NON-NLS-1$ | |
866e5b51 FC |
193 | |
194 | if (context != null) { | |
07002e0a | 195 | definitions = context.getDefinitions(); |
866e5b51 FC |
196 | list = context.getDeclaration().getFieldsList(); |
197 | ||
198 | for (String field : list) { | |
9ac2eb62 MK |
199 | retString.append(field |
200 | + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$ | |
866e5b51 FC |
201 | } |
202 | } | |
203 | ||
204 | if (fields != null) { | |
07002e0a | 205 | definitions = fields.getDefinitions(); |
866e5b51 FC |
206 | list = fields.getDeclaration().getFieldsList(); |
207 | ||
208 | for (String field : list) { | |
9ac2eb62 MK |
209 | retString.append(field |
210 | + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$ | |
866e5b51 FC |
211 | } |
212 | } | |
213 | ||
07002e0a | 214 | return retString.toString(); |
866e5b51 FC |
215 | } |
216 | ||
217 | } |