Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log posthook stdout and stderr (#262) #263

Merged
merged 1 commit into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

}