1 /*******************************************************************************
2 * Copyright (c) 2014 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.text
;
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
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimePreferences
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampFormat
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.text
.TextTrace
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.text
.TextTraceEventContent
;
29 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.text
.SyslogEventType
.Index
;
32 * Extension of TmfTrace for handling of system logs.
34 public class SyslogTrace
extends TextTrace
<SyslogEvent
> {
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. */
41 public static final SimpleDateFormat TIMESTAMP_SIMPLEDATEFORMAT
= new SimpleDateFormat(
42 TIMESTAMP_FORMAT
, TmfTimePreferences
.getInstance().getLocale());
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$
47 /* The current calendar to use */
48 private static final Calendar CURRENT
= Calendar
.getInstance();
53 public SyslogTrace() {
54 setCacheSize(CACHE_SIZE
);
58 protected Pattern
getFirstLinePattern() {
63 protected SyslogEvent
parseFirstLine(Matcher matcher
, String line
) {
65 ITmfTimestamp timestamp
= null;
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);
77 long ns
= calendar
.getTimeInMillis() * 1000000;
78 timestamp
= createTimestamp(ns
);
80 } catch (ParseException e
) {
81 timestamp
= new TmfTimestamp();
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$
91 SyslogEvent event
= new SyslogEvent(
95 SyslogEventType
.INSTANCE
,
103 protected void parseNextLine(SyslogEvent event
, String line
) {
104 TextTraceEventContent content
= 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());
112 public ITmfTimestamp
getInitialRangeOffset() {
113 return new TmfTimestamp(60, ITmfTimestamp
.SECOND_SCALE
);