Skip to content

Commit 007a9ab

Browse files
author
Ajay Kannan
committed
simplify logging logic
1 parent 6e66dfd commit 007a9ab

1 file changed

Lines changed: 36 additions & 30 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ private static Path executablePath(String cmd) {
179179
}
180180

181181
private static class ProcessStreamReader extends Thread {
182-
private final BufferedReader reader;
182+
private volatile BufferedReader reader;
183+
private boolean terminated = false;
183184

184185
ProcessStreamReader(InputStream inputStream) {
185186
super("Local GCD InputStream reader");
@@ -188,14 +189,18 @@ private static class ProcessStreamReader extends Thread {
188189
}
189190

190191
void terminate() throws IOException {
192+
terminated = true;
191193
reader.close();
192194
}
193195

194196
@Override
195197
public void run() {
196198
try {
197-
while (!(reader.readLine() != null)) {
198-
// consume line
199+
while (!terminated) {
200+
String line = reader.readLine();
201+
if (line == null) {
202+
terminated = true;
203+
}
199204
}
200205
} catch (IOException e) {
201206
// ignore
@@ -214,9 +219,11 @@ private static class ProcessErrorStreamReader extends Thread {
214219
private static final String GCD_LOGGING_CLASS =
215220
"com.google.apphosting.client.serviceapp.BaseApiServlet";
216221

217-
private final BufferedReader errorReader;
222+
private volatile BufferedReader errorReader;
218223
private String currentLog = null;
219224
private Level currentLogLevel = null;
225+
private boolean collectionMode = false;
226+
private boolean terminated = false;
220227

221228
ProcessErrorStreamReader(InputStream errorStream, String blockUntil) throws IOException {
222229
super("Local GCD ErrorStream reader");
@@ -231,52 +238,55 @@ private static class ProcessErrorStreamReader extends Thread {
231238
}
232239

233240
void terminate() throws IOException {
241+
terminated = true;
234242
writeLog(currentLogLevel, currentLog);
235243
errorReader.close();
236244
}
237245

238246
@Override
239247
public void run() {
240248
try {
241-
boolean errorReaderDone = false;
242249
String previousLine = "";
243-
String currentLine = "";
244-
while (!errorReaderDone) {
245-
previousLine = currentLine;
246-
currentLine = errorReader.readLine();
247-
if (currentLine == null) {
248-
errorReaderDone = true;
250+
String nextLine = "";
251+
while (!terminated) {
252+
previousLine = nextLine;
253+
nextLine = errorReader.readLine();
254+
if (nextLine == null) {
255+
terminated = true;
256+
writeLog(currentLogLevel, currentLog);
249257
} else {
250-
processLogLine(previousLine, currentLine);
258+
processLogLine(previousLine, nextLine);
251259
}
252260
}
253261
} catch (IOException e) {
254262
// ignore
255263
}
256264
}
257265

258-
private void processLogLine(String previousLine, String currentLine) {
266+
private void processLogLine(String previousLine, String nextLine) {
259267
// Each gcd log is two lines with the following format:
260268
// [Date] [Time] [GCD_LOGGING_CLASS] [method]
261269
// [LEVEL]: error message
262270
// Exceptions and stack traces are included in gcd error stream, separated by a newline
263-
Level nextLogLevel = getLevel(currentLine);
271+
Level nextLogLevel = getLevel(nextLine);
264272
if (previousLine.contains(GCD_LOGGING_CLASS) && nextLogLevel != null) {
265273
writeLog(currentLogLevel, currentLog);
266-
if (currentLine.startsWith("SEVERE: ")) {
267-
// don't show duplicate error messages from gcd.sh (see issue #258)
268-
currentLog = null;
269-
currentLogLevel = null;
274+
currentLog = "";
275+
currentLogLevel = nextLogLevel;
276+
if (nextLine.startsWith("SEVERE: ")) {
277+
collectionMode = false; // don't show duplicate messages (see issue #258)
278+
} else {
279+
collectionMode = true;
280+
}
281+
} else if (collectionMode && currentLog.length() > LOG_LENGTH_LIMIT) {
282+
collectionMode = false;
283+
} else if (collectionMode) {
284+
if (currentLog.length() == 0) {
285+
// strip level out of the line
286+
currentLog = "GCD" + previousLine.split(":", 2)[1] + System.getProperty("line.separator");
270287
} else {
271-
currentLog = "GCD" + currentLine.split(":", 2)[1] + System.getProperty("line.separator");
272-
currentLogLevel = nextLogLevel;
288+
currentLog += previousLine + System.getProperty("line.separator");
273289
}
274-
} else if (currentLog != null && currentLog.length() > LOG_LENGTH_LIMIT) {
275-
// log processing may be off, so drop some logs before the string becomes too big
276-
currentLog = null;
277-
currentLogLevel = null;
278-
} else if (currentLog != null && isUsefulLogInfo(currentLine)) {
279-
currentLog += currentLine + System.getProperty("line.separator");
280290
}
281291
}
282292

@@ -286,10 +296,6 @@ private static void writeLog(Level level, String msg) {
286296
}
287297
}
288298

289-
private static boolean isUsefulLogInfo(String line) {
290-
return !line.trim().startsWith("at ") && !line.contains(GCD_LOGGING_CLASS);
291-
}
292-
293299
private static Level getLevel(String line) {
294300
try {
295301
return Level.parse(line.split(":")[0]);

0 commit comments

Comments
 (0)