Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d38b73046c9cc3bb1cc1416a0eded39b871268a1 (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
144
145
146
/***************************************************************************
 * Copyright (c) 2004-2007 Eike Stepper, Germany.
 * 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.IOUtil;
import org.eclipse.net4j.util.om.OMTraceHandler;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.UUID;

/**
 * @author Eike Stepper
 */
public class RemoteTraceHandler implements OMTraceHandler
{
  public static final String DEFAULT_HOST = "localhost"; //$NON-NLS-1$

  public static final int DEFAULT_PORT = RemoteTraceServer.DEFAULT_PORT;

  private static int uniqueCounter;

  private String agentID;

  private String host;

  private int port;

  private Socket socket;

  public RemoteTraceHandler() throws IOException
  {
    this(uniqueAgentID());
  }

  public RemoteTraceHandler(String agentID) throws IOException
  {
    this(agentID, DEFAULT_HOST);
  }

  public RemoteTraceHandler(String agentID, String host) throws IOException
  {
    this(agentID, host, DEFAULT_PORT);
  }

  public RemoteTraceHandler(String agentID, String host, int port) throws IOException
  {
    this.agentID = agentID;
    this.host = host;
    this.port = port;
    socket = connect();
  }

  public Exception close()
  {
    try
    {
      socket.close();
      return null;
    }
    catch (IOException ex)
    {
      return ex;
    }
  }

  public void traced(Event event)
  {
    try
    {
      OutputStream outputStream = socket.getOutputStream();
      DataOutputStream out = new DataOutputStream(outputStream);

      out.writeLong(event.getTimeStamp());
      writeUTF(out, agentID);
      writeUTF(out, event.getTracer().getBundle().getBundleID());
      writeUTF(out, event.getTracer().getFullName());
      writeUTF(out, event.getContext() == null ? "" : event.getContext().getName());
      writeUTF(out, event.getMessage());
      if (event.getThrowable() == null)
      {
        out.writeBoolean(false);
      }
      else
      {
        out.writeBoolean(true);
        String message = event.getThrowable().getMessage();
        writeUTF(out, message);

        StackTraceElement[] stackTrace = event.getThrowable().getStackTrace();
        int size = stackTrace == null ? 0 : stackTrace.length;
        out.writeInt(size);

        for (int i = 0; i < size; i++)
        {
          StackTraceElement element = stackTrace[i];
          writeUTF(out, element.getClassName());
          writeUTF(out, element.getMethodName());
          writeUTF(out, element.getFileName());
          out.writeInt(element.getLineNumber());
        }
      }

      out.flush();
    }
    catch (IOException ex)
    {
      IOUtil.print(ex);
    }
  }

  protected Socket connect() throws IOException
  {
    return new Socket(host, port);
  }

  protected void writeUTF(DataOutputStream out, String str) throws IOException
  {
    out.writeUTF(str == null ? "" : str);
  }

  public static String uniqueAgentID()
  {
    try
    {
      InetAddress localMachine = InetAddress.getLocalHost();
      return localMachine.getHostName() + "#" + (++uniqueCounter);
    }
    catch (Exception ex)
    {
      UUID uuid = UUID.randomUUID();
      return uuid.toString();
    }
  }
}

Back to the top