Skip to content

Commit e3a50cb

Browse files
Fixed: Use android.util.Log for terminal-emulator logging if TerminalSessionClient is null like when running tests
1 parent af5fef4 commit e3a50cb

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.termux.terminal;
2+
3+
import android.util.Log;
4+
5+
import java.io.IOException;
6+
import java.io.PrintWriter;
7+
import java.io.StringWriter;
8+
9+
public class Logger {
10+
11+
public static void logError(TerminalSessionClient client, String logTag, String message) {
12+
if (client != null)
13+
client.logError(logTag, message);
14+
else
15+
Log.e(logTag, message);
16+
}
17+
18+
public static void logWarn(TerminalSessionClient client, String logTag, String message) {
19+
if (client != null)
20+
client.logWarn(logTag, message);
21+
else
22+
Log.w(logTag, message);
23+
}
24+
25+
public static void logInfo(TerminalSessionClient client, String logTag, String message) {
26+
if (client != null)
27+
client.logInfo(logTag, message);
28+
else
29+
Log.i(logTag, message);
30+
}
31+
32+
public static void logDebug(TerminalSessionClient client, String logTag, String message) {
33+
if (client != null)
34+
client.logDebug(logTag, message);
35+
else
36+
Log.d(logTag, message);
37+
}
38+
39+
public static void logVerbose(TerminalSessionClient client, String logTag, String message) {
40+
if (client != null)
41+
client.logVerbose(logTag, message);
42+
else
43+
Log.v(logTag, message);
44+
}
45+
46+
public static void logStackTraceWithMessage(TerminalSessionClient client, String tag, String message, Throwable throwable) {
47+
logError(client, tag, getMessageAndStackTraceString(message, throwable));
48+
}
49+
50+
public static String getMessageAndStackTraceString(String message, Throwable throwable) {
51+
if (message == null && throwable == null)
52+
return null;
53+
else if (message != null && throwable != null)
54+
return message + ":\n" + getStackTraceString(throwable);
55+
else if (throwable == null)
56+
return message;
57+
else
58+
return getStackTraceString(throwable);
59+
}
60+
61+
public static String getStackTraceString(Throwable throwable) {
62+
if (throwable == null) return null;
63+
64+
String stackTraceString = null;
65+
66+
try {
67+
StringWriter errors = new StringWriter();
68+
PrintWriter pw = new PrintWriter(errors);
69+
throwable.printStackTrace(pw);
70+
pw.close();
71+
stackTraceString = errors.toString();
72+
errors.close();
73+
} catch (IOException e) {
74+
e.printStackTrace();
75+
}
76+
77+
return stackTraceString;
78+
}
79+
80+
}

terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ public void processCodePoint(int b) {
824824
if (internalBit != -1) {
825825
value = isDecsetInternalBitSet(internalBit) ? 1 : 2; // 1=set, 2=reset.
826826
} else {
827-
mClient.logError(LOG_TAG, "Got DECRQM for unrecognized private DEC mode=" + mode);
827+
Logger.logError(mClient, LOG_TAG, "Got DECRQM for unrecognized private DEC mode=" + mode);
828828
value = 0; // 0=not recognized, 3=permanently set, 4=permanently reset
829829
}
830830
}
@@ -961,7 +961,7 @@ private void doDeviceControl(int b) {
961961
case "&8": // Undo key - ignore.
962962
break;
963963
default:
964-
mClient.logWarn(LOG_TAG, "Unhandled termcap/terminfo name: '" + trans + "'");
964+
Logger.logWarn(mClient, LOG_TAG, "Unhandled termcap/terminfo name: '" + trans + "'");
965965
}
966966
// Respond with invalid request:
967967
mSession.write("\033P0+r" + part + "\033\\");
@@ -973,12 +973,12 @@ private void doDeviceControl(int b) {
973973
mSession.write("\033P1+r" + part + "=" + hexEncoded + "\033\\");
974974
}
975975
} else {
976-
mClient.logError(LOG_TAG, "Invalid device termcap/terminfo name of odd length: " + part);
976+
Logger.logError(mClient, LOG_TAG, "Invalid device termcap/terminfo name of odd length: " + part);
977977
}
978978
}
979979
} else {
980980
if (LOG_ESCAPE_SEQUENCES)
981-
mClient.logError(LOG_TAG, "Unrecognized device control string: " + dcs);
981+
Logger.logError(mClient, LOG_TAG, "Unrecognized device control string: " + dcs);
982982
}
983983
finishSequence();
984984
}
@@ -1068,7 +1068,7 @@ private void doCsiQuestionMark(int b) {
10681068
int externalBit = mArgs[i];
10691069
int internalBit = mapDecSetBitToInternalBit(externalBit);
10701070
if (internalBit == -1) {
1071-
mClient.logWarn(LOG_TAG, "Ignoring request to save/recall decset bit=" + externalBit);
1071+
Logger.logWarn(mClient, LOG_TAG, "Ignoring request to save/recall decset bit=" + externalBit);
10721072
} else {
10731073
if (b == 's') {
10741074
mSavedDecSetFlags |= internalBit;
@@ -1258,7 +1258,7 @@ private void doCsiBiggerThan(int b) {
12581258
// (1) enables this feature for keys except for those with well-known behavior, e.g., Tab, Backarrow and
12591259
// some special control character cases, e.g., Control-Space to make a NUL.
12601260
// (2) enables this feature for keys including the exceptions listed.
1261-
mClient.logError(LOG_TAG, "(ignored) CSI > MODIFY RESOURCE: " + getArg0(-1) + " to " + getArg1(-1));
1261+
Logger.logError(mClient, LOG_TAG, "(ignored) CSI > MODIFY RESOURCE: " + getArg0(-1) + " to " + getArg1(-1));
12621262
break;
12631263
default:
12641264
parseArg(b);
@@ -1805,7 +1805,7 @@ private void selectGraphicRendition() {
18051805
int firstArg = mArgs[i + 1];
18061806
if (firstArg == 2) {
18071807
if (i + 4 > mArgIndex) {
1808-
mClient.logWarn(LOG_TAG, "Too few CSI" + code + ";2 RGB arguments");
1808+
Logger.logWarn(mClient, LOG_TAG, "Too few CSI" + code + ";2 RGB arguments");
18091809
} else {
18101810
int red = mArgs[i + 2], green = mArgs[i + 3], blue = mArgs[i + 4];
18111811
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
@@ -1830,7 +1830,7 @@ private void selectGraphicRendition() {
18301830
mBackColor = color;
18311831
}
18321832
} else {
1833-
if (LOG_ESCAPE_SEQUENCES) mClient.logWarn(LOG_TAG, "Invalid color index: " + color);
1833+
if (LOG_ESCAPE_SEQUENCES) Logger.logWarn(mClient, LOG_TAG, "Invalid color index: " + color);
18341834
}
18351835
} else {
18361836
finishSequenceAndLogError("Invalid ISO-8613-3 SGR first argument: " + firstArg);
@@ -1847,7 +1847,7 @@ private void selectGraphicRendition() {
18471847
mBackColor = code - 100 + 8;
18481848
} else {
18491849
if (LOG_ESCAPE_SEQUENCES)
1850-
mClient.logWarn(LOG_TAG, String.format("SGR unknown code %d", code));
1850+
Logger.logWarn(mClient, LOG_TAG, String.format("SGR unknown code %d", code));
18511851
}
18521852
}
18531853
}
@@ -1981,7 +1981,7 @@ private void doOscSetTextParameters(String bellOrStringTerminator) {
19811981
String clipboardText = new String(Base64.decode(textParameter.substring(startIndex), 0), StandardCharsets.UTF_8);
19821982
mSession.onCopyTextToClipboard(clipboardText);
19831983
} catch (Exception e) {
1984-
mClient.logError(LOG_TAG, "OSC Manipulate selection, invalid string '" + textParameter + "");
1984+
Logger.logError(mClient, LOG_TAG, "OSC Manipulate selection, invalid string '" + textParameter + "");
19851985
}
19861986
break;
19871987
case 104:
@@ -2177,7 +2177,7 @@ private void logError(String errorType) {
21772177
}
21782178

21792179
private void finishSequenceAndLogError(String error) {
2180-
if (LOG_ESCAPE_SEQUENCES) mClient.logWarn(LOG_TAG, error);
2180+
if (LOG_ESCAPE_SEQUENCES) Logger.logWarn(mClient, LOG_TAG, error);
21812181
finishSequence();
21822182
}
21832183

terminal-emulator/src/main/java/com/termux/terminal/TerminalSession.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void finishIfRunning() {
236236
try {
237237
Os.kill(mShellPid, OsConstants.SIGKILL);
238238
} catch (ErrnoException e) {
239-
mClient.logWarn(LOG_TAG, "Failed sending SIGKILL: " + e.getMessage());
239+
Logger.logWarn(mClient, LOG_TAG, "Failed sending SIGKILL: " + e.getMessage());
240240
}
241241
}
242242
}
@@ -308,7 +308,7 @@ public String getCwd() {
308308
return outputPath;
309309
}
310310
} catch (IOException | SecurityException e) {
311-
mClient.logStackTraceWithMessage(LOG_TAG, "Error getting current directory", e);
311+
Logger.logStackTraceWithMessage(mClient, LOG_TAG, "Error getting current directory", e);
312312
}
313313
return null;
314314
}
@@ -326,7 +326,7 @@ private static FileDescriptor wrapFileDescriptor(int fileDescriptor, TerminalSes
326326
descriptorField.setAccessible(true);
327327
descriptorField.set(result, fileDescriptor);
328328
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
329-
client.logStackTraceWithMessage(LOG_TAG, "Error accessing FileDescriptor#descriptor private field", e);
329+
Logger.logStackTraceWithMessage(client, LOG_TAG, "Error accessing FileDescriptor#descriptor private field", e);
330330
System.exit(1);
331331
}
332332
return result;

0 commit comments

Comments
 (0)