Skip to content

Commit

Permalink
log posthook stdout and stderr (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur authored and davidcoutadeur committed May 15, 2024
1 parent d9c5c3e commit 91b632c
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/main/java/org/lsc/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.ProcessBuilder;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Optional;
import java.util.Scanner;
import org.lsc.utils.output.LdifLayout;
import org.lsc.beans.syncoptions.ISyncOptions.OutputFormat;
import com.fasterxml.jackson.databind.ObjectMapper; // For encoding object to JSON
Expand Down Expand Up @@ -96,27 +98,40 @@ public final static void callHook( LscModificationType operationType,
}

try {
Process p;
if( modifications != null ) {
Process p = new ProcessBuilder(
p = new ProcessBuilder(
hook,
identifier,
operationType.getDescription())
.start();

// sends ldif modifications to stdin of hook script
// sends modifications to stdin of hook script
OutputStream stdin = p.getOutputStream();
stdin.write(modifications.getBytes());
stdin.write("\n".getBytes());
stdin.flush();
stdin.close();
}
else {
Process p = new ProcessBuilder(
p = new ProcessBuilder(
hook,
identifier,
operationType.getDescription())
.start();
}
printHookOutput(p.getInputStream(),
"stdout",
operationType.getDescription(),
hook,
outputFormat.toString(),
identifier);
printHookOutput(p.getErrorStream(),
"stderr",
operationType.getDescription(),
hook,
outputFormat.toString(),
identifier);
}
catch(IOException e) {
LOGGER.error("Error while calling {} posthook {} with format {} for {}",
Expand Down Expand Up @@ -145,4 +160,23 @@ public final static String getJsonModifications(final LscModifications lm) {
return json;
}

private static void printHookOutput( final InputStream src, String output,
String operation, String hook,
String outputFormat, String identifier) {
new Thread(new Runnable() {
public void run() {
Scanner sc = new Scanner(src);
while (sc.hasNextLine()) {
LOGGER.warn("Hook {} with format {} for identifier {} and operation {} returned {}: {}",
hook,
outputFormat,
identifier,
operation,
output,
sc.nextLine());
}
}
}).start();
}

}

0 comments on commit 91b632c

Please sign in to comment.