ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / trace / text / SyslogTrace.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.tests.stubs.trace.text;
14
15import java.text.ParseException;
16import java.text.SimpleDateFormat;
17import java.util.Calendar;
18import java.util.Date;
19import java.util.GregorianCalendar;
20import java.util.regex.Matcher;
21import java.util.regex.Pattern;
22
23import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
8765b2d4 24import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimePreferences;
eadf9801
BH
25import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
26import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
27import org.eclipse.linuxtools.tmf.core.trace.text.TextTrace;
28import org.eclipse.linuxtools.tmf.core.trace.text.TextTraceEventContent;
29import org.eclipse.linuxtools.tmf.tests.stubs.trace.text.SyslogEventType.Index;
30
31/**
32 * Extension of TmfTrace for handling of system logs.
33 */
34public class SyslogTrace extends TextTrace<SyslogEvent> {
35
36 /** The cache size for system log traces. */
37 private static final int CACHE_SIZE = 100;
38 /** The time stamp format of the trace type. */
39 public static final String TIMESTAMP_FORMAT = "MMM dd HH:mm:ss"; //$NON-NLS-1$
40 /** The corresponding date format of the time stamp. */
8765b2d4
PT
41 public static final SimpleDateFormat TIMESTAMP_SIMPLEDATEFORMAT = new SimpleDateFormat(
42 TIMESTAMP_FORMAT, TmfTimePreferences.getInstance().getLocale());
eadf9801
BH
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 }
6b44794a
MK
77 long ns = calendar.getTimeInMillis() * 1000000;
78 timestamp = createTimestamp(ns);
eadf9801
BH
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) {
5ece050b 104 TextTraceEventContent content = event.getContent();
eadf9801
BH
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}
This page took 0.037846 seconds and 5 git commands to generate.