private void addLogPacket(byte[] logpacket)
{
if (currentLogHeadStream == null) {
throw new Exception("log is not ready for writes, call prepareLog() ! ");
}
// (1) if there is not room on the current log segment, skip to the next segment
{
long pos = currentLogHeadStream.Position;
if (debugLogSegments) {
// this helps us debug log segments by only allowing a single log packet per segment
// TODO: change the tests to actually "fill" log segments, and remove this.
if (pos != 0) {
this.advanceActiveLogSegment();
}
} else {
if ((pos + logpacket.Length + LOG_SEGMENT_RESERVE) > currentLogSegmentInfo.logsegment_size) {
// too big to fit! Advance the curreng log segment;
this.advanceActiveLogSegment();
}
}
}
// (2) check to make sure it will fit in this segment
{
long pos = currentLogHeadStream.Position;
if ((pos + logpacket.Length + LOG_END_MARKER_SIZE) > currentLogSegmentInfo.logsegment_size) {
Console.WriteLine("*\n*\n*\n*\n* LOG RAN OUT OF SPACE\n*\n*\n*\n");
Environment.Exit(1);
// too big to fit in an empty segment!
throw new Exception("log packet too big to fit in log segment!");
}
}
// (3) add the log packet
BinaryWriter bw = new BinaryWriter(currentLogHeadStream);
bw.Write(logpacket);
_doLogEnd(bw);
}