Example #1
0
        public int FillTailCache(LogFileStream logFileStream)
        {
            int lineCount = 0;

            // Quickly fast forward to near the file bottom
            if (!logFileStream.CloseToEnd(Items.Count))
            {
                do
                {
                    if (logFileStream.ReadLine(lineCount + 1) != null)
                        lineCount++;

                    // We have read a good part of the file
                    if (lineCount % Items.Count == 0)
                    {
                        if (LoadingFileEvent != null)
                            LoadingFileEvent(logFileStream, null);
                    }
                }
                while (!logFileStream.CloseToEnd(Items.Count));

                // We are almost finished with loading the file
                if (LoadingFileEvent != null)
                    LoadingFileEvent(null, null);
            }

            // Read the last lines of the file into the cache
            bool continueReading = false;
            PrepareCache(lineCount, lineCount, true);
            do
            {
                int lastCacheIndex = FillCache(logFileStream, FirstIndex + Items.Count);
                System.Diagnostics.Debug.Assert(lastCacheIndex != -1 || logFileStream.Length == 0);
                continueReading = (lastCacheIndex == Items.Count - 1);
                lineCount = FirstIndex + lastCacheIndex + 1;
                if (continueReading)
                    PrepareCache(lineCount + Items.Count / 2, lineCount + Items.Count / 2, true);
            } while (continueReading);

            return lineCount;
        }