char tstamp[32];
time_t now;
- ASSERT(outfp == NULL);
- ASSERT(state == NOT_IN_GUEST_LOGGING);
+ /*
+ * There could be multiple LOG_START_MARK in the log,
+ * close existing one before opening a new file.
+ */
+ if (outfp) {
+ ASSERT(state == IN_GUEST_LOGGING);
+ Warning("Found a new start mark before end mark for "
+ "previous one\n");
+ fclose(outfp);
+ outfp = NULL;
+ } else {
+ ASSERT(state == NOT_IN_GUEST_LOGGING);
+ }
DEBUG_ONLY(state = IN_GUEST_LOGGING);
/*
ver = ver + sizeof "ver - " - 1;
version = strtol(ver, NULL, 0);
if (version != LOG_VERSION) {
- Warning("input version %d doesnt match the\
+ Warning("Input version %d doesn't match the\
version of this binary %d", version, LOG_VERSION);
} else {
- printf("reading file %s to %s \n", logInpFilename, fname);
+ printf("Reading file %s to %s \n", logInpFilename, fname);
if (!(outfp = fopen(fname, "wb"))) {
Warning("Error opening file %s\n", fname);
}
}
}
} else if (strstr(buf, LOG_END_MARK)) { // close the output file.
- ASSERT(state == IN_GUEST_LOGGING);
+ /*
+ * Need to check outfp, because we might get LOG_END_MARK
+ * before LOG_START_MARK due to log rotation.
+ */
+ if (outfp) {
+ ASSERT(state == IN_GUEST_LOGGING);
+ fclose(outfp);
+ outfp = NULL;
+ } else {
+ ASSERT(state == NOT_IN_GUEST_LOGGING);
+ Warning("Reached file end mark without start mark\n");
+ }
DEBUG_ONLY(state = NOT_IN_GUEST_LOGGING);
- fclose(outfp);
- outfp = NULL;
} else { // write to the output file
- ASSERT(state == IN_GUEST_LOGGING);
if (outfp) {
+ ASSERT(state == IN_GUEST_LOGGING);
ptrStr = strstr(buf, LOG_GUEST_MARK);
ptrStr += sizeof LOG_GUEST_MARK - 1;
if (Base64_Decode(ptrStr, base64Out, BUF_OUT_SIZE, &lenOut)) {
} else {
Warning("Error decoding output %s\n", ptrStr);
}
+ } else {
+ ASSERT(state == NOT_IN_GUEST_LOGGING);
+ Warning("Missing file start mark\n");
}
}
}
}
+
+ /*
+ * We may need to close file in case LOG_END_MARK is missing.
+ */
+ if (outfp) {
+ ASSERT(state == IN_GUEST_LOGGING);
+ fclose(outfp);
+ }
fclose(fp);
}