1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 * Patrick Tasse - Move field declarations to trace
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.text
;
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
;
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
;
35 import com
.google
.common
.collect
.ImmutableList
;
38 * Extension of TmfTrace for handling of system logs.
40 public class SyslogTrace
extends TextTrace
<SyslogEvent
> {
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$
53 /* The current calendar to use */
54 private static final Calendar CURRENT
= Calendar
.getInstance();
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";
66 /** The event aspects */
67 public static final @NonNull Collection
<ITmfEventAspect
<?
>> ASPECTS
=
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
)
80 public SyslogTrace() {
81 setCacheSize(CACHE_SIZE
);
85 protected Pattern
getFirstLinePattern() {
90 protected SyslogEvent
parseFirstLine(Matcher matcher
, String line
) {
92 ITmfTimestamp timestamp
= null;
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);
104 long ns
= calendar
.getTimeInMillis() * 1000000;
105 timestamp
= createTimestamp(ns
);
107 } catch (ParseException e
) {
108 timestamp
= TmfTimestamp
.create(0, ITmfTimestamp
.SECOND_SCALE
);
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$
119 SyslogEvent event
= new SyslogEvent(
122 SyslogEventType
.INSTANCE
,
123 content
); //$NON-NLS-1$
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());
138 public ITmfTimestamp
getInitialRangeOffset() {
139 return TmfTimestamp
.fromSeconds(60);
143 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {