From 9f10faedc8e9cd3b6f60ee744dbded9d28bae430 Mon Sep 17 00:00:00 2001 From: Michael Scharf Date: Wed, 14 Nov 2007 06:15:07 +0000 Subject: [PATCH] bug 209665: Need ability to log byte streams from Terminal for debugging https://bugs.eclipse.org/bugs/show_bug.cgi?id=209665 --- .../terminal/telnet/TelnetConnection.java | 4 -- .../emulator/LoggingOutputStream.java | 48 +++++++++++++++++++ .../emulator/VT100TerminalControl.java | 6 ++- 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/LoggingOutputStream.java diff --git a/terminal/org.eclipse.tm.terminal.telnet/src/org/eclipse/tm/internal/terminal/telnet/TelnetConnection.java b/terminal/org.eclipse.tm.terminal.telnet/src/org/eclipse/tm/internal/terminal/telnet/TelnetConnection.java index 895951ea5bf..6fcb145ca3f 100644 --- a/terminal/org.eclipse.tm.terminal.telnet/src/org/eclipse/tm/internal/terminal/telnet/TelnetConnection.java +++ b/terminal/org.eclipse.tm.terminal.telnet/src/org/eclipse/tm/internal/terminal/telnet/TelnetConnection.java @@ -311,10 +311,6 @@ public class TelnetConnection extends Thread implements TelnetCodes { terminalControl.setState(TerminalState.CLOSED); break; } else { - if(Logger.isLogEnabled()) - Logger.log("Received " + nRawBytes + " bytes: '" + //$NON-NLS-1$ //$NON-NLS-2$ - Logger.encode(new String(rawBytes, 0, nRawBytes)) + "'"); //$NON-NLS-1$ - // Process any TELNET protocol data that we receive. Don't // send any TELNET protocol data until we are sure the remote // endpoint is a TELNET server. diff --git a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/LoggingOutputStream.java b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/LoggingOutputStream.java new file mode 100644 index 00000000000..b71c907442a --- /dev/null +++ b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/LoggingOutputStream.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. 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: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.emulator; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.eclipse.tm.internal.terminal.provisional.api.Logger; + +public class LoggingOutputStream extends FilterOutputStream { + + public LoggingOutputStream(OutputStream out) { + super(out); + } + + public void write(byte[] b, int off, int len) throws IOException { + if(Logger.isLogEnabled()) + Logger.log("Received " + len + " bytes: '" + //$NON-NLS-1$ //$NON-NLS-2$ + Logger.encode(new String(b, 0, len)) + "'"); //$NON-NLS-1$ + + // we cannot call super.write, because this would call our write + // which logs character by character..... + //super.write(b, off, len); + if ((off | len | (b.length - (len + off)) | (off + len)) < 0) + throw new IndexOutOfBoundsException(); + + for (int i = 0 ; i < len ; i++) { + super.write(b[off + i]); + } + } + + public void write(int b) throws IOException { + if(Logger.isLogEnabled()) + Logger.log("Received " + 1 + " bytes: '" + //$NON-NLS-1$ //$NON-NLS-2$ + Logger.encode(new String(new byte[]{(byte)b}, 0, 1)) + "'"); //$NON-NLS-1$ + super.write(b); + } + +} diff --git a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/VT100TerminalControl.java b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/VT100TerminalControl.java index b0d0c81494c..4ecf4a92ca6 100644 --- a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/VT100TerminalControl.java +++ b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/emulator/VT100TerminalControl.java @@ -581,7 +581,11 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC } public OutputStream getRemoteToTerminalOutputStream() { - return fInputStream.getOutputStream(); + if(Logger.isLogEnabled()) { + return new LoggingOutputStream(fInputStream.getOutputStream()); + } else { + return fInputStream.getOutputStream(); + } } protected boolean isLogCharEnabled() { return TerminalPlugin.isOptionEnabled(Logger.TRACE_DEBUG_LOG_CHAR);