Skip to content

Commit b0a88f5

Browse files
committed
Add fine-grained logging to LocalGcdHelper
1 parent 1c52730 commit b0a88f5

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@
5555
import java.util.zip.ZipEntry;
5656
import java.util.zip.ZipInputStream;
5757

58+
import java.util.logging.Level;
59+
import java.util.logging.Logger;
60+
5861
/**
5962
* Utility to start and stop local Google Cloud Datastore process.
6063
*/
6164
public class LocalGcdHelper {
6265

66+
private static final Logger log = Logger.getLogger(LocalGcdHelper.class.getName());
67+
6368
private final String projectId;
6469
private Path gcdPath;
6570
private ProcessStreamReader processReader;
@@ -99,14 +104,27 @@ private static Path installedGcdPath() {
99104
Path gcloudPath = executablePath(gcloudExecutableName);
100105
gcloudPath = (gcloudPath == null) ? null : gcloudPath.getParent();
101106
if (gcloudPath == null) {
107+
if (log.isLoggable(Level.FINE)) {
108+
log.fine("SDK not found");
109+
}
102110
return null;
103111
}
112+
if (log.isLoggable(Level.FINE)) {
113+
log.fine("SDK found, looking for datastore emulator");
114+
}
104115
Path installedGcdPath = gcloudPath.resolve("platform").resolve("gcd");
105116
if (Files.exists(installedGcdPath)) {
106117
try {
107118
String installedVersion = installedGcdVersion();
108119
if (installedVersion != null && installedVersion.startsWith(GCD_VERSION)) {
120+
if (log.isLoggable(Level.FINE)) {
121+
log.fine("SDK datastore emulator found");
122+
}
109123
return installedGcdPath;
124+
} else {
125+
if (log.isLoggable(Level.FINE)) {
126+
log.fine("SDK datastore emulator found but version mismatch");
127+
}
110128
}
111129
} catch (IOException | InterruptedException ignore) {
112130
// ignore
@@ -308,14 +326,23 @@ private void downloadGcd() throws IOException {
308326
// check if we already have a local copy of the gcd utility and download it if not.
309327
File gcdZipFile = new File(System.getProperty("java.io.tmpdir"), GCD_FILENAME);
310328
if (!gcdZipFile.exists() || !MD5_CHECKSUM.equals(md5(gcdZipFile))) {
329+
if (log.isLoggable(Level.FINE)) {
330+
log.fine("Fetching datastore emulator");
331+
}
311332
ReadableByteChannel rbc = Channels.newChannel(GCD_URL.openStream());
312333
try (FileOutputStream fos = new FileOutputStream(gcdZipFile)) {
313334
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
314335
}
336+
} else {
337+
if (log.isLoggable(Level.FINE)) {
338+
log.fine("Using cached datastore emulator");
339+
}
315340
}
316-
317341
// unzip the gcd
318342
try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(gcdZipFile))) {
343+
if (log.isLoggable(Level.FINE)) {
344+
log.fine("Unzipping datastore emulator");
345+
}
319346
ZipEntry entry = zipIn.getNextEntry();
320347
while (entry != null) {
321348
File filePath = new File(gcdPath.toFile(), entry.getName());
@@ -350,6 +377,9 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
350377
}
351378

352379
// create the datastore for the project
380+
if (log.isLoggable(Level.FINE)) {
381+
log.log(Level.FINE, "Creating datastore for the project: {0}", projectId);
382+
}
353383
Process temp = gcdCreateCommand.command(gcdAbsolutePath.toString(), "create", "-p", projectId,
354384
projectId)
355385
.redirectErrorInherit()
@@ -359,6 +389,9 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
359389
temp.waitFor();
360390

361391
// start the datastore for the project
392+
if (log.isLoggable(Level.FINE)) {
393+
log.log(Level.FINE, "Starting datastore emulator for the project: {0}", projectId);
394+
}
362395
temp = gcdStartCommand.command(gcdAbsolutePath.toString(), "start", "--testing",
363396
"--allow_remote_shutdown", projectId)
364397
.directory(gcdPath)

0 commit comments

Comments
 (0)