From 637aeccc13237a7b76471bffeb6226654e726d6c Mon Sep 17 00:00:00 2001 From: David Coutadeur Date: Mon, 22 Jan 2024 11:55:19 +0100 Subject: [PATCH] log posthook stdout and stderr (#262) --- src/main/java/org/lsc/Hooks.java | 40 +++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/lsc/Hooks.java b/src/main/java/org/lsc/Hooks.java index 69a557fc..f8aa0a36 100644 --- a/src/main/java/org/lsc/Hooks.java +++ b/src/main/java/org/lsc/Hooks.java @@ -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 @@ -96,14 +98,15 @@ 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()); @@ -111,12 +114,24 @@ public final static void callHook( LscModificationType operationType, 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 {}", @@ -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(); + } + }