Skip to content

Commit

Permalink
8248261: Add timestamps to jpackage and jpackage tests verbose output
Browse files Browse the repository at this point in the history
Reviewed-by: herrick, asemenyuk
  • Loading branch information
Alexander Matveev committed Jul 13, 2020
1 parent 8f8ff52 commit 231a840
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
Expand Up @@ -542,11 +542,11 @@ public boolean processArguments() {
Log.verbose(e);
} else {
String msg1 = e.getMessage();
Log.error(msg1);
Log.fatalError(msg1);
if (e.getCause() != null && e.getCause() != e) {
String msg2 = e.getCause().getMessage();
if (msg2 != null && !msg1.contains(msg2)) {
Log.error(msg2);
Log.fatalError(msg2);
}
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -45,7 +45,7 @@ public synchronized int run(
try {
return new jdk.incubator.jpackage.main.Main().execute(out, err, args);
} catch (RuntimeException re) {
Log.error(re.getMessage());
Log.fatalError(re.getMessage());
Log.verbose(re);
return 1;
}
Expand Down
Expand Up @@ -26,6 +26,8 @@
package jdk.incubator.jpackage.internal;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Log
Expand Down Expand Up @@ -77,7 +79,16 @@ public void info(String msg) {
}
}

public void fatalError(String msg) {
if (err != null) {
err.println(msg);
} else {
System.err.println(msg);
}
}

public void error(String msg) {
msg = addTimestamp(msg);
if (err != null) {
err.println(msg);
} else {
Expand All @@ -87,19 +98,28 @@ public void error(String msg) {

public void verbose(Throwable t) {
if (out != null && verbose) {
out.print(addTimestamp(""));
t.printStackTrace(out);
} else if (verbose) {
System.out.print(addTimestamp(""));
t.printStackTrace(System.out);
}
}

public void verbose(String msg) {
msg = addTimestamp(msg);
if (out != null && verbose) {
out.println(msg);
} else if (verbose) {
System.out.println(msg);
}
}

private String addTimestamp(String msg) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
Date time = new Date(System.currentTimeMillis());
return String.format("[%s] %s", sdf.format(time), msg);
}
}

private static Logger delegate = null;
Expand All @@ -120,6 +140,12 @@ public static void info(String msg) {
}
}

public static void fatalError(String msg) {
if (delegate != null) {
delegate.fatalError(msg);
}
}

public static void error(String msg) {
if (delegate != null) {
delegate.error(msg);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -76,11 +76,11 @@ private int execute(String... args) {
try {
newArgs = CommandLine.parse(args);
} catch (FileNotFoundException fnfe) {
Log.error(MessageFormat.format(I18N.getString(
Log.fatalError(MessageFormat.format(I18N.getString(
"ERR_CannotParseOptions"), fnfe.getMessage()));
return 1;
} catch (IOException ioe) {
Log.error(ioe.getMessage());
Log.fatalError(ioe.getMessage());
return 1;
}

Expand Down
9 changes: 9 additions & 0 deletions test/jdk/tools/jpackage/helpers/jdk/jpackage/test/TKit.java
Expand Up @@ -39,10 +39,12 @@
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -217,7 +219,14 @@ public static boolean isUbuntu() {
return false;
}

private static String addTimestamp(String msg) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
Date time = new Date(System.currentTimeMillis());
return String.format("[%s] %s", sdf.format(time), msg);
}

static void log(String v) {
v = addTimestamp(v);
System.out.println(v);
if (extraLogStream != null) {
extraLogStream.println(v);
Expand Down
18 changes: 9 additions & 9 deletions test/jdk/tools/jpackage/linux/LinuxResourceTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -73,21 +73,21 @@ public static void testHardcodedProperties() throws IOException {
})
.addBundleVerifier((cmd, result) -> {
TKit.assertTextStream("Using custom package resource [DEB control file]")
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
TKit.assertTextStream(String.format(
"Expected value of \"Package\" property is [%s]. Actual value in output package is [dont-install-me]",
LinuxHelper.getPackageName(cmd)))
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
TKit.assertTextStream(
"Expected value of \"Version\" property is [1.0-1]. Actual value in output package is [1.2.3-R2]")
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
TKit.assertTextStream(String.format(
"Expected value of \"Architecture\" property is [%s]. Actual value in output package is [bar]",
LinuxHelper.getDefaultPackageArch(cmd.packageType())))
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
})
.forTypes(PackageType.LINUX_RPM)
Expand Down Expand Up @@ -116,20 +116,20 @@ public static void testHardcodedProperties() throws IOException {
})
.addBundleVerifier((cmd, result) -> {
TKit.assertTextStream("Using custom package resource [RPM spec file]")
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
TKit.assertTextStream(String.format(
"Expected value of \"Name\" property is [%s]. Actual value in output package is [dont-install-me]",
LinuxHelper.getPackageName(cmd)))
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
TKit.assertTextStream(
"Expected value of \"Version\" property is [1.0]. Actual value in output package is [1.2.3]")
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
TKit.assertTextStream(
"Expected value of \"Release\" property is [1]. Actual value in output package is [R2]")
.predicate(String::startsWith)
.predicate(String::contains)
.apply(result.getOutput().stream());
})
.run();
Expand Down

0 comments on commit 231a840

Please sign in to comment.