Commit | Line | Data |
---|---|---|
eadf9801 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
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 | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.tests.stubs.trace.text; | |
14 | ||
15 | import java.text.ParseException; | |
16 | import java.text.SimpleDateFormat; | |
17 | import java.util.Calendar; | |
18 | import java.util.Date; | |
19 | import java.util.GregorianCalendar; | |
20 | import java.util.regex.Matcher; | |
21 | import java.util.regex.Pattern; | |
22 | ||
23 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; | |
24 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
25 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat; | |
26 | import org.eclipse.linuxtools.tmf.core.trace.text.TextTrace; | |
27 | import org.eclipse.linuxtools.tmf.core.trace.text.TextTraceEventContent; | |
28 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.text.SyslogEventType.Index; | |
29 | ||
30 | /** | |
31 | * Extension of TmfTrace for handling of system logs. | |
32 | */ | |
33 | public class SyslogTrace extends TextTrace<SyslogEvent> { | |
34 | ||
35 | /** The cache size for system log traces. */ | |
36 | private static final int CACHE_SIZE = 100; | |
37 | /** The time stamp format of the trace type. */ | |
38 | public static final String TIMESTAMP_FORMAT = "MMM dd HH:mm:ss"; //$NON-NLS-1$ | |
39 | /** The corresponding date format of the time stamp. */ | |
40 | public static final SimpleDateFormat TIMESTAMP_SIMPLEDATEFORMAT = new SimpleDateFormat(TIMESTAMP_FORMAT); | |
41 | /** The scale of the time stamps. */ | |
42 | public static final byte TIMESTAMP_SCALE = ITmfTimestamp.MILLISECOND_SCALE; | |
43 | /** The regular expression pattern of the first line of an event. */ | |
44 | public static final Pattern LINE1_PATTERN = Pattern.compile( | |
45 | "\\s*(\\S\\S\\S \\d\\d? \\d\\d:\\d\\d:\\d\\d)\\s*(\\S*)\\s*(\\S*):+\\s*(.*\\S)?"); //$NON-NLS-1$ | |
46 | ||
47 | /* The current calendar to use */ | |
48 | private static final Calendar CURRENT = Calendar.getInstance(); | |
49 | ||
50 | /** | |
51 | * Constructor | |
52 | */ | |
53 | public SyslogTrace() { | |
54 | setCacheSize(CACHE_SIZE); | |
55 | } | |
56 | ||
57 | @Override | |
58 | protected Pattern getFirstLinePattern() { | |
59 | return LINE1_PATTERN; | |
60 | } | |
61 | ||
62 | @Override | |
63 | protected SyslogEvent parseFirstLine(Matcher matcher, String line) { | |
64 | ||
65 | ITmfTimestamp timestamp = null; | |
66 | ||
67 | try { | |
68 | synchronized (TIMESTAMP_SIMPLEDATEFORMAT) { | |
69 | TIMESTAMP_SIMPLEDATEFORMAT.setTimeZone(TmfTimestampFormat.getDefaulTimeFormat().getTimeZone()); | |
70 | Date date = TIMESTAMP_SIMPLEDATEFORMAT.parse(matcher.group(1)); | |
71 | GregorianCalendar calendar = new GregorianCalendar(); | |
72 | calendar.setTime(date); | |
73 | calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR)); | |
74 | if (calendar.after(CURRENT)) { | |
75 | calendar.set(Calendar.YEAR, CURRENT.get(Calendar.YEAR) - 1); | |
76 | } | |
77 | long ms = calendar.getTimeInMillis(); | |
78 | timestamp = new TmfTimestamp(ms, TIMESTAMP_SCALE); | |
79 | } | |
80 | } catch (ParseException e) { | |
81 | timestamp = new TmfTimestamp(); | |
82 | } | |
83 | ||
84 | TextTraceEventContent content = new TextTraceEventContent(SyslogEventType.LABELS); | |
85 | content.setValue(new StringBuffer(line)); | |
86 | content.setFieldValue(Index.TIMESTAMP, matcher.group(1)); | |
87 | content.setFieldValue(Index.HOST, matcher.group(2)); | |
88 | content.setFieldValue(Index.LOGGER, matcher.group(3)); | |
89 | content.setFieldValue(Index.MESSAGE, new StringBuffer(matcher.group(4) != null ? matcher.group(4) : "")); //$NON-NLS-1$ | |
90 | ||
91 | SyslogEvent event = new SyslogEvent( | |
92 | this, | |
93 | timestamp, | |
94 | "", //$NON-NLS-1$ | |
95 | SyslogEventType.INSTANCE, | |
96 | content, | |
97 | ""); //$NON-NLS-1$ | |
98 | ||
99 | return event; | |
100 | } | |
101 | ||
102 | @Override | |
103 | protected void parseNextLine(SyslogEvent event, String line) { | |
104 | TextTraceEventContent content = (TextTraceEventContent) event.getContent(); | |
105 | ((StringBuffer) content.getValue()).append("\n").append(line); //$NON-NLS-1$ | |
106 | if (line.trim().length() > 0) { | |
107 | ((StringBuffer) content.getFieldValue(Index.MESSAGE)).append(SEPARATOR + line.trim()); | |
108 | } | |
109 | } | |
110 | ||
111 | @Override | |
112 | public ITmfTimestamp getInitialRangeOffset() { | |
113 | return new TmfTimestamp(60, ITmfTimestamp.SECOND_SCALE); | |
114 | } | |
115 | ||
116 | } |