58b126aaa1d7419824e6495c13974983a9ab8425
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / stubs / org / eclipse / tracecompass / tmf / tests / stubs / trace / text / SyslogTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 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 * Patrick Tasse - Move field declarations to trace
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.tests.stubs.trace.text;
15
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.Calendar;
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.GregorianCalendar;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
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;
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;
34
35 import com.google.common.collect.ImmutableList;
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. */
47 public static final SimpleDateFormat TIMESTAMP_SIMPLEDATEFORMAT = new SimpleDateFormat(
48 TIMESTAMP_FORMAT, TmfTimePreferences.getLocale());
49 /** The regular expression pattern of the first line of an event. */
50 public static final Pattern LINE1_PATTERN = Pattern.compile(
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$
52
53 /* The current calendar to use */
54 private static final Calendar CURRENT = Calendar.getInstance();
55
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 =
68 ImmutableList.of(
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)
75 );
76
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 }
104 long ns = calendar.getTimeInMillis() * 1000000;
105 timestamp = createTimestamp(ns);
106 }
107 } catch (ParseException e) {
108 timestamp = TmfTimestamp.create(0, ITmfTimestamp.SECOND_SCALE);
109 }
110
111 TextTraceEventContent content = new TextTraceEventContent(5);
112 content.setValue(new StringBuffer(line));
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$
118
119 SyslogEvent event = new SyslogEvent(
120 this,
121 timestamp,
122 SyslogEventType.INSTANCE,
123 content); //$NON-NLS-1$
124
125 return event;
126 }
127
128 @Override
129 protected void parseNextLine(SyslogEvent event, String line) {
130 TextTraceEventContent content = event.getContent();
131 ((StringBuffer) content.getValue()).append("\n").append(line); //$NON-NLS-1$
132 if (line.trim().length() > 0) {
133 ((StringBuffer) content.getFieldValue(Field.MESSAGE)).append(SEPARATOR + line.trim());
134 }
135 }
136
137 @Override
138 public ITmfTimestamp getInitialRangeOffset() {
139 return TmfTimestamp.fromSeconds(60);
140 }
141
142 @Override
143 public Iterable<ITmfEventAspect<?>> getEventAspects() {
144 return ASPECTS;
145 }
146 }
This page took 0.044329 seconds and 4 git commands to generate.