Skip to main content
summaryrefslogtreecommitdiffstats
blob: 443578fbc9056b44497f6cc4831b2de5a7a378a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.om.trace;

import org.eclipse.net4j.util.io.IOUtil;

import java.io.PrintStream;
import java.text.MessageFormat;

/**
 * A {@link OMTraceHandler trace handler} that appends {@link OMTraceHandlerEvent trace events}
 * to a {@link #getStream() print stream}.
 *
 * @author Eike Stepper
 */
public class PrintTraceHandler implements OMTraceHandler
{
  public static final PrintTraceHandler CONSOLE = new PrintTraceHandler();

  private PrintStream stream;

  private String pattern;

  private boolean shortContext;

  public PrintTraceHandler(PrintStream stream)
  {
    this.stream = stream;
  }

  protected PrintTraceHandler()
  {
    this(IOUtil.OUT());
  }

  /**
   * @since 3.2
   */
  public PrintStream getStream()
  {
    return stream;
  }

  public String getPattern()
  {
    return pattern;
  }

  /**
   * Pattern arguments:
   * <p>
   * <ul>
   * <li>{0} --> String <b>tracerName</b>
   * <li>{1} --> String <b>tracerShort</b>
   * <li>{2} --> String <b>contextName</b>
   * <li>{3} --> String <b>contextShort</b>
   * <li>{4} --> long <b>timeStamp</b>
   * <li>{5} --> String <b>message</b>
   * <li>{6} --> String <b>threadName</b>
   * <li>{7} --> long <b>threadID</b>
   * <li>{8} --> int <b>threadPriority</b>
   * <li>{9} --> Thread.State <b>threadState</b>
   * </ul>
   */
  public void setPattern(String pattern)
  {
    this.pattern = pattern;
  }

  public boolean isShortContext()
  {
    return shortContext;
  }

  public void setShortContext(boolean shortContext)
  {
    this.shortContext = shortContext;
  }

  public void traced(OMTraceHandlerEvent event)
  {
    String line = pattern == null ? format(shortContext, event) : format(pattern, event);
    stream.println(line);
    if (event.getThrowable() != null)
    {
      IOUtil.print(event.getThrowable(), stream);
    }
  }

  public static String format(boolean shortContext, OMTraceHandlerEvent event)
  {
    Class<?> context = event.getContext();
    String contextName = shortContext ? context.getSimpleName() : context.getName();
    return Thread.currentThread().getName() + " [" + contextName + "] " + event.getMessage(); //$NON-NLS-1$ //$NON-NLS-2$
  }

  /**
   * Pattern arguments:
   * <p>
   * <ul>
   * <li>{0} --> String <b>tracerName</b>
   * <li>{1} --> String <b>tracerShort</b>
   * <li>{2} --> String <b>contextName</b>
   * <li>{3} --> String <b>contextShort</b>
   * <li>{4} --> long <b>timeStamp</b>
   * <li>{5} --> String <b>message</b>
   * <li>{6} --> String <b>threadName</b>
   * <li>{7} --> long <b>threadID</b>
   * <li>{8} --> int <b>threadPriority</b>
   * <li>{9} --> Thread.State <b>threadState</b>
   * </ul>
   */
  public static String format(String pattern, OMTraceHandlerEvent event)
  {
    final OMTracer tracer = event.getTracer();
    final String tracerName = tracer.getFullName();
    final String tracerShort = tracer.getName();

    final Class<?> context = event.getContext();
    final String contextName = context.getName();
    final String contextShort = context.getName();

    final long timeStamp = event.getTimeStamp();
    final String message = event.getMessage();

    final Thread thread = Thread.currentThread();
    final String threadName = thread.getName();
    final long threadID = thread.getId();
    final int threadPriority = thread.getPriority();
    final Thread.State threadState = thread.getState();

    return MessageFormat.format(pattern, tracerName, tracerShort, contextName, contextShort, timeStamp, message,
        threadName, threadID, threadPriority, threadState);
  }
}

Back to the top