Skip to content

Commit f3a83a5

Browse files
committed
[GR-52401] Add logging to GraalPy launcher #386.
PullRequest: graalpython/3227
2 parents 4bd8b61 + 8780de0 commit f3a83a5

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public static void main(String[] args) {
8989
private static long startupWallClockTime = -1;
9090
private static long startupNanoTime = -1;
9191

92+
private boolean verboseLauncher = false;
9293
private ArrayList<String> programArgs = null;
9394
private ArrayList<String> origArgs = null;
9495
private String commandString = null;
@@ -116,6 +117,10 @@ public static void main(String[] args) {
116117
private String checkHashPycsMode = "default";
117118
private String execName;
118119

120+
public GraalPythonMain() {
121+
verboseLauncher = Boolean.parseBoolean(System.getenv("VERBOSE_GRAALVM_LAUNCHERS"));
122+
}
123+
119124
protected static void setStartupTime() {
120125
if (GraalPythonMain.startupNanoTime == -1) {
121126
GraalPythonMain.startupNanoTime = System.nanoTime();
@@ -480,10 +485,12 @@ protected String getLauncherExecName() {
480485
return execName;
481486
}
482487
execName = getProgramName();
488+
log("initial executable name: ", execName);
483489
if (execName == null) {
484490
return null;
485491
}
486492
execName = calculateProgramFullPath(execName);
493+
log("resolved executable name: ", execName);
487494
return execName;
488495
}
489496

@@ -505,7 +512,7 @@ protected String getLauncherExecName() {
505512
* @param program The program name as passed in the process' argument vector (position 0).
506513
* @return The absolute path to the program or {@code null}.
507514
*/
508-
private static String calculateProgramFullPath(String program) {
515+
private String calculateProgramFullPath(String program) {
509516
Path programPath = Paths.get(program);
510517

511518
// If this is an absolute path, we are already fine.
@@ -522,9 +529,11 @@ private static String calculateProgramFullPath(String program) {
522529
// Resolve the program name with respect to the PATH variable.
523530
String path = getEnv("PATH");
524531
if (path != null) {
532+
log("resolving the executable name in $PATH = ", path);
525533
int last = 0;
526534
for (int i = path.indexOf(File.pathSeparatorChar); i != -1; i = path.indexOf(File.pathSeparatorChar, last)) {
527535
Path resolvedProgramName = Paths.get(path.substring(last, i)).resolve(programPath);
536+
log("checking if exists and is executable: ", resolvedProgramName);
528537
if (Files.isExecutable(resolvedProgramName)) {
529538
return resolvedProgramName.toString();
530539
}
@@ -533,6 +542,8 @@ private static String calculateProgramFullPath(String program) {
533542
// 'i' points to ':'
534543
last = i + 1;
535544
}
545+
} else {
546+
log("executable name looks like it is from $PATH, but $PATH is not available.");
536547
}
537548
return null;
538549
}
@@ -598,6 +609,7 @@ private String getExecutable() {
598609
if (launcherExecName != null) {
599610
return launcherExecName;
600611
}
612+
log("cannot determine the executable path. Using java command invocation for executable.");
601613
String[] executableList = getExecutableList();
602614
for (int i = 0; i < executableList.length; i++) {
603615
if (executableList[i].matches("\\s")) {
@@ -831,22 +843,27 @@ private void findAndApplyVenvCfg(Builder contextBuilder, String executable) {
831843
try {
832844
binDir = Paths.get(executable).getParent();
833845
} catch (InvalidPathException e) {
846+
log("cannot determine the parent directory of the executable");
834847
return;
835848
}
836849
if (binDir == null) {
850+
log("parent directory of the executable does not exist");
837851
return;
838852
}
839853
Path venvCfg = binDir.resolve(J_PYENVCFG);
854+
log("checking: ", venvCfg);
840855
if (!Files.exists(venvCfg)) {
841856
Path binParent = binDir.getParent();
842857
if (binParent == null) {
843858
return;
844859
}
845860
venvCfg = binParent.resolve(J_PYENVCFG);
861+
log("checking: ", venvCfg);
846862
if (!Files.exists(venvCfg)) {
847863
return;
848864
}
849865
}
866+
log("found: ", venvCfg);
850867
try (BufferedReader reader = Files.newBufferedReader(venvCfg)) {
851868
String line;
852869
while ((line = reader.readLine()) != null) {
@@ -1439,4 +1456,14 @@ private static void addArgument(String pid, String uuid, ArrayList<String> envAr
14391456
private static boolean doEcho(@SuppressWarnings("unused") Context context) {
14401457
return true;
14411458
}
1459+
1460+
private void log(String message) {
1461+
log(message, "");
1462+
}
1463+
1464+
private void log(String message1, Object message2) {
1465+
if (verboseLauncher) {
1466+
System.err.println("GraalPy launcher: " + message1 + message2);
1467+
}
1468+
}
14421469
}

0 commit comments

Comments
 (0)