1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Improve Terminal Logging by quoting characters. Move logs to "eclipselogs"

This commit is contained in:
Martin Oberhuber 2007-01-03 11:52:41 +00:00
parent 95a8de4d41
commit cd1decc3cf
3 changed files with 74 additions and 6 deletions

View file

@ -41,13 +41,14 @@ public final class Logger {
static {
String logFile = null;
File logDirWindows = new File("C:\\wblogs"); //$NON-NLS-1$
File logDirUNIX = new File("/tmp/wblogs"); //$NON-NLS-1$
//TODO I think this should go into the workspace metadata instead.
File logDirWindows = new File("C:\\eclipselogs"); //$NON-NLS-1$
File logDirUNIX = new File("/tmp/eclipselogs"); //$NON-NLS-1$
if (logDirWindows.isDirectory()) {
logFile = logDirWindows + "\\wbterminal.log"; //$NON-NLS-1$
logFile = logDirWindows + "\\tmterminal.log"; //$NON-NLS-1$
} else if (logDirUNIX.isDirectory()) {
logFile = logDirUNIX + "/wbterminal.log"; //$NON-NLS-1$
logFile = logDirUNIX + "/tmterminal.log"; //$NON-NLS-1$
}
if (logFile != null) {
@ -61,6 +62,71 @@ public final class Logger {
}
}
}
/**
* Encodes a String such that non-printable control characters are
* converted into user-readable escape sequences for logging.
* @param message String to encode
* @return encoded String
*/
public static final String encode(String message) {
boolean encoded = false;
StringBuffer buf = new StringBuffer(message.length()+32);
for (int i=0; i<message.length(); i++) {
char c=message.charAt(i);
switch(c) {
case '\\':
case '\'':
buf.append('\\'); buf.append(c); encoded=true;
break;
case '\r':
buf.append('\\'); buf.append('r'); encoded=true;
break;
case '\n':
buf.append('\\'); buf.append('n'); encoded=true;
break;
case '\t':
buf.append('\\'); buf.append('t'); encoded=true;
break;
case '\f':
buf.append('\\'); buf.append('f'); encoded=true;
break;
case '\b':
buf.append('\\'); buf.append('b'); encoded=true;
break;
default:
if (c <= '\u000f') {
buf.append('\\'); buf.append('x'); buf.append('0');
buf.append(Integer.toHexString(c));
encoded=true;
} else if (c>=' ' && c<'\u007f') {
buf.append(c);
} else {
buf.append('\\'); buf.append('u');
if (c<='\u0fff') {
buf.append('0');
if (c<='\u00ff') {
buf.append('0');
}
}
buf.append(Integer.toHexString(c));
encoded=true;
}
}
}
if (encoded) {
return buf.toString();
}
return message;
}
/**
* Checks if logging is enabled.
* @return true if logging is enabled.
*/
public static final boolean isLogEnabled() {
return (logStream!=null);
}
/**
* Logs the specified message. Do not append a newline to parameter

View file

@ -371,7 +371,9 @@ public class TerminalText implements Runnable, ControlListener {
* host.
*/
public synchronized void setNewText(StringBuffer newBuffer) {
Logger.log("new text: '" + newBuffer + "'"); //$NON-NLS-1$ //$NON-NLS-2$
if (Logger.isLogEnabled()) {
Logger.log("new text: '" + Logger.encode(newBuffer.toString()) + "'"); //$NON-NLS-1$ //$NON-NLS-2$
}
newText = newBuffer;
// When continuous output is being processed by the Terminal view code, it

View file

@ -312,7 +312,7 @@ public class TelnetConnection extends Thread implements TelnetCodes {
break;
} else {
Logger.log("Received " + nRawBytes + " bytes: '" + //$NON-NLS-1$ //$NON-NLS-2$
new String(rawBytes, 0, nRawBytes) + "'"); //$NON-NLS-1$
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