diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java index 23c008779..5f3d46224 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java @@ -201,6 +201,9 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon @With private final boolean allowLoadingDuplicates; + @With + private final IWorkerContext.ILoggingService loggingService; + public SimpleWorkerContextBuilder() { cacheTerminologyClientErrors = false; alwaysUseTerminologyServer = false; @@ -209,6 +212,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon locale = null; userAgent = null; allowLoadingDuplicates = false; + loggingService = new SystemOutLoggingService(); } private SimpleWorkerContext getSimpleWorkerContextInstance() throws IOException { @@ -227,6 +231,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon private SimpleWorkerContext build(SimpleWorkerContext context) throws IOException { context.initTS(terminologyCachePath); context.setUserAgent(userAgent); + context.setLogger(loggingService); return context; } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SystemOutLoggingService.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SystemOutLoggingService.java index f38076180..9daa6d791 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SystemOutLoggingService.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SystemOutLoggingService.java @@ -1,6 +1,15 @@ package org.hl7.fhir.r5.context; -class SystemOutLoggingService implements IWorkerContext.ILoggingService { +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class SystemOutLoggingService implements IWorkerContext.ILoggingService { + + private final boolean debug; + + public SystemOutLoggingService() { + this(false); + } @Override public void logMessage(String message) { @@ -9,6 +18,8 @@ class SystemOutLoggingService implements IWorkerContext.ILoggingService { @Override public void logDebugMessage(LogCategory category, String message) { - System.out.println(" -" + category.name().toLowerCase() + ": " + message); - } + if (debug) { + System.out.println(" -" + category.name().toLowerCase() + ": " + message); + } + } } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java index 13ced41da..8dd098234 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java @@ -12,9 +12,11 @@ import org.hl7.fhir.convertors.txClient.TerminologyClientFactory; import org.hl7.fhir.exceptions.DefinitionException; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.r5.conformance.ProfileUtilities; +import org.hl7.fhir.r5.context.IWorkerContext; import org.hl7.fhir.r5.context.IWorkerContext.ICanonicalResourceLocator; import org.hl7.fhir.r5.context.IWorkerContext.PackageVersion; import org.hl7.fhir.r5.context.SimpleWorkerContext; +import org.hl7.fhir.r5.context.SystemOutLoggingService; import org.hl7.fhir.r5.elementmodel.Element; import org.hl7.fhir.r5.elementmodel.Manager; import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat; @@ -207,6 +209,10 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP @With private final boolean canRunWithoutTerminologyServer; + @With + private final IWorkerContext.ILoggingService loggingService; + + public ValidationEngineBuilder() { terminologyCachePath = null; userAgent = null; @@ -216,9 +222,10 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP txVersion = null; timeTracker = null; canRunWithoutTerminologyServer = false; + loggingService = new SystemOutLoggingService(); } - public ValidationEngineBuilder(String terminologyCachePath, String userAgent, String version, String txServer, String txLog, FhirPublication txVersion, TimeTracker timeTracker, boolean canRunWithoutTerminologyServer) { + public ValidationEngineBuilder(String terminologyCachePath, String userAgent, String version, String txServer, String txLog, FhirPublication txVersion, TimeTracker timeTracker, boolean canRunWithoutTerminologyServer, IWorkerContext.ILoggingService loggingService) { this.terminologyCachePath = terminologyCachePath; this.userAgent = userAgent; this.version = version; @@ -227,15 +234,16 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP this.txVersion = txVersion; this.timeTracker = timeTracker; this.canRunWithoutTerminologyServer = canRunWithoutTerminologyServer; + this.loggingService = loggingService; } public ValidationEngineBuilder withTxServer(String txServer, String txLog, FhirPublication txVersion) { - return new ValidationEngineBuilder(terminologyCachePath, userAgent, version, txServer, txLog, txVersion,timeTracker, canRunWithoutTerminologyServer); + return new ValidationEngineBuilder(terminologyCachePath, userAgent, version, txServer, txLog, txVersion,timeTracker, canRunWithoutTerminologyServer, loggingService); } public ValidationEngine fromNothing() throws IOException { ValidationEngine engine = new ValidationEngine(); - SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder(); + SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder().withLoggingService(loggingService); if (terminologyCachePath != null) contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath); engine.setContext(contextBuilder.build()); @@ -246,7 +254,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP public ValidationEngine fromSource(String src) throws IOException, URISyntaxException { ValidationEngine engine = new ValidationEngine(); - engine.loadCoreDefinitions(src, false, terminologyCachePath, userAgent, timeTracker); + engine.loadCoreDefinitions(src, false, terminologyCachePath, userAgent, timeTracker, loggingService); engine.getContext().setCanRunWithoutTerminology(canRunWithoutTerminologyServer); if (txServer != null) { @@ -258,11 +266,11 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP } } - private void loadCoreDefinitions(String src, boolean recursive, String terminologyCachePath, String userAgent, TimeTracker tt) throws FHIRException, IOException { + private void loadCoreDefinitions(String src, boolean recursive, String terminologyCachePath, String userAgent, TimeTracker tt, IWorkerContext.ILoggingService loggingService) throws FHIRException, IOException { NpmPackage npm = getPcm().loadPackage(src, null); if (npm != null) { version = npm.fhirVersion(); - SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder(); + SimpleWorkerContext.SimpleWorkerContextBuilder contextBuilder = new SimpleWorkerContext.SimpleWorkerContextBuilder().withLoggingService(loggingService); if (terminologyCachePath != null) contextBuilder = contextBuilder.withTerminologyCachePath(terminologyCachePath); if (userAgent != null) { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java index a2bce8dcb..0457d25ce 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java @@ -2,6 +2,7 @@ package org.hl7.fhir.validation.cli.services; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.r5.context.SimpleWorkerContext; +import org.hl7.fhir.r5.context.SystemOutLoggingService; import org.hl7.fhir.r5.context.TerminologyCache; import org.hl7.fhir.r5.elementmodel.Manager; import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat; @@ -329,6 +330,7 @@ public class ValidationService { String txver = validator.setTerminologyServer(cliContext.getTxServer(), cliContext.getTxLog(), ver); System.out.println(" - Version " + txver + " (" + tt.milestone() + ")"); validator.setDebug(cliContext.isDoDebug()); + validator.getContext().setLogger(new SystemOutLoggingService(cliContext.isDoDebug())); for (String src : cliContext.getIgs()) { igLoader.loadIg(validator.getIgs(), validator.getBinaries(), src, cliContext.isRecursive()); }