Commit | Line | Data |
---|---|---|
eadf9801 | 1 | /******************************************************************************* |
5b12450f | 2 | * Copyright (c) 2014, 2015 Ericsson |
eadf9801 BH |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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: | |
10 | * Bernd Hufmann - Initial API and implementation | |
5b12450f | 11 | * Patrick Tasse - Move field declarations to trace |
eadf9801 BH |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.tests.stubs.trace.text; |
eadf9801 BH |
15 | |
16 | import java.text.ParseException; | |
17 | import java.text.SimpleDateFormat; | |
18 | import java.util.Calendar; | |
5b12450f | 19 | import java.util.Collection; |
eadf9801 BH |
20 | import java.util.Date; |
21 | import java.util.GregorianCalendar; | |
22 | import java.util.regex.Matcher; | |
23 | import java.util.regex.Pattern; | |
24 | ||
5b12450f PT |
25 | import org.eclipse.jdt.annotation.NonNull; |
26 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; | |
27 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfContentFieldAspect; | |
2bdf0193 AM |
28 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
29 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimePreferences; | |
30 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
31 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; | |
32 | import org.eclipse.tracecompass.tmf.core.trace.text.TextTrace; | |
33 | import org.eclipse.tracecompass.tmf.core.trace.text.TextTraceEventContent; | |
5b12450f PT |
34 | |
35 | import com.google.common.collect.ImmutableList; | |
eadf9801 BH |
36 | |
37 | /** | |
38 | * Extension of TmfTrace for handling of system logs. | |
39 | */ | |
40 | public class SyslogTrace extends TextTrace<SyslogEvent> { | |
41 | ||
42 | /** The cache size for system log traces. */ | |
43 | private static final int CACHE_SIZE = 100; | |
44 | /** The time stamp format of the trace type. */ | |
45 | public static final String TIMESTAMP_FORMAT = "MMM dd HH:mm:ss"; //$NON-NLS-1$ | |
46 | /** The corresponding date format of the time stamp. */ | |
8765b2d4 | 47 | public static final SimpleDateFormat TIMESTAMP_SIMPLEDATEFORMAT = new SimpleDateFormat( |
7c34a4ea | 48 | TIMESTAMP_FORMAT, TmfTimePreferences.getLocale()); |
eadf9801 BH |
49 | /** The regular expression pattern of the first line of an event. */ |
50 | public static final Pattern LINE1_PATTERN = Pattern.compile( | |
32528869 | 51 | "\\s*(\\S\\S\\S \\d\\d? \\d\\d:\\d\\d:\\d\\d)\\s*(\\S*)\\s*(\\S*):+\\s*(\\S*):([0-9]*)\\s*(.*\\S)?"); //$NON-NLS-1$ |
eadf9801 BH |
52 | |
53 | /* The current calendar to use */ | |
54 | private static final Calendar CURRENT = Calendar.getInstance(); | |
55 | ||
5b12450f PT |
56 | /** The event fields */ |
57 | @SuppressWarnings({"javadoc", "nls"}) | |
58 | public interface Field { | |
59 | @NonNull String HOST = "Host"; | |
60 | @NonNull String LOGGER = "Logger"; | |
61 | @NonNull String FILE = "File"; | |
62 | @NonNull String LINE = "Line"; | |
63 | @NonNull String MESSAGE = "Message"; | |
64 | } | |
65 | ||
66 | /** The event aspects */ | |
67 | public static final @NonNull Collection<ITmfEventAspect> ASPECTS = | |
0e4f957e | 68 | ImmutableList.of( |
5b12450f PT |
69 | ITmfEventAspect.BaseAspects.TIMESTAMP, |
70 | new TmfContentFieldAspect(Field.HOST, Field.HOST), | |
71 | new TmfContentFieldAspect(Field.LOGGER, Field.LOGGER), | |
72 | new TmfContentFieldAspect(Field.FILE, Field.FILE), | |
73 | new TmfContentFieldAspect(Field.LINE, Field.LINE), | |
74 | new TmfContentFieldAspect(Field.MESSAGE, Field.MESSAGE) | |
0e4f957e | 75 | ); |
5b12450f | 76 | |
eadf9801 BH |
77 | /** |
78 | * Constructor | |
79 | */ | |
80 | public SyslogTrace() { | |
81 | setCacheSize(CACHE_SIZE); | |
82 | } | |
83 | ||
84 | @Override | |
85 | protected Pattern getFirstLinePattern() { | |
86 | return LINE1_PATTERN; | |
87 | } | |
88 | ||
89 | @Override | |
90 | protected SyslogEvent parseFirstLine(Matcher matcher, String line) { | |
91 | ||
92 | ITmfTimestamp timestamp = null; | |
93 | ||
94 | try { | |
95 | synchronized (TIMESTAMP_SIMPLEDATEFORMAT) { | |
96 | TIMESTAMP_SIMPLEDATEFORMAT.setTimeZone(TmfTimestampFormat.getDefaulTimeFormat().getTimeZone()); | |
97 | Date date = TIMESTAMP_SIMPLEDATEFORMAT.parse(matcher.group(1)); | |
98 | GregorianCalendar calendar = new GregorianCalendar(); | |
99 | calendar.setTime(date); | |
100 | calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR)); | |
101 | if (calendar.after(CURRENT)) { | |
102 | calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR) - 1); | |
103 | } | |
6b44794a MK |
104 | long ns = calendar.getTimeInMillis() * 1000000; |
105 | timestamp = createTimestamp(ns); | |
eadf9801 BH |
106 | } |
107 | } catch (ParseException e) { | |
108 | timestamp = new TmfTimestamp(); | |
109 | } | |
110 | ||
5b12450f | 111 | TextTraceEventContent content = new TextTraceEventContent(5); |
eadf9801 | 112 | content.setValue(new StringBuffer(line)); |
5b12450f PT |
113 | content.addField(Field.HOST, matcher.group(2)); |
114 | content.addField(Field.LOGGER, matcher.group(3)); | |
115 | content.addField(Field.FILE, matcher.group(4)); | |
116 | content.addField(Field.LINE, matcher.group(5)); | |
117 | content.addField(Field.MESSAGE, new StringBuffer(matcher.group(6) != null ? matcher.group(6) : "")); //$NON-NLS-1$ | |
eadf9801 BH |
118 | |
119 | SyslogEvent event = new SyslogEvent( | |
120 | this, | |
121 | timestamp, | |
eadf9801 | 122 | SyslogEventType.INSTANCE, |
e1de2fd4 | 123 | content); //$NON-NLS-1$ |
eadf9801 BH |
124 | |
125 | return event; | |
126 | } | |
127 | ||
128 | @Override | |
129 | protected void parseNextLine(SyslogEvent event, String line) { | |
5ece050b | 130 | TextTraceEventContent content = event.getContent(); |
eadf9801 BH |
131 | ((StringBuffer) content.getValue()).append("\n").append(line); //$NON-NLS-1$ |
132 | if (line.trim().length() > 0) { | |
5b12450f | 133 | ((StringBuffer) content.getFieldValue(Field.MESSAGE)).append(SEPARATOR + line.trim()); |
eadf9801 BH |
134 | } |
135 | } | |
136 | ||
137 | @Override | |
138 | public ITmfTimestamp getInitialRangeOffset() { | |
139 | return new TmfTimestamp(60, ITmfTimestamp.SECOND_SCALE); | |
140 | } | |
141 | ||
5b12450f PT |
142 | @Override |
143 | public Iterable<ITmfEventAspect> getEventAspects() { | |
144 | return ASPECTS; | |
145 | } | |
eadf9801 | 146 | } |